101. Symmetric Tree (easy)
判断树是否为左右对称
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree [1,2,2,3,4,4,3] is symmetric: 1 / \ 2 2 / \ / \ 3 4 4 3 But the following [1,2,2,null,3,null,3] is not: 1 / \ 2 2 \ \ 3 3 Note: Bonus points if you could solve it both recursively and iteratively.
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 bool isSymmetric(TreeNode* root) { 13 return func(root, root); 14 } 15 bool func(TreeNode* l, TreeNode* r) { 16 if (!l || !r) return l == r; 17 return (l -> val == r -> val) && func(l -> left, r -> right) && func(l -> right, r -> left); 18 } 19 };
102. Binary Tree Level Order Traversal (medium)
将二叉树按层级顺序输出到二维数组
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ]
1 // 递归 2 /** 3 * Definition for a binary tree node. 4 * struct TreeNode { 5 * int val; 6 * TreeNode *left; 7 * TreeNode *right; 8 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 9 * }; 10 */ 11 class Solution { 12 public: 13 vectorint>> levelOrder(TreeNode* root) { 14 vector int>> res; 15 func(res, root, 0); 16 return res; 17 } 18 void func(vector int>>& res, TreeNode* root, int depth) { 19 if (!root) return; 20 if (res.size() <= depth) res.push_back(vector<int>()); 21 res[depth].push_back(root -> val); 22 func(res, root -> left, depth + 1); 23 func(res, root -> right, depth + 1); 24 } 25 };
1 // 队列 2 class Solution { 3 public: 4 vectorint> > levelOrder(TreeNode *root) { 5 vector int> > result; 6 if (!root) return result; 7 queue q; 8 q.push(root); 9 q.push(NULL); 10 vector<int> cur_vec; 11 while(!q.empty()) { 12 TreeNode* t = q.front(); 13 q.pop(); 14 if (t==NULL) { 15 result.push_back(cur_vec); 16 cur_vec.resize(0); 17 if (q.size() > 0) { 18 q.push(NULL); 19 } 20 } else { 21 cur_vec.push_back(t->val); 22 if (t->left) q.push(t->left); 23 if (t->right) q.push(t->right); 24 } 25 } 26 return result; 27 } 28 };
103. Binary Tree Zigzag Level Order Traversal (medium)
zigzag的方式层级遍历
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 return its zigzag level order traversal as: [ [3], [20,9], [15,7] ]
1 // 使用队列遍历,写入vector时再考虑反转 2 /** 3 * Definition for a binary tree node. 4 * struct TreeNode { 5 * int val; 6 * TreeNode *left; 7 * TreeNode *right; 8 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 9 * }; 10 */ 11 class Solution { 12 public: 13 vectorint>> zigzagLevelOrder(TreeNode* root) { 14 vector int>> res; 15 queue q; 16 bool symbol = true; 17 if (!root) return res; 18 q.push(root); 19 while (!q.empty()) { 20 int size = q.size(); 21 vector<int> temp(size); 22 for (int i = 0; i < size; ++i) { 23 if (q.front() -> left) q.push(q.front() -> left); 24 if (q.front() -> right) q.push(q.front() -> right); 25 //int index = ; 26 temp[symbol ? i : size - i - 1] = q.front() -> val; 27 q.pop(); 28 } 29 res.push_back(temp); 30 symbol = !symbol; 31 } 32 return res; 33 } 34 };
104. Maximum Depth of Binary Tree (easy)
树的最大深度
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. Note: A leaf is a node with no children. Example: Given binary tree [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 return its depth = 3.
1 // 递归:dfs 2 /** 3 * Definition for a binary tree node. 4 * struct TreeNode { 5 * int val; 6 * TreeNode *left; 7 * TreeNode *right; 8 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 9 * }; 10 */ 11 class Solution { 12 public: 13 int maxDepth(TreeNode* root) { 14 if (!root) return 0; 15 return 1 + max(maxDepth(root -> left), maxDepth(root -> right)); 16 } 17 };
1 // 队列:bfs 2 int maxDepth(TreeNode *root) 3 { 4 if(root == NULL) 5 return 0; 6 7 int res = 0; 8 queueq; 9 q.push(root); 10 while(!q.empty()) 11 { 12 ++ res; 13 for(int i = 0, n = q.size(); i < n; ++ i) 14 { 15 TreeNode *p = q.front(); 16 q.pop(); 17 18 if(p -> left != NULL) 19 q.push(p -> left); 20 if(p -> right != NULL) 21 q.push(p -> right); 22 } 23 } 24 25 return res; 26 }
105. Construct Binary Tree from Preorder and Inorder Traversal (medium)
通过二叉树的前序遍历和中序遍历,还原二叉树
106. Construct Binary Tree from Inorder and Postorder Traversal (medium)
通过二叉树的后序遍历和中序遍历,还原二叉树
107. Binary Tree Level Order Traversal II (easy)
从下往上层级遍历
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example: Given binary tree [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 return its bottom-up level order traversal as: [ [15,7], [9,20], [3] ]
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 vectorint>> levelOrderBottom(TreeNode* root) { 13 vector int>> res; 14 queue q; 15 if (!root) return res; 16 q.push(root); 17 while (!q.empty()) { 18 int size = q.size(); 19 vector<int> temp; 20 for (int i = 0; i < size; ++i) { 21 if (q.front() -> left) q.push(q.front() -> left); 22 if (q.front() -> right) q.push(q.front() -> right); 23 temp.push_back(q.front() -> val); 24 q.pop(); 25 } 26 res.push_back(temp); 27 } 28 reverse(res.begin(), res.end()); 29 return res; 30 } 31 };
108. Convert Sorted Array to Binary Search Tree (easy)
将排序好的数组构造成BST
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. Example: Given the sorted array: [-10,-3,0,5,9], One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST: 0 / \ -3 9 / / -10 5
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 TreeNode* sortedArrayToBST(vector<int>& nums) { 13 return func(nums, 0, nums.size() - 1); 14 } 15 TreeNode* func(vector<int>& nums, int left, int right) { 16 if (left > right) return NULL; 17 int index = (left + right)/2; 18 TreeNode* node = new TreeNode(nums[index]); 19 node -> left = func(nums, left, index - 1); 20 node -> right = func(nums, index + 1, right); 21 return node; 22 } 23 };
109. Convert Sorted List to Binary Search Tree (medium) #
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
将有序链表转换成平衡二叉树
110. Balanced Binary Tree (easy) #
判断二叉树是否为height-balanced.
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as: a binary tree in which the depth of the two subtrees of every node never differ by more than 1. Example 1: Given the following tree [3,9,20,null,null,15,7]: 3 / \ 9 20 / \ 15 7 Return true. Example 2: Given the following tree [1,2,2,3,3,null,null,4,4]: 1 / \ 2 2 / \ 3 3 / \ 4 4 Return false.
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 bool isBalanced(TreeNode* root) { 13 return depth(root) != -1; 14 } 15 int depth(TreeNode* root) { 16 if (!root) return 0; 17 int left = depth(root -> left); 18 int right = depth(root -> right); 19 if (left == -1 || right == -1 || abs(left - right) > 1) return -1; 20 return max(left, right) + 1; 21 } 22 };
111. Minimum Depth of Binary Tree (easy)
树的最小深度
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. Note: A leaf is a node with no children. Example: Given binary tree [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 return its minimum depth = 2.
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 int minDepth(TreeNode* root) { 13 if (!root) return 0; 14 if (!root -> left) return minDepth(root -> right) + 1; 15 if (!root -> right) return minDepth(root -> left) + 1; 16 return min(minDepth(root -> left), minDepth(root -> right)) + 1; 17 } 18 };
112. Path Sum (easy)
是否存在根节点到叶节点的累加等于sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. Note: A leaf is a node with no children. Example: Given the below binary tree and sum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ \ 7 2 1 return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 bool hasPathSum(TreeNode* root, int sum) { 13 if (!root) return false; 14 if (root -> left == NULL && root -> right == NULL && root -> val == sum) return true; 15 return hasPathSum(root -> left, sum - root -> val) || hasPathSum(root -> right, sum - root -> val); 16 } 17 };
113. Path Sum II (medium)
根节点到叶节点的累加等于sum的所有路径
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. Note: A leaf is a node with no children. Example: Given the below binary tree and sum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1 Return: [ [5,4,11,2], [5,8,4,5] ]
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 vectorint>> pathSum(TreeNode* root, int sum) { 13 vector<int> temp; 14 vector int>> res; 15 func(root, res, sum, temp); 16 return res; 17 } 18 void func(TreeNode* root, vector int>>& res, int sum, vector<int>& temp) { 19 if (!root) return; 20 temp.push_back(root -> val); 21 if (root -> left == NULL && root -> right == NULL && sum == root -> val) res.push_back(temp); 22 if (root -> left) func(root -> left, res, sum - root -> val, temp); 23 if (root -> right) func(root -> right, res, sum - root -> val, temp); 24 temp.pop_back(); 25 } 26 };
114. Flatten Binary Tree to Linked List (medium) #
将二叉树展开为链表
Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 / \ 2 5 / \ \ 3 4 6 The flattened tree should look like: 1 \ 2 \ 3 \ 4 \ 5 \ 6
1 // 先序遍历会改变right指针,所以采用先访问right,再访问left的后序遍历,把节点都放在栈中,再改变连接 2 /** 3 * Definition for a binary tree node. 4 * struct TreeNode { 5 * int val; 6 * TreeNode *left; 7 * TreeNode *right; 8 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 9 * }; 10 */ 11 class Solution { 12 public: 13 TreeNode* node; 14 void flatten(TreeNode* root) { 15 if (!root) return; 16 flatten(root -> right); 17 flatten(root -> left); 18 root -> left = NULL; 19 root -> right = node; 20 node = root; 21 } 22 };
115. Distinct Subsequences (hard)
一个string删除某些字母后得到另一个string的方式数量
116. Populating Next Right Pointers in Each Node (medium) #
为完全二叉树节点增加next指针,指向右边的节点
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL. Initially, all next pointers are set to NULL. Note: You may only use constant extra space. Recursive approach is fine, implicit stack space does not count as extra space for this problem. You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children). Example: Given the following perfect binary tree, 1 / \ 2 3 / \ / \ 4 5 6 7 After calling your function, the tree should look like: 1 -> NULL / \ 2 -> 3 -> NULL / \ / \ 4->5->6->7 -> NULL
1 // 利用next指针bfs遍历 2 /** 3 * Definition for binary tree with next pointer. 4 * struct TreeLinkNode { 5 * int val; 6 * TreeLinkNode *left, *right, *next; 7 * TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 void connect(TreeLinkNode *root) { 13 TreeLinkNode* cur = root; 14 TreeLinkNode* pre = root; 15 while (pre && pre -> left) { 16 cur = pre; 17 while (cur && cur -> right) { 18 cur -> left -> next = cur -> right; 19 if (cur -> next) cur -> right -> next = cur -> next -> left; 20 cur = cur -> next; 21 } 22 pre = pre -> left; 23 } 24 } 25 };
117. Populating Next Right Pointers in Each Node II (medium)
为二叉树节点增加next指针,指向右边的节点
1 Given a binary tree 2 3 struct TreeLinkNode { 4 TreeLinkNode *left; 5 TreeLinkNode *right; 6 TreeLinkNode *next; 7 } 8 Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL. 9 10 Initially, all next pointers are set to NULL. 11 12 Note: 13 14 You may only use constant extra space. 15 Recursive approach is fine, implicit stack space does not count as extra space for this problem. 16 Example: 17 18 Given the following binary tree, 19 20 1 21 / \ 22 2 3 23 / \ \ 24 4 5 7 25 After calling your function, the tree should look like: 26 27 1 -> NULL 28 / \ 29 2 -> 3 -> NULL 30 / \ \ 31 4-> 5 -> 7 -> NULL
1 /** 2 * Definition for binary tree with next pointer. 3 * struct TreeLinkNode { 4 * int val; 5 * TreeLinkNode *left, *right, *next; 6 * TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 void connect(TreeLinkNode *root) { 12 TreeLinkNode* cur = root; 13 TreeLinkNode* pre = NULL; 14 TreeLinkNode* next_level = NULL; 15 while (cur) { 16 while (cur) { 17 if (cur -> left) { 18 if (pre) pre -> next = cur -> left; 19 else next_level = cur -> left; 20 pre = cur -> left; 21 } 22 if (cur -> right) { 23 if (pre) pre -> next = cur -> right; 24 else next_level = cur -> right; 25 pre = cur -> right; 26 } 27 cur = cur -> next; 28 } 29 cur = next_level; 30 pre = NULL; 31 next_level = NULL; 32 } 33 } 34 };
118. Pascal's Triangle (easy)
杨辉三角
119. Pascal's Triangle II (easy)
杨辉三角的第k行
120. Triangle (medium)
121. Best Time to Buy and Sell Stock (easy)
股票的最好买入卖出时间,返回最大收益
122. Best Time to Buy and Sell Stock II (easy)
股票的最好买入卖出时间,返回最大收益,可进行多次交易
123. Best Time to Buy and Sell Stock III (hard)
股票的最好买入卖出时间,返回最大收益,最多进行两次交易
124. Binary Tree Maximum Path Sum (hard)
任意节点到另外一个任意节点路径上的数值和最大是多少
Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and does not need to go through the root. Example 1: Input: [1,2,3] 1 / \ 2 3 Output: 6 Example 2: Input: [-10,9,20,null,null,15,7] -10 / \ 9 20 / \ 15 7 Output: 42
1 // java 2 public class Solution { 3 int maxValue; 4 5 public int maxPathSum(TreeNode root) { 6 maxValue = Integer.MIN_VALUE; 7 maxPathDown(root); 8 return maxValue; 9 } 10 11 private int maxPathDown(TreeNode node) { 12 if (node == null) return 0; 13 int left = Math.max(0, maxPathDown(node.left)); 14 int right = Math.max(0, maxPathDown(node.right)); 15 maxValue = Math.max(maxValue, left + right + node.val); 16 return Math.max(left, right) + node.val; 17 } 18 }
125. Valid Palindrome (easy)
判断回文
126. Word Ladder II (hard)
单词接龙,前后单词只有一个字母不同
127. Word Ladder (medium) #
词语接龙,给出候选词
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that: Only one letter can be changed at a time. Each transformed word must exist in the word list. Note that beginWord is not a transformed word. Note: Return 0 if there is no such transformation sequence. All words have the same length. All words contain only lowercase alphabetic characters. You may assume no duplicates in the word list. You may assume beginWord and endWord are non-empty and are not the same. Example 1: Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"] Output: 5 Explanation: As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog", return its length 5. Example 2: Input: beginWord = "hit" endWord = "cog" wordList = ["hot","dot","dog","lot","log"] Output: 0 Explanation: The endWord "cog" is not in wordList, therefore no possible transformation.
1 class Solution { 2 public: 3 int ladderLength(string beginWord, string endWord, unordered_set<string>& wordDict) { 4 unordered_set<string> head, tail, *phead, *ptail; 5 head.insert(beginWord); 6 tail.insert(endWord); 7 int dist = 2; 8 while (!head.empty() && !tail.empty()) { 9 if (head.size() < tail.size()) { 10 phead = &head; 11 ptail = &tail; 12 } 13 else { 14 phead = &tail; 15 ptail = &head; 16 } 17 unordered_set<string> temp; 18 for (auto itr = phead -> begin(); itr != phead -> end(); itr++) { 19 string word = *itr; 20 wordDict.erase(word); 21 for (int p = 0; p < (int)word.length(); p++) { 22 char letter = word[p]; 23 for (int k = 0; k < 26; k++) { 24 word[p] = 'a' + k; 25 if (ptail -> find(word) != ptail -> end()) 26 return dist; 27 if (wordDict.find(word) != wordDict.end()) { 28 temp.insert(word); 29 wordDict.erase(word); 30 } 31 } 32 word[p] = letter; 33 } 34 } 35 dist++; 36 swap(*phead, temp); 37 } 38 return 0; 39 } 40 };
128. Longest Consecutive Sequence (hard) #
最长的连续序列:找到数组中可以组成的连续数字的最长长度
129. Sum Root to Leaf Numbers (medium)
所有根节点到叶节点组成数字之和
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. Note: A leaf is a node with no children. Example: Input: [1,2,3] 1 / \ 2 3 Output: 25 Explanation: The root-to-leaf path 1->2 represents the number 12. The root-to-leaf path 1->3 represents the number 13. Therefore, sum = 12 + 13 = 25. Example 2: Input: [4,9,0,5,1] 4 / \ 9 0 / \ 5 1 Output: 1026 Explanation: The root-to-leaf path 4->9->5 represents the number 495. The root-to-leaf path 4->9->1 represents the number 491. The root-to-leaf path 4->0 represents the number 40. Therefore, sum = 495 + 491 + 40 = 1026.
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 int sumNumbers(TreeNode* root) { 13 return func(root, 0); 14 } 15 int func(TreeNode* root, int num) { 16 if (!root) return 0; 17 if (root -> left == NULL && root -> right == NULL) return num*10 + root -> val; 18 return func(root -> left, num*10 + root -> val) + func(root -> right, num*10 + root -> val); 19 } 20 };
130. Surrounded Regions (medium)
把围住的点同化掉
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region. Example: X X X X X O O X X X O X X O X X After running your function, the board should be: X X X X X X X X X X X X X O X X Explanation: Surrounded regions shouldn’t be on the border, which means that any 'O' on the border of the board are not flipped to 'X'. Any 'O' that is not on the border and it is not connected to an 'O' on the border will be flipped to 'X'. Two cells are connected if they are adjacent cells connected horizontally or vertically.
1 class Solution { 2 public: 3 int height, width; 4 void solve(vectorchar>>& board) { 5 if (board.size() == 0 || board[0].size() == 0) return; 6 height = board.size(); 7 width = board[0].size(); 8 for (int i = 0; i < height; ++i) { 9 alter(board, i, 0); 10 if (width > 1) alter(board, i, width - 1); 11 } 12 for (int i = 0; i < width; ++i) { 13 alter(board, 0, i); 14 if (height > 1) alter(board, height - 1, i); 15 } 16 for (int i = 0; i < height; ++i) { 17 for (int j = 0; j < width; ++j) { 18 if (board[i][j] == 'O') board[i][j] = 'X'; 19 if (board[i][j] == 'Y') board[i][j] = 'O'; 20 } 21 } 22 } 23 void alter(vector char>>& board, int x, int y) { 24 if (board[x][y] == 'O') { 25 board[x][y] = 'Y'; 26 if (x > 0) alter(board, x - 1, y); 27 if (x < height - 1) alter(board, x + 1, y); 28 if (y > 0) alter(board, x, y - 1); 29 if (y < width - 1) alter(board, x, y + 1); 30 } 31 } 32 };
131. Palindrome Partitioning (medium)
把string拆分为回文的所有情况
Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. Example: Input: "aab" Output: [ ["aa","b"], ["a","a","b"] ]
1 class Solution { 2 public: 3 vectorstring>> partition(string s) { 4 vector string>> res; 5 vector<string> tmp; 6 backtracking(res, tmp, 0, s.size(), s); 7 return res; 8 } 9 void backtracking(vector string>>& res, vector<string>& tmp, int start, int end, string& s) { 10 if (start == end) { 11 res.push_back(tmp); 12 return; 13 } 14 for (int i = start; i < end; ++i) { 15 if (is_palindrome(start, i, s)) { 16 tmp.push_back(s.substr(start, i - start + 1)); 17 backtracking(res, tmp, i + 1, end, s); 18 tmp.pop_back(); 19 } 20 } 21 } 22 bool is_palindrome(int start, int end, string& s) { 23 while (start <= end) 24 if (s[start++] != s[end--]) 25 return false; 26 return true; 27 } 28 };
132. Palindrome Partitioning II (hard) ##
把string拆分为回文的切割数
1 class Solution { 2 public: 3 int minCut(string s) { 4 int n = s.size(); 5 vector<int> cut(n+1, 0); // number of cuts for the first k characters 6 for (int i = 0; i <= n; i++) cut[i] = i-1; 7 for (int i = 0; i < n; i++) { 8 for (int j = 0; i-j >= 0 && i+j < n && s[i-j]==s[i+j] ; j++) // odd length palindrome 9 cut[i+j+1] = min(cut[i+j+1],1+cut[i-j]); 10 11 for (int j = 1; i-j+1 >= 0 && i+j < n && s[i-j+1] == s[i+j]; j++) // even length palindrome 12 cut[i+j+1] = min(cut[i+j+1],1+cut[i-j+1]); 13 } 14 return cut[n]; 15 } 16 };
132. Palindrome Partitioning II (hard) #
136. Single Number (easy)
找出只出现一次的数字
138. Copy List with Random Pointer (medium) #
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.
Return a deep copy of the list.
链表的节点中包含一个指向随机节点的指针,复制这个链表
141. Linked List Cycle (easy)
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
确认链表中是否有环
142. Linked List Cycle II (medium)
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
Note: Do not modify the linked list.
Follow up:
Can you solve it without using extra space?
确认链表中是否有环,以及环的起始位置
143. Reorder List (medium)
Given a singly linked list L: L0→L1→…→Ln-1→Ln,
reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…
You must do this in-place without altering the nodes’ values.
For example,
Given {1,2,3,4}, reorder it to {1,4,2,3}.
147. Insertion Sort List (medium)
Sort a linked list using insertion sort.
排序链表,使用插入法
148. Sort List (medium)
Sort a linked list in O(n log n) time using constant space complexity.
对链表进行排序
149. Max Points on a Line (hard) # 待做
在同一条线上的做多的点数
151. Reverse Words in a String (medium)
反转string中的word,并去掉头尾空格
152. Maximum Product Subarray (medium) #
拥有最大乘积的子序列
153. Find Minimum in Rotated Sorted Array (medium)
在旋转后的有序数列中找到最小值
154. Find Minimum in Rotated Sorted Array II (hard)
在旋转后的有序数列中找到最小值,,数组中可能有重复的数
160. Intersection of Two Linked Lists (easy)
Write a program to find the node at which the intersection of two singly linked lists begins.
寻找两个链表相交的位置
162. Find Peak Element (medium)
找一个峰值,只要大于两边的数就可以,nums[-1]=nums[n]=负无穷
165. Compare Version Numbers (medium)
版本大小判断
166. Fraction to Recurring Decimal (medium)
两个数相除,返回字符串,循环部分写在小括号里
167. Two Sum II - Input array is sorted (easy)
有序数组中找到和为target的两个数的索引(结果只有一组)
169. Majority Element (easy)
找到出现次数大于n/2的元素
187. Repeated DNA Sequences (medium) #
找出长度为10的重复的字符串
189. Rotate Array (easy)
旋转数组,使最后的k个数旋转到最前面
#