冲刺001:无重复字符的最长子串
class Solution {
public int lengthOfLongestSubstring(String s) {
int left = 0, n = s.length(), ans = 0;
Map<Character, Integer> map = new HashMap<>();
for (int i = 0; i < n; i++) {
if (map.containsKey(s.charAt(i))) {
left = Math.max(left, map.get(s.charAt(i)) + 1);
}
map.put(s.charAt(i), i);
ans = Math.max(ans, i - left + 1);
}
return ans;
}
}
冲刺002:K 个一组翻转链表
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
ListNode hair = new ListNode(0);
hair.next = head;
ListNode pre = hair;
ListNode cur = head;
while (cur != null) {
ListNode tail = pre;
for (int i = 0; i < k; i++) {
tail = tail.next;
if (tail == null) {
return hair.next;
}
}
ListNode next = tail.next;
ListNode[] reverse = myReverse(cur, tail);
cur = reverse[0];
tail = reverse[1];
pre.next = cur;
tail.next = next;
pre = tail;
cur = tail.next;
}
return hair.next;
}
public ListNode[] myReverse(ListNode cur, ListNode tail) {
ListNode head = cur;
ListNode pre = null;
while (cur != tail) {
ListNode next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
cur.next = pre;
return new ListNode[]{cur, head};
}
}
冲刺003:有效的括号
class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
Map<Character, Character> map = new HashMap<>(){{
put(')', '(');
put(']', '[');
put('}', '{');
}};
for (int i = 0; i < s.length(); i++) {
Character c = s.charAt(i);
if (c == '(' || c == '[' || c == '{') {
stack.add(c);
} else {
if (!stack.empty() && map.get(c) == stack.peek()) {
stack.pop();
} else {
return false;
}
}
}
if (stack.empty()) {
return true;
} else {
return false;
}
}
}
冲刺004:两数之和
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int pair = target - nums[i];
if (map.containsKey(pair)) {
return new int[] {i, map.get(pair)};
}
map.put(nums[i], i);
}
return new int[0];
}
}
冲刺005:三数之和
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
Arrays.sort(nums);
for (int first = 0; first < nums.length; first++) {
if (first > 0 && nums[first] == nums[first-1]) {
continue;
}
int third = nums.length - 1;
int target = - nums[first];
for (int second = first + 1; second < nums.length; second++) {
if (second > first + 1 && nums[second] == nums[second-1]) {
continue;
}
while (second < third && nums[second] + nums[third] > target) {
third--;
}
if (second == third) {
break;
}
if (nums[second] + nums[third] == target) {
List<Integer> tmp = new ArrayList<>();
tmp.add(nums[first]);
tmp.add(nums[second]);
tmp.add(nums[third]);
res.add(tmp);
}
}
}
return res;
}
}
冲刺006:螺旋矩阵
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> res = new ArrayList<>();
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return res;
}
int rows = matrix.length;
int cols = matrix[0].length;
int total = rows * cols;
int x = 0, y = 0;
int dirIndex = 0;
int[][] dir = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
boolean[][] vis = new boolean[rows][cols];
for (int i = 0; i < total; i++) {
res.add(matrix[x][y]);
vis[x][y] = true;
int tx = x + dir[dirIndex][0];
int ty = y + dir[dirIndex][1];
if (tx < 0 || tx >= rows || ty < 0 || ty >= cols || vis[tx][ty]) {
dirIndex = (dirIndex + 1) % 4;
}
x += dir[dirIndex][0];
y += dir[dirIndex][1];
}
return res;
}
}
冲刺007:搜索旋转排序数组
class Solution {
public int search(int[] nums, int target) {
int n = nums.length;
if (n == 0) return -1;
else if (n == 1) return nums[0] == target ? 0 : -1;
int l = 0, r = nums.length - 1;
while(l <= r) {
int mid = (l + r) / 2;
if (nums[mid] == target) {
return mid;
}
if (nums[l] <= nums[mid]) {
if (nums[l] <= target && target < nums[mid]) {
r = mid - 1;
} else {
l = mid + 1;
}
} else {
if (nums[mid] < target && target <= nums[r]) {
l = mid + 1;
} else {
r = mid - 1;
}
}
}
return -1;
}
}
冲刺008:合并两个有序链表
冲刺009:下一个排列
- 冲刺010:接雨水(https://leetcode-cn.com/problems/trapping-rain-water/)
- 冲刺011:最大子序和(https://leetcode-cn.com/problems/maximum-subarray/)
冲刺012:合并两个有序数组(https://leetcode-cn.com/problems/merge-sorted-array/)
冲刺013:二叉树的层序遍历(https://leetcode-cn.com/problems/binary-tree-level-order-traversal/)
- 冲刺014:二叉树的锯齿形层序遍历(https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal/)
冲刺015:从前序与中序遍历序列构造二叉树(https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/)
冲刺016:买卖股票的最佳时机(https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/)
- 冲刺017:环形链表(https://leetcode-cn.com/problems/linked-list-cycle/)
冲刺018:LRU 缓存机制(https://leetcode-cn.com/problems/lru-cache/)
冲刺019:相交链表(https://leetcode-cn.com/problems/intersection-of-two-linked-lists/)
- 冲刺020:二叉树的右视图(https://leetcode-cn.com/problems/binary-tree-right-side-view/)
冲刺021:岛屿数量(https://leetcode-cn.com/problems/number-of-islands/)
冲刺022:反转链表(https://leetcode-cn.com/problems/reverse-linked-list/)
- 冲刺023:数组中的第K个最大元素(https://leetcode-cn.com/problems/kth-largest-element-in-an-array/)
冲刺024:二叉树的最近公共祖先(https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/)
冲刺025:最长递增子序列(https://leetcode-cn.com/problems/longest-increasing-subsequence/)
- 冲刺026:字符串相加(https://leetcode-cn.com/problems/add-strings/)
冲刺027:合并K个升序链表(https://leetcode-cn.com/problems/merge-k-sorted-lists/)
冲刺028:两数相加(https://leetcode-cn.com/problems/add-two-numbers/)
- 冲刺029:最长回文子串(https://leetcode-cn.com/problems/longest-palindromic-substring/)
冲刺030:组合总和(https://leetcode-cn.com/problems/combination-sum/)
冲刺031:缺失的第一个正数(https://leetcode-cn.com/problems/first-missing-positive/)
- 冲刺032:全排列(https://leetcode-cn.com/problems/permutations/)
冲刺033:合并区间(https://leetcode-cn.com/problems/merge-intervals/)
冲刺034:x 的平方根(https://leetcode-cn.com/problems/sqrtx/)
- 冲刺035:爬楼梯(https://leetcode-cn.com/problems/climbing-stairs/)
冲刺036:最小覆盖子串(https://leetcode-cn.com/problems/minimum-window-substring/)
冲刺037:反转链表 II(https://leetcode-cn.com/problems/reverse-linked-list-ii/)
- 冲刺038:二叉树的中序遍历(https://leetcode-cn.com/problems/binary-tree-inorder-traversal/)
冲刺039:验证二叉搜索树(https://leetcode-cn.com/problems/validate-binary-search-tree/)
冲刺040:对称二叉树(https://leetcode-cn.com/problems/symmetric-tree/)
- 冲刺041:路径总和(https://leetcode-cn.com/problems/path-sum/)
冲刺042:路径总和 II(https://leetcode-cn.com/problems/path-sum-ii/)
冲刺043:二叉树中的最大路径和(https://leetcode-cn.com/problems/binary-tree-maximum-path-sum/)
- 冲刺044:求根节点到叶节点数字之和(https://leetcode-cn.com/problems/sum-root-to-leaf-numbers/)
冲刺045:环形链表 II(https://leetcode-cn.com/problems/linked-list-cycle-ii/)
冲刺046:重排链表(https://leetcode-cn.com/problems/reorder-list/)
- 冲刺047:最小栈(https://leetcode-cn.com/problems/min-stack/)
冲刺048:用栈实现队列(https://leetcode-cn.com/problems/implement-queue-using-stacks/)
冲刺049:二叉树的完全性检验(https://leetcode-cn.com/problems/check-completeness-of-a-binary-tree/)
- 冲刺050:剑指 Offer 22. 链表中倒数第k个节点(https://leetcode-cn.com/problems/lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof/)
冲刺051:剑指 Offer 36. 二叉搜索树与双向链表(https://leetcode-cn.com/problems/er-cha-sou-suo-shu-yu-shuang-xiang-lian-biao-lcof/)
冲刺052:寻找两个正序数组的中位数(https://leetcode-cn.com/problems/median-of-two-sorted-arrays/)
- 冲刺053:字符串转换整数 (atoi)(https://leetcode-cn.com/problems/string-to-integer-atoi/)
冲刺054:删除链表的倒数第 N 个结点(https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/)
冲刺055:两两交换链表中的节点(https://leetcode-cn.com/problems/swap-nodes-in-pairs/)
- 冲刺056:最长有效括号(https://leetcode-cn.com/problems/longest-valid-parentheses/)
冲刺057:旋转图像(https://leetcode-cn.com/problems/rotate-image/)
冲刺058:N 皇后(https://leetcode-cn.com/problems/n-queens/)
- 冲刺059:不同路径(https://leetcode-cn.com/problems/unique-paths/)
冲刺060:用两个栈实现队列(https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof/)
冲刺061:最小路径和(https://leetcode-cn.com/problems/minimum-path-sum/)
- 冲刺062:编辑距离(https://leetcode-cn.com/problems/edit-distance/)
冲刺064:单词搜索(https://leetcode-cn.com/problems/word-search/)
- 冲刺065:删除排序链表中的重复元素(https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/)
冲刺066:删除排序链表中的重复元素 II(https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii/)
冲刺070:二叉树展开为链表(https://leetcode-cn.com/problems/flatten-binary-tree-to-linked-list/)
- 冲刺071:买卖股票的最佳时机 II(https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/)
- 冲刺072:单词拆分(https://leetcode-cn.com/problems/word-break/)
- 冲刺073:二叉树的前序遍历(https://leetcode-cn.com/problems/binary-tree-preorder-traversal/)
- 冲刺074:排序链表(https://leetcode-cn.com/problems/sort-list/)
冲刺075:翻转字符串里的单词(https://leetcode-cn.com/problems/reverse-words-in-a-string/)
冲刺076:寻找峰值(https://leetcode-cn.com/problems/find-peak-element/)
- 冲刺077:比较版本号(https://leetcode-cn.com/problems/compare-version-numbers/)
冲刺078:多数元素(https://leetcode-cn.com/problems/majority-element/)
冲刺079:打家劫舍(https://leetcode-cn.com/problems/house-robber/)
- 冲刺080:长度最小的子数组(https://leetcode-cn.com/problems/minimum-size-subarray-sum/)
冲刺081:最大正方形(https://leetcode-cn.com/problems/maximal-square/)
冲刺082:基本计算器(https://leetcode-cn.com/problems/basic-calculator/)
- 冲刺083:翻转二叉树(https://leetcode-cn.com/problems/invert-binary-tree/)
冲刺084:二叉搜索树中第K小的元素(https://leetcode-cn.com/problems/kth-smallest-element-in-a-bst/)
冲刺085:回文链表(https://leetcode-cn.com/problems/palindrome-linked-list/)
- 冲刺086:滑动窗口最大值(https://leetcode-cn.com/problems/sliding-window-maximum/)
冲刺087:搜索二维矩阵 II(https://leetcode-cn.com/problems/search-a-2d-matrix-ii/)
冲刺088:丑数 II(https://leetcode-cn.com/problems/ugly-number-ii/)
- 冲刺089:零钱兑换(https://leetcode-cn.com/problems/coin-change/)
冲刺090:字符串解码(https://leetcode-cn.com/problems/decode-string/)
冲刺091:用 Rand7() 实现 Rand10()(https://leetcode-cn.com/problems/implement-rand10-using-rand7/)
- 冲刺092:零钱兑换 II(https://leetcode-cn.com/problems/coin-change-2/)
- 冲刺093:二叉树的直径(https://leetcode-cn.com/problems/diameter-of-binary-tree/)
- 冲刺094:最大交换(https://leetcode-cn.com/problems/maximum-swap/)
- 冲刺095:岛屿的最大面积(https://leetcode-cn.com/problems/max-area-of-island/)
冲刺096:二分查找(https://leetcode-cn.com/problems/binary-search/)
冲刺097:最长重复子数组(https://leetcode-cn.com/problems/maximum-length-of-repeated-subarray/)
- 冲刺098:排序数组(https://leetcode-cn.com/problems/sort-an-array/)
冲刺099:最长公共子序列(https://leetcode-cn.com/problems/longest-common-subsequence/)
冲刺100:规划兼职工作(https://leetcode-cn.com/problems/maximum-profit-in-job-scheduling/)