-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:maxshine/leetcode
# Please enter a …
…commit message to explain why this merge is necessary, # especially if it merges an updated upstream into a topic branch. # # Lines starting with '#' will be ignored, and an empty message aborts # the commit.
- Loading branch information
Showing
15 changed files
with
779 additions
and
0 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
src/[158]read-n-characters-given-read4-ii-call-multiple-times.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.lang.StringBuilder; | ||
|
||
// @solution-sync:begin | ||
/** | ||
* The read4 API is defined in the parent class Reader4. | ||
* int read4(char[] buf4); | ||
*/ | ||
|
||
class Solution extends Reader4 { | ||
/** | ||
* @param buf Destination buffer | ||
* @param n Number of characters to read | ||
* @return The number of actual characters read | ||
*/ | ||
char[] prevBuf = new char[4]; | ||
int prevSize = 0; | ||
int prevIndex = 0; | ||
|
||
public int read(char[] buf, int n) { | ||
int counter = 0; | ||
|
||
while (counter < n) { | ||
if (prevIndex < prevSize) { | ||
buf[counter++] = prevBuf[prevIndex++]; | ||
} else { | ||
prevSize = read4(prevBuf); | ||
prevIndex = 0; | ||
if (prevSize == 0) { | ||
// no more data to consume from stream | ||
break; | ||
} | ||
} | ||
} | ||
return counter; | ||
} | ||
} | ||
// @solution-sync:end | ||
|
||
class Main { | ||
|
||
public static void main(String[] args) { | ||
String buf = "abc"; | ||
int[] n = new int[]{1, 2, 1}; | ||
|
||
int result = new Solution().read(buf, n); | ||
System.out.println(listToString(result)); | ||
} | ||
|
||
private static String listToString(String[] array) { | ||
return listToString(Arrays.asList(array)); | ||
} | ||
|
||
private static String listToString(List<String> list) { | ||
StringBuilder buf = new StringBuilder(); | ||
buf.append("["); | ||
for (int i = 0; i < list.size(); i++) { | ||
if (i != 0) | ||
buf.append(","); | ||
buf.append(list.get(i)); | ||
} | ||
buf.append("]"); | ||
return buf.toString(); | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
src/[159]longest-substring-with-at-most-two-distinct-characters.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
|
||
// @solution-sync:begin | ||
class Solution { | ||
public int lengthOfLongestSubstringTwoDistinct(String s) { | ||
int n = s.length(); | ||
if (n < 3) return n; | ||
|
||
// sliding window left and right pointers | ||
int left = 0; | ||
int right = 0; | ||
// hashmap character -> its rightmost position | ||
// in the sliding window | ||
HashMap<Character, Integer> hashmap = new HashMap<Character, Integer>(); | ||
|
||
int max_len = 2; | ||
|
||
while (right < n) { | ||
// when the slidewindow contains less than 3 characters | ||
hashmap.put(s.charAt(right), right++); | ||
|
||
// slidewindow contains 3 characters | ||
if (hashmap.size() == 3) { | ||
// delete the leftmost character | ||
int del_idx = Collections.min(hashmap.values()); | ||
hashmap.remove(s.charAt(del_idx)); | ||
// move left pointer of the slidewindow | ||
left = del_idx + 1; | ||
} | ||
|
||
max_len = Math.max(max_len, right - left); | ||
} | ||
return max_len; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
class ListNode { | ||
int val; | ||
ListNode next; | ||
ListNode(int x) { | ||
val = x; | ||
next = null; | ||
} | ||
} | ||
|
||
// @solution-sync:begin | ||
class Solution { | ||
public ListNode getIntersectionNodeBrutal(ListNode headA, ListNode headB) { | ||
while (headA != null) { | ||
ListNode pB = headB; | ||
while (pB != null) { | ||
if (headA == pB) return headA; | ||
pB = pB.next; | ||
} | ||
headA = headA.next; | ||
} | ||
return null; | ||
} | ||
|
||
public ListNode getIntersectionNodeHashTable(ListNode headA, ListNode headB) { | ||
Set<ListNode> nodesInB = new HashSet<ListNode>(); | ||
|
||
while (headB != null) { | ||
nodesInB.add(headB); | ||
headB = headB.next; | ||
} | ||
|
||
while (headA != null) { | ||
// if we find the node pointed to by headA, | ||
// in our set containing nodes of B, then return the node | ||
if (nodesInB.contains(headA)) { | ||
return headA; | ||
} | ||
headA = headA.next; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public ListNode getIntersectionNode(ListNode headA, ListNode headB) { | ||
ListNode pA = headA; | ||
ListNode pB = headB; | ||
while (pA != pB) { | ||
pA = pA == null ? headB : pA.next; | ||
pB = pB == null ? headA : pB.next; | ||
} | ||
return pA; | ||
// Note: In the case lists do not intersect, the pointers for A and B | ||
// will still line up in the 2nd iteration, just that here won't be | ||
// a common node down the list and both will reach their respective ends | ||
// at the same time. So pA will be NULL in that case. | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
class Solution { | ||
public boolean isOneEditDistance(String s, String t) { | ||
int ns = s.length(); | ||
int nt = t.length(); | ||
|
||
// Ensure that s is shorter than t. | ||
if (ns > nt) | ||
return isOneEditDistance(t, s); | ||
|
||
// The strings are NOT one edit away distance | ||
// if the length diff is more than 1. | ||
if (nt - ns > 1) | ||
return false; | ||
|
||
for (int i = 0; i < ns; i++) | ||
if (s.charAt(i) != t.charAt(i)) | ||
// if strings have the same length | ||
if (ns == nt) | ||
return s.substring(i + 1).equals(t.substring(i + 1)); | ||
// if strings have different lengths | ||
else | ||
return s.substring(i).equals(t.substring(i + 1)); | ||
|
||
// If there is no diffs on ns distance | ||
// the strings are one edit away only if | ||
// t has one more character. | ||
return (ns + 1 == nt); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
class Solution { | ||
public int findPeakElement(int[] nums) { | ||
int l = 0, r = nums.length - 1; | ||
while (l < r) { | ||
int mid = (l + r) / 2; | ||
if (nums[mid] > nums[mid + 1]) | ||
r = mid; | ||
else | ||
l = mid + 1; | ||
} | ||
return l; | ||
} | ||
|
||
public int findPeakElementLinear(int[] nums) { | ||
for (int i = 0; i < nums.length - 1; i++) { | ||
if (nums[i] > nums[i + 1]) | ||
return i; | ||
} | ||
return nums.length - 1; | ||
} | ||
|
||
public int findPeakElementRecursive(int[] nums) { | ||
return search(nums, 0, nums.length - 1); | ||
} | ||
public int search(int[] nums, int l, int r) { | ||
if (l == r) | ||
return l; | ||
int mid = (l + r) / 2; | ||
if (nums[mid] > nums[mid + 1]) | ||
return search(nums, l, mid); | ||
return search(nums, mid + 1, r); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
// @solution-sync:begin | ||
class Solution { | ||
public List<String> findMissingRanges(int[] nums, int lower, int upper) { | ||
List<String> result = new ArrayList<>(); | ||
int prev = lower - 1; | ||
for (int i = 0; i <= nums.length; i++) { | ||
int curr = (i < nums.length) ? nums[i] : upper + 1; | ||
if (prev + 1 <= curr - 1) { | ||
result.add(formatRange(prev + 1, curr - 1)); | ||
} | ||
prev = curr; | ||
} | ||
return result; | ||
} | ||
|
||
// formats range in the requested format | ||
private String formatRange(int lower, int upper) { | ||
if (lower == upper) { | ||
return String.valueOf(lower); | ||
} | ||
return lower + "->" + upper; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import java.util.Arrays; | ||
|
||
class Solution { | ||
public int maximumGapSimple(int[] nums) { | ||
if (nums.length < 2) // check if array is empty or small sized | ||
return 0; | ||
|
||
Arrays.sort(nums); // sort the array | ||
|
||
int maxGap = 0; | ||
|
||
for (int i = 0; i < nums.length - 1; i++) | ||
maxGap = Math.max(nums[i + 1] - nums[i], maxGap); | ||
|
||
return maxGap; | ||
} | ||
|
||
public int maximumGapRadix(int[] nums) { | ||
if (nums.length < 2) // check if array is empty or small sized | ||
return 0; | ||
|
||
int maxVal = Integer.MIN_VALUE; | ||
for (int i : nums) { | ||
maxVal = Math.max(maxVal, i); | ||
} | ||
|
||
int exp = 1; // 1, 10, 100, 1000 ... | ||
int radix = 10; // base 10 system | ||
|
||
int[] aux = new int[nums.length]; | ||
|
||
/* LSD Radix Sort */ | ||
while (maxVal / exp > 0) { // Go through all digits from LSD to MSD | ||
int[] count = new int[radix]; | ||
Arrays.fill(count, 0); | ||
|
||
for (int i = 0; i < nums.length; i++) // Counting sort | ||
count[(nums[i] / exp) % 10]++; | ||
|
||
for (int i = 1; i < count.length; i++) // you could also use partial_sum() | ||
count[i] += count[i - 1]; | ||
|
||
for (int i = nums.length - 1; i >= 0; i--) | ||
aux[--count[(nums[i] / exp) % 10]] = nums[i]; | ||
|
||
for (int i = 0; i < nums.size(); i++) | ||
nums[i] = aux[i]; | ||
|
||
exp *= 10; | ||
} | ||
|
||
int maxGap = 0; | ||
|
||
for (int i = 0; i < nums.length - 1; i++) | ||
maxGap = Math.max(nums[i + 1] - nums[i], maxGap); | ||
|
||
return maxGap; | ||
} | ||
class Bucket { | ||
public boolean used = false; | ||
public int minval = Integer.MAX_VALUE; // same as INT_MAX | ||
public int maxval = Integer.MIN_VALUE; // same as INT_MIN | ||
}; | ||
|
||
public int maximumGapBucket(int[] nums) { | ||
if (nums.length < 2) // check if array is empty or small sized | ||
return 0; | ||
int maxi = Integer.MIN_VALUE; | ||
int mini = Integer.MAX_VALUE; | ||
for (int i : nums) { | ||
maxi = Math.max(maxi, i); | ||
mini = Math.min(maxi, i); | ||
} | ||
|
||
int bucketSize = Math.max(1, (maxi - mini) / (nums.length - 1)); | ||
int bucketNum = (maxi - mini) / bucketSize + 1; | ||
Bucket[] buckets = new Bucket[bucketNum]; | ||
|
||
for (int num : nums) { | ||
int bucketIdx = (num - mini) / bucketSize; | ||
buckets[bucketIdx].used = true; | ||
buckets[bucketIdx].minval = Math.min(num, buckets[bucketIdx].minval); | ||
buckets[bucketIdx].maxval = Math.max(num, buckets[bucketIdx].maxval); | ||
} | ||
int prevBucketMax = mini, maxGap = 0; | ||
for (Bucket bucket : buckets) { | ||
if (!bucket.used) | ||
continue; | ||
maxGap = Math.max(maxGap, bucket.minval - prevBucketMax); | ||
prevBucketMax = bucket.maxval; | ||
} | ||
return maxGap; | ||
} | ||
} |
Oops, something went wrong.