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
public class BSTIterator { | |
private Stack<TreeNode> stack; | |
private TreeNode cur; | |
public BSTIterator(TreeNode root) { | |
stack = new Stack<>(); | |
cur = root; | |
} | |
/** @return whether we have a next smallest number */ | |
public boolean hasNext() { |
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
class Solution { | |
public ListNode rotateRight(ListNode head, int k) { | |
if (head == null) { | |
return head; | |
} | |
ListNode dummy = new ListNode(0); | |
dummy.next = head; | |
ListNode fast = dummy; | |
ListNode slow = dummy; | |
int length = 0; |
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
class Solution { | |
public String minWindow(String s, String t) { | |
int min = Integer.MAX_VALUE; | |
int minStart = 0; | |
int[] map = new int[128]; | |
for (char c: t.toCharArray()) { | |
map[c]++; | |
} | |
int start = 0; | |
int end = 0; |
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
public class Solution { | |
public boolean hasCycle(ListNode head) { | |
ListNode slow = head; | |
ListNode fast = head; | |
while (fast != null && fast.next != null) { | |
slow = slow.next; | |
fast = fast.next.next; | |
if (slow == fast) { | |
return true; |
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
class Solution { | |
public boolean isSubsequence(String s, String t) { | |
for (int i = 0, pos = 0; i < s.length(); i++, pos++) { | |
pos = t.indexOf(s.charAt(i), pos); | |
if (pos == -1) return false; | |
} | |
return true; | |
} | |
} |
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
class Solution { | |
public int findDuplicate(int[] nums) { | |
int slow = nums[0]; | |
int fast = nums[nums[0]]; | |
while (slow != fast) { | |
slow = nums[slow]; | |
fast = nums[nums[fast]]; | |
} | |
fast = 0; | |
while (slow != fast) { |
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
class Solution { | |
public int divide(int dividend, int divisor) { | |
// according to note: | |
// For the purpose of this problem, assume that your function returns 231 − 1 when the division result overflows. | |
if (dividend == Integer.MIN_VALUE && divisor == -1) { | |
return Integer.MAX_VALUE; | |
} | |
boolean sign = (dividend > 0) ^ (divisor > 0); | |
long dvd = Math.abs((long) dividend); | |
long dvs = Math.abs((long) divisor); |
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
class Solution { | |
public int findMin(int[] nums) { | |
int left = 0; | |
int right = nums.length - 1; | |
while (left < right) { | |
int mid = left + (right - left) / 2; | |
if (nums[mid] <= nums[right]) { | |
right = mid; | |
} else { | |
left = mid + 1; |
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
class Solution { | |
public List<List<Integer>> generate(int numRows) { | |
List<List<Integer>> res = new ArrayList(); | |
List<Integer> row = new ArrayList(); | |
for (int i = 0; i < numRows; i++) { | |
row.add(0, 1); | |
for (int j = 1; j < row.size() - 1; j++) { | |
row.set(j, row.get(j) + row.get(j + 1)); | |
} | |
res.add(new ArrayList(row)); |
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
class Solution { | |
public int maxArea(int[] height) { | |
int start = 0; | |
int end = height.length - 1; | |
int max = 0; | |
while (start < end) { | |
int min = Math.min(height[start], height[end]); | |
max = Math.max(min * (end - start), max); | |
while (height[start] <= min && start < end) { | |
start++; |
NewerOlder