文章目录
- 1. Two Sum
- 3. Longest Substring Without Repeating Characters
- 5. Longest Palindromic Substring
- 23. Merge k Sorted Lists
- 25. Reverse Nodes in k-Group
- 41. First Missing Positive
- 43 Multiply Strings
- 49 Group Anagrams
- 53 Maximum Subarray
- 54 Spiral Matrix
- 62 Unique Paths
- 72 Edit Distance
- 99 Recover Binary Search Tree
- 103 Binary Tree Zigzag Level Order Traversal
- 116 Populating Next Right Pointers in Each Node
- 117 Populating Next Right Pointers in Each Node II
- 128 Longest Consecutive Sequence
- 138 Copy List with Random Pointer
- 139 Word Break
- 146 LRU Cache
- 151 Reverse Words in a String
- 200 Number of Islands
- 210 Course Schedule II
- 212 Word Search II
- 218 The Skyline Problem
- 224 Basic Calculator
- 227 Basic Calculator II
- 236 Lowest Common Ancestor of a Binary Tree
- 243 Shortest Word Distance
- 273 Integer to English Words
- 277 Find the Celebrity
- 295 Find Median from Data Stream
- 297 Serialize and Deserialize Binary Tree
- 348 Design Tic-Tac-Toe
- 428 Serialize and Deserialize N-ary Tree
- 443 String Compression
- 460 LFU Cache
- 510 Inorder Successor in BST II
- 531 Lonely Pixel I
- 545 Boundary of Binary Tree
- 564 Find the Closest Palindrome
- 591 Tag Validator
- 642 Design Search Autocomplete System
- 658 Find K Closest Elements
- 706 Design HashMap
- 721 Accounts Merge
- 722 Remove Comments
- 836 Rectangle Overlap
- 915
- 1239 Maximum Length of a Concatenated String with Unique Characters
- 1448 Count Good Nodes in Binary Tree
- 1576 Replace All ?'s to Avoid Consecutive Repeating Characters
- 1578 Minimum Deletion Cost to Avoid Repeating Letters
- 1615 Maximal Network Rank
- 1647 Minimum Deletions to Make Character Frequencies Unique
1. Two Sum
3. Longest Substring Without Repeating Characters
5. Longest Palindromic Substring
23. Merge k Sorted Lists
25. Reverse Nodes in k-Group
41. First Missing Positive
43 Multiply Strings
49 Group Anagrams
53 Maximum Subarray
54 Spiral Matrix
62 Unique Paths
72 Edit Distance
99 Recover Binary Search Tree
103 Binary Tree Zigzag Level Order Traversal
116 Populating Next Right Pointers in Each Node
117 Populating Next Right Pointers in Each Node II
128 Longest Consecutive Sequence
138 Copy List with Random Pointer
139 Word Break
146 LRU Cache
151 Reverse Words in a String
200 Number of Islands
210 Course Schedule II
212 Word Search II
218 The Skyline Problem
224 Basic Calculator
227 Basic Calculator II
236 Lowest Common Ancestor of a Binary Tree
243 Shortest Word Distance
273 Integer to English Words
277 Find the Celebrity
295 Find Median from Data Stream
297 Serialize and Deserialize Binary Tree
348 Design Tic-Tac-Toe
class TicTacToe {
/*
If at any time a row or column matches the size of the board then that player has won.
*/
private int[] rows;
private int[] cols;
private int diagonal; // 左上到右下的对角线1
private int antiDiagonal; // 右上到左下的对角线2
private int n;
/** Initialize your data structure here. */
public TicTacToe(int n) {
rows = new int[n];
cols = new int[n];
this.n = n;
diagonal = 0;
antiDiagonal = 0;
}
/** Player {player} makes a move at ({row}, {col}).
@param row The row of the board.
@param col The column of the board.
@param player The player, can be either 1 or 2.
@return The current winning condition, can be either:
0: No one wins.
1: Player 1 wins.
2: Player 2 wins. */
public int move(int row, int col, int player) {
int val = (player == 1) ? 1 : -1;
int target = (player == 1) ? n : -n;
// 判断 对角线1
if(row == col){
diagonal += val;
if(diagonal == target){
return player;
}
}
// 判断 对角线2
if(row == n-col-1){
antiDiagonal += val;
if(antiDiagonal == target){
return player;
}
}
rows[row] += val;
cols[col] += val;
if(rows[row] == target || cols[col] == target) return player;
return 0;
}
}
/**
* Your TicTacToe object will be instantiated and called as such:
* TicTacToe obj = new TicTacToe(n);
* int param_1 = obj.move(row,col,player);
*/
428 Serialize and Deserialize N-ary Tree
443 String Compression
class Solution {
public int compress(char[] chars) {
int left = 0;
int size = 0;
int n = chars.length;
for (int i = 0; i <= n; ++i) {
// 当遍历完成,或右指针元素不等于左指针元素时,更新数组
if (i == n || chars[i] != chars[left]) {
// 更新字符
chars[size++] = chars[left];
// 更新计数,当个数大于 1 时才更新
if (i - left > 1) {
for (char c : String.valueOf(i - left).toCharArray()) {
chars[size++] = c;
}
}
left = i;
}
}
return size;
}
}
460 LFU Cache
510 Inorder Successor in BST II
/*
// Definition for a Node.
class Node {
public int val;
public Node left;
public Node right;
public Node parent;
};
*/
class Solution {
public Node inorderSuccessor(Node node) {
if (node == null) {
return null;
}
// case 1: 右子树不为空
if (node.right != null) {
node = node.right;
while (node != null && node.left != null) {
node = node.left;
}
return node;
} else {
// case 2: 右子树不为空不断向上找父节点且当前节点在其左子树上
Node cur = node;
while (cur.parent != null && cur.parent.right == cur) {
cur = cur.parent;
}
return cur.parent;
}
}
}
531 Lonely Pixel I
class Solution {
public int findLonelyPixel(char[][] picture) {
int m = picture.length;
int n = picture[0].length;
int res = 0;
for (