public class Solution {
public ListNode detectCycle(ListNode head) {
if (head == null) return null;
ListNode fast = head, slow = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (fast == slow) {
break;
}
}
if (fast == null || fast.next == null) {
return null;
}
fast = head;
while (fast != slow) {
fast = fast.next;
slow = slow.next;
}
return fast;
}
}
class LRUCache {
LinkedHashMap<Integer, Integer> cache;
int capacity;
public LRUCache(int capacity) {
cache = new LinkedHashMap<>(capacity + 1, 0.75f, true);
this.capacity = capacity;
}
public int get(int key) {
if (!cache.containsKey(key)) {
return -1;
}
return cache.get(key);
}
public void put(int key, int value) {
cache.put(key, value);
if (cache.size() > capacity) {
int oldestKey = cache.keySet().iterator().next();
cache.remove(oldestKey);
}
}
}
class Solution {
public ListNode sortList(ListNode head) {
if (head == null || head.next == null) return head;
ListNode lastNode = getLastNode(head);
ListNode l1 = sortList(head);
ListNode l2 = sortList(lastNode);
return merge(l1, l2);
}
private ListNode merge(ListNode l1, ListNode l2) {
if (l1 == null) return l2;
if (l2 == null) return l1;
if (l1.val < l2.val) {
l1.next = merge(l1.next, l2);
return l1;
} else {
l2.next = merge(l2.next, l1);;
return l2;
}
}
private ListNode getLastNode(ListNode node) {
if (node == null) return null;
ListNode fast = node, slow = node;
ListNode breakNode = node;
while (fast != null && fast.next != null) {
fast = fast.next.next;
breakNode = slow;
slow = slow.next;
}
breakNode.next = null;
return slow;
}
}
class Solution {
public int maxProduct(int[] nums) {
int n = nums.length;
int[] dpMax = new int[n];
int[] dpMin = new int[n];
dpMax[0] = nums[0];
dpMin[0] = nums[0];
int max = nums[0];
for (int i = 1; i < n; i++) {
dpMax[i] = Math.max(nums[i], Math.max(dpMin[i - 1] * nums[i], dpMax[i - 1] * nums[i]));
dpMin[i] = Math.min(nums[i], Math.min(dpMax[i - 1] * nums[i], dpMin[i - 1] * nums[i]));
if (max < dpMax[i]) {
max = dpMax[i];
}
}
return max;
}
}
class MinStack {
Stack<Integer> stack;
Stack<Integer> minStack;
public MinStack() {
stack = new Stack<>();
minStack = new Stack<>();
}
public void push(int val) {
stack.push(val);
if (minStack.isEmpty() || minStack.peek() >= val) {
minStack.push(val);
} else {
minStack.push(Math.min(val, minStack.peek()));
}
}
public void pop() {
if (minStack.isEmpty()) {
return;
}
minStack.pop();
stack.pop();
}
public int top() {
return stack.peek();
}
public int getMin() {
return minStack.peek();
}
}
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode A = headA, B = headB;
while (A != B) {
A = A == null ? headB : A.next;
B = B == null ? headA : B.next;
}
return A;
}
}
class Solution {
public int majorityElement(int[] nums) {
int candidate = 0, count = 0, n = nums.length;
for (int num : nums) {
if (count == 0) {
candidate = num;
count = 1;
} else {
count += (num == candidate) ? 1 : -1;
}
}
return candidate;
}
}
class Solution {
public int rob(int[] nums) {
int n = nums.length;
if (n == 1) return nums[0];
int[][] dp = new int[n][2];
dp[0][0] = 0;
dp[0][1] = nums[0];
for (int i = 1; i < n; i++) {
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1]);
dp[i][1] = dp[i - 1][0] + nums[i];
}
return Math.max(dp[n - 1][0], dp[n - 1][1]);
}
}
class Solution {
public int numIslands(char[][] grid) {
if (grid == null || grid.length == 0) return 0;
int res = 0;
int row = grid.length;
int col = grid[0].length;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (grid[i][j] == '1') {
res += 1;
dfs(grid, i, j, row, col);
}
}
}
return res;
}
private void dfs(char[][] grid, int x, int y, int row, int col) {
if (x < 0 || y < 0 || x >= row || y >= col || grid[x][y] == '0') {
return;
}
grid[x][y] = '0';
dfs(grid, x + 1, y, row, col);
dfs(grid, x - 1, y, row, col);
dfs(grid, x, y - 1, row, col);
dfs(grid, x, y + 1, row, col);
}
}
class Solution {
public ListNode reverseList(ListNode head) {
return reverse(null, head);
}
private ListNode reverse(ListNode pre, ListNode cur) {
if (cur == null) return pre;
ListNode next = cur.next;
cur.next = pre;
return reverse(cur, next);
}
}