# LeetCode题解整理版(一)

## Evaluate Reverse Polish Notation

 class Solution { public: int evalRPN(vector &tokens) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. stack st; for (auto &s: tokens) { if (s.length() > 1 || isdigit(s[0])) st.push(stoi(s)); else { int num1 = st.top(); st.pop(); int num2 = st.top(); st.pop(); if (s[0] == '+') num2 += num1; if (s[0] == '-') num2 -= num1; if (s[0] == '*') num2 *= num1; if (s[0] == '/') num2 /= num1; st.push(num2); } } return st.top(); } }; 

## Max Points on a Line

 /** * Definition for a point. * struct Point { * int x; * int y; * Point() : x(0), y(0) {} * Point(int a, int b) : x(a), y(b) {} * }; */ class Solution { public: int maxPoints(vector &points) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. int size = points.size(); int ans = 0, horz, same; map mp; map::iterator it; for (int i = 0; i < size; i++) { mp.clear(); horz = same = 0; for (int j = 0; j < points.size(); j++) { if (points[j].x == points[i].x && points[j].y == points[i].y) same++; else if (points[j].x == points[i].x) horz ++; else mp[(points[j].y-points[i].y)*1.0/(points[j].x-points[i].x)]++; } if (horz + same > ans) ans = horz + same; for (it = mp.begin(); it != mp.end(); it++) if (it->second + same > ans) ans = it->second + same; } return ans; } }; 

## Sort List

 class Solution { public: ListNode *sortList(ListNode *head) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. if (head == NULL) return head; ListNode *tail = head; int size = 0; for (; tail!= NULL; tail = tail->next, size++); MergeSort(head, tail, size); return head; } void MergeSort(ListNode *l, ListNode *r, int size) { if (l->next == r) return; ListNode *m = l; for (int i = 0; i < size / 2; i++, m = m->next); MergeSort(l, m, size / 2); MergeSort(m, r, size - size / 2); ListNode *lp = l, *rp = m, *s; if (l->val > m-> val) { swap(l->val, m->val); for (ListNode *t = m; t->next != r && t->val > t->next->val; swap(t->val, t->next->val), t = t->next); } s = lp, lp = lp->next; for (int i = 1; i < size; i++) { if (rp == r || (lp !=m && lp->val < rp->val)) s->next = lp, lp = lp->next; else s->next = rp, rp = rp->next; s = s->next; } s->next = r; } void swap (int &x, int &y) { x^=y^=x^=y; } }; 

## Insertion Sort List

 class Solution { public: ListNode *insertionSortList(ListNode *head) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. if (head == NULL) return head; ListNode *newHead = new ListNode(0); ListNode *nextHead = head->next, *now; newHead->next = head, head->next = NULL; while (nextHead != NULL){ now = nextHead, nextHead = nextHead->next; for (ListNode *h = newHead; h != NULL; h = h->next) { if (h->next == NULL || now->val < h->next->val) { ListNode *tmp = h->next; h->next = now, now->next = tmp; break; } } } head = newHead->next; delete newHead; return head; } }; 

## LRU Cache

LRU缓存算法实现，这个算法就是在缓存已满时，将最久未使用的元素移出缓存。

 class Node{ public: int key, val; Node *pre, *next; Node(int k, int v): key(k), val(v), pre(NULL), next(NULL) {} }; class LRUCache{ map mp; map::iterator iter; int used, cap; Node *head, *tail; public: LRUCache(int capacity) { mp.clear(); used = 0, cap = capacity; head = new Node(0, 0); tail = new Node(0, 0); head->next = tail, tail->pre = head; } ~LRUCache(){ for (Node *n = head, *nnext; n; n = nnext) { nnext = n->next; delete n; } } int get(int key) { if ((iter = mp.find(key)) != mp.end()) { movetoFirst(iter->second); return iter->second->val; }else return -1; } void set(int key, int value) { if ((iter = mp.find(key)) != mp.end()) { iter->second->val = value; movetoFirst(iter->second); } else { Node *node; if (used == cap) { mp.erase(tail->pre->key); node = tail->pre; node->key = key, node->val = value; } else { node = new Node(key, value); used++; } mp[node->key] = node; movetoFirst(node); } } void movetoFirst(Node *node) { if (node->pre && node->next) { node->pre->next = node->next; node->next->pre = node->pre; } node->pre = head, node->next = head->next; head->next->pre = node, head->next = node; } }; 

## Reorder List

 class Solution { public: void reorderList(ListNode *head) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. int len = 0, i; ListNode *p, *last, *pnext, *tmp; for (p = head; p; p = p->next, len++); if (len <= 2) return; for (p = head, i = 0; i < len/2; p = p->next, i++); tmp = p, p = p->next, tmp->next = NULL; for (pnext = p->next, p->next = NULL; pnext;) { tmp = pnext; pnext = pnext->next; tmp->next = p; p = tmp; } last = p; for (p = head; p; p = tmp){ tmp = p->next; if (last) { p->next = last; last = last->next; p->next->next = tmp; } } } }; 

## Linked List Cycle

 class Solution { public: bool hasCycle(ListNode *head) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. ListNode *h1, *h2; for(h1 = h2 = head; h1 && h2;){ if (!h1->next || !h2->next || !h2->next->next) return false; h1 = h1->next; h2 = h2->next->next; if (h1 == h2 && h1) return true; } return false; } }; 

## Linked List Cycle II

 class Solution { public: ListNode *detectCycle(ListNode *head) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. ListNode *h1, *h2; for(h1 = h2 = head; h1 && h2;){ if (!h1->next || !h2->next || !h2->next->next) return false; h1 = h1->next; h2 = h2->next->next; if (h1 == h2 && h1) break; } if (!h1 || !h2) return NULL; h2 = head; while(h1 != h2) h1 = h1->next, h2 = h2->next; return h1; } }; 

## Word Break

DP，假设原串为s[1..n]，用d[i][j]表示s[i..j]是否可以有字典里的词组成，有DP方程 d[i][j] = true if (d[i][k]&&d[k+1][j] == true,i<=k<j)

 class Solution { public: bool wordBreak(string s, unordered_set &dict) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. vector > dp(s.length(), vector(s.length(), -1)); return dpit(dp, s, dict, 0, s.length() - 1); } bool dpit(vector > &dp, string &s, unordered_set &dict, int l, int r) { if (dp[l][r] != -1) return dp[l][r]; if (dict.find(s.substr(l, r - l + 1)) != dict.end()) return dp[l][r] = 1; for (int i = l; i < r; i++) { if (dpit(dp, s, dict, l, i) && dpit(dp, s, dict, i+1, r)) return dp[l][r] = 1; } return dp[l][r] = 0; } }; 

## Word Break II

PS：其实这一题可以构造出有2^(n-1)个解的数据，令s="a..a",dict={"a","aa","aaa",...,"a..a"}即可，这组数据无法在多项式时间内求解。

 class Solution { public: vector strRes; vector seqRes; vector setVec; vector wordBreak(string s, unordered_set &dict) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. unordered_set::iterator iter; vector > dp(s.length(), vector(s.length(), -1)); setVec.clear(); strRes.clear(); seqRes.clear(); for (string ds: dict) { setVec.push_back(ds); int pos = 0; while ((pos = s.find(ds, pos)) != string::npos) dp[pos][pos+ds.length()-1] = setVec.size(), pos ++; } dfs_result(dp, 0, s.length()-1); return strRes; } void dfs_result(vector > &dp, int s, int e) { if (s > e) { string s; for (int i = 0; i < seqRes.size(); i++) { s.append(setVec[seqRes[i] - 1]); if (i != seqRes.size() - 1) s.append(" "); } strRes.push_back(s); return; } if (dpit(dp, s, e) == 0) return; for (int i = s; i <= e; i++) { if (dp[s][i] > 0) { seqRes.push_back(dp[s][i]); dfs_result(dp, i + 1, e); seqRes.pop_back(); } } } int dpit(vector > &dp, int l, int r) { if (dp[l][r] != -1) return dp[l][r]; for (int i = l; i < r; i++) { if (dpit(dp, l, i) && dpit(dp, i+1, r)) return dp[l][r] = -2; } return dp[l][r] = 0; } }; 

## Copy List with Random Pointer

 class Solution { public: RandomListNode *copyRandomList(RandomListNode *head) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. if (!head) return NULL; RandomListNode *bhead, *anow, *bnow; for (anow = head; anow; anow = anow->next) { bnow = new RandomListNode(anow->label); bnow->next = anow->random; anow->random = bnow; } for(anow = head; anow; anow = anow->next) { bnow = anow->random; bnow->random = bnow->next?bnow->next->random:NULL; } bhead = head->random; for(anow = head; anow; anow = anow->next) { bnow = anow->random; anow->random = bnow->next; bnow->next = anow->next?anow->next->random:NULL; } return bhead; } }; 

## Single Number

 class Solution { public: int singleNumber(int A[], int n) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. int x = 0; while (--n >= 0) x ^= A[n]; return x; } }; 

## Single Number II

 class Solution { public: int singleNumber(int A[], int n) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. int x[3] = {0}; while (--n >= 0) { x[2] = x[1] & A[n]; x[1] |= x[0] & A[n]; x[0] |= A[n]; x[0] &= ~x[2], x[1] &= ~x[2]; } return x[0]; } }; 

## Candy

 class Solution { public: int candy(vector &ratings) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. int size = ratings.size(), ans = 0; if (size == 1) return 1; vector candies(size, 1); for (int i = 0; i < size; i++) { if (i == 0 && ratings[i] <= ratings[i + 1] || i == size - 1 && ratings[i] <= ratings[i - 1] || ratings[i] <= ratings[i + 1] && ratings[i] <= ratings[i - 1]) { for (int j = i - 1; j >=0 && ratings[j] > ratings[j + 1]; j--) candies[j] = max(candies[j], candies[j + 1] + 1); for (int j = i + 1; j < size && ratings[j] > ratings[j - 1]; j++) candies[j] = max(candies[j], candies[j - 1] + 1); } } //for (int i = 0; i < size; i++) printf("%d ", candies[i]); for (int i = 0; i < size; i++) ans += candies[i]; return ans; } }; 

## Gas Station

n个点围成一个圈，1->2..->n->1，到达i点可以加油gas[i]，i到i+1点需要花费cost[i]，求是否能找到一个起点保证有油转一整圈。

 class Solution { public: int canCompleteCircuit(vector &gas, vector &cost) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. int remain = 0, totgas = 0, startp = 0; for (int i = 0; i < gas.size(); i++) { remain += gas[i] - cost[i]; totgas += gas[i] - cost[i]; if (remain < 0) remain = 0, startp = i + 1; } startp %= gas.size(); return totgas >= 0 ? startp : -1; } }; 

## Clone Graph

clone一张图

 #define UNODE UndirectedGraphNode class Solution { public: map::iterator iter; UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. map nodeMap; if (node == NULL) return node; UNODE *nnode = new UNODE(node->label); nodeMap[node] = nnode; dfsGraph(nodeMap, node, nnode); return nnode; } void dfsGraph(map &nodeMap, UNODE *node, UNODE *nnode) { for (int i = 0; i < node->neighbors.size(); i++) { UNODE *now = node->neighbors[i]; if ((iter = nodeMap.find(now)) != nodeMap.end()) { nnode->neighbors.push_back(iter->second); } else { UNODE *nnow = new UNODE(now->label); nodeMap[now] = nnow; nnode->neighbors.push_back(nnow); dfsGraph(nodeMap, now, nnow); } } } }; 

## Palindrome Partitioning

 class Solution { public: vector > vecResult; vector > partition(string s) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. vecResult.clear(); vector vecStr; vector > parStart(s.length()); for (int i = 0; i < s.length(); i++) { for (int j = i; 2*i-j < s.length() && j >= 0 && s[j] == s[2*i-j]; j--) parStart[j].push_back(s.substr(j, 2*i-2*j+1)); for (int j = i; 2*i-j+1 < s.length() && j >= 0 && s[j] == s[2*i-j+1]; j--) parStart[j].push_back(s.substr(j, 2*i-2*j+2)); } dfs(parStart, vecStr, 0, s.length()); return vecResult; } void dfs(vector > &parStart, vector &vecStr, int sp, int ep) { if (sp == ep) { vecResult.push_back(vecStr); } else { for (int i = 0; i < parStart[sp].size(); i++) { vecStr.push_back(parStart[sp][i]); dfs(parStart, vecStr, sp + parStart[sp][i].length(), ep); vecStr.pop_back(); } } } }; 

## Palindrome Partitioning II

DP，用minC[i]表示s[1..i]需要几刀，显然minC[i]最大是i。DP方程
minC[i] = min(i, minC[j]+1)(1<=j<=i&&isPar[j+1][i])isPar[i][j]在DP的过程中可以顺便求出来，isPar[i][j] = true if (isPar[i+1][j-1]&&s[i]==s[j])

 class Solution { public: int minCut(string s) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. vector > isPar(s.length(), vector(s.length(), false)); vector minC(s.length(), 0); for (int i = 0; i < s.length(); i++) { minC[i] = i + 1; for (int j = 0; j <= i; j++) { if ((j + 2 > i || isPar[j+1][i-1]) && s[j] == s[i]) { isPar[j][i] = true; minC[i] = min(minC[i], j ? (minC[j-1] + 1) : 1); } } } return minC[s.length() - 1] - 1; } }; 

## Surrounded Regions

 class Solution { public: void solve(vector > &board) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. queue > q; int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}; if (board.size() == 0) return ; int lenh = board.size(), lenw = board[0].size(); for (int i = 0; i < lenh; i++) for (int j = 0; j < lenw; j++) if ((i == 0 || j == 0 || i == lenh - 1 || j == lenw - 1) && board[i][j] == 'O') { board[i][j] = 'Y'; q.push(make_pair(i, j)); } while (!q.empty()) { pair p = q.front(); q.pop(); for (int i = 0; i < 4; i++) { int ni = p.first + dx[i], nj = p.second + dy[i]; if (ni >= 0 && ni < lenh && nj >= 0 && nj < lenw && board[ni][nj] == 'O') { board[ni][nj] = 'Y'; q.push(make_pair(ni, nj)); } } } for (int i = 0; i < lenh; i++) for (int j = 0; j < lenw; j++) board[i][j] = (board[i][j] == 'Y' ? 'O' : 'X'); } }; 

## Sum Root to Leaf Numbers

 class Solution { public: int sumNumbers(TreeNode *root) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. int ans = 0; if (!root) return 0; dfsTree(root, root->val, ans); return ans; } void dfsTree(TreeNode *fa, int now, int &ans) { if (!(fa->left || fa->right)) ans += now; else { if (fa->left) dfsTree(fa->left, now*10+fa->left->val, ans); if (fa->right)dfsTree(fa->right,now*10+fa->right->val,ans); } } }; 

## Longest Consecutive Sequence

 class Solution { public: int longestConsecutive(vector &num) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. unordered_set uset; for (int i = 0; i < num.size(); i++) uset.insert(num[i]); int ans = 0, tmpAns = 0, now, nowb; while (!uset.empty()) { now = *uset.begin(), tmpAns = 1; uset.erase(now); for (nowb = now + 1; uset.count(nowb); uset.erase(nowb), tmpAns++, nowb++); for (nowb = now - 1; uset.count(nowb); uset.erase(nowb), tmpAns++, nowb--); if (tmpAns > ans) ans = tmpAns; } return ans; } }; 

## Word Ladder

 class Solution { public: int ladderLength(string start, string end, unordered_set &dict) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. queue q; unordered_map vis; int len = start.length(); q.push(start); vis[start] = 1; while (!q.empty()) { string s = q.front(); q.pop(); int step = vis[s]; for (int i = 0; i < len; i++) { string news = s; for (char c = 'a'; c <= 'z'; c++) { if (s[i] == c) continue; news[i] = c; if (news == end) return step + 1; if (dict.count(news) && !vis[news]) { vis[news] = step + 1; q.push(news); } } } } return 0; } }; 

## Word Ladder II

 class Solution { public: vector > ans; vector > findLadders(string start, string end, unordered_set &dict) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. dict.insert(end); int dsize = dict.size(), len = start.length(); unordered_map > next; unordered_map vis; queue q; vector path; ans.clear(); q.push(start); vis[start] = 0; while (!q.empty()) { string s = q.front(); q.pop(); if (s == end) break; int step = vis[s]; vector snext; for (int i = 0; i < len; i++) { string news = s; for (char c = 'a'; c <= 'z'; c++) { news[i] = c; if (c == s[i] || dict.find(news) == dict.end()) continue; auto it = vis.find(news); if (it == vis.end()) { q.push(news); vis[news] = step + 1; } snext.push_back(news); } } next[s] = snext; } path.push_back(start); dfspath(path, next, vis, start, end); return ans; } void dfspath(vector &path, unordered_map > &next, unordered_map &vis, string now, string end){ //cout << now << endl; if (now == end) ans.push_back(path); else { auto vec = next[now]; int visn = vis[now]; for (int i = 0; i < vec.size(); i++) { if (vis[vec[i]] != visn + 1) continue; path.push_back(vec[i]); dfspath(path, next, vis, vec[i], end); path.pop_back(); } } } }; 

## Valid Palindrome

 class Solution { public: bool isPalindrome(string s) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. int len = s.length(), l = 0, r = len - 1; while (l < r) { if (!isalnum(s[l])) l++; else if (!isalnum(s[r])) r--; else if (tolower(s[l]) == tolower(s[r])) l++, r--; else return false; } return true; } }; 

## Binary Tree Maximum Path Sum

 class Solution { public: int maxPathSum(TreeNode *root) { int ans = root->val; dfsTree(root, ans); return ans; } int dfsTree(TreeNode *fa, int &ans) { if (fa == NULL) return 0; int tmp = fa->val, lmax, rmax; if ((lmax = dfsTree(fa->left, ans)) > 0) tmp += lmax; if ((rmax = dfsTree(fa->right, ans)) > 0) tmp += rmax; ans = max(ans, tmp); return max(fa->val, max(fa->val+lmax, fa->val+rmax)); } }; 

## Best Time to Buy and Sell Stock

 class Solution { public: int maxProfit(vector &prices) { if (prices.size() == 0) return 0; int ans = 0, minv = prices[0]; for (int i = 1; i < prices.size(); i++) { ans = max(ans, prices[i] - minv); minv = min(minv, prices[i]); } return ans; } }; 

## Best Time to Buy and Sell Stock II

 class Solution { public: int maxProfit(vector &prices) { if (prices.size() == 0) return 0; int minv = prices[0], ans = 0; for (int i = 1; i < prices.size(); i++) { if (prices[i] > minv) ans += prices[i] - minv, minv = prices[i]; minv = min(minv, prices[i]); } return ans; } }; 

## Best Time to Buy and Sell Stock III

Stock1里，我们从前向后依次能求出当天卖出一颗石头的最大收益pre[i]，同理，我们也能从后向前依次求出当天买入一颗石头能得到的最大收益last[i]，再用maxlast[i]=max(last[k](i<=k<=n))表示从i~n天内买入一颗石头的最大收益，所以，ans=max(pre[i]+maxlast[i])(1<=i<=n)，就是说再第i天完成第一笔交易，并在第i天之后的某一天完成第二笔交易。

 class Solution { public: int maxProfit(vector &prices) { int size = prices.size(), ans = 0; if (size <= 1) return 0; vector p(size, 0); int minv = prices[0], maxv; for (int i = 1; i < size; i++) p[i] = prices[i] - minv, minv = min(prices[i], minv); ans = max(ans, p[size-1]); minv = prices[size-1], maxv = 0; for (int i = size-2; i >= 0; i--) maxv = max(maxv, minv - prices[i]), ans = max(ans, p[i] + maxv), minv = max(prices[i], minv); return ans; } }; 

## Triangle

 class Solution { public: int minimumTotal(vector > &triangle) { int size = triangle.size(); vector ans(triangle[size-1]); for (int i = size-2; i >= 0; i--) { for (int j = 0; j <= i; j++) ans[j] = triangle[i][j] + min(ans[j], ans[j+1]); } return ans[0]; } }; 

## Pascal’s Triangle

 class Solution { public: vector > ans; vector > generate(int numRows) { ans.clear(); ans.resize(numRows); for (int i = 0; i < numRows ;i++) { ans[i].resize(i+1); ans[i][0] = ans[i][i] = 1; for (int j = 1; j < i; j++) ans[i][j] = ans[i-1][j-1] + ans[i-1][j]; } return ans; } }; 

## Pascal’s Triangle II

C(N, i)=C(N, i-1)*(N-i+1)/i，递推即可。

 class Solution { public: vector ans; vector getRow(int rowIndex) { ans.resize(rowIndex + 1); ans[0] = 1; for (int i = 1; i <= rowIndex; i++) ans[i] = (long long)ans[i-1] * (rowIndex - i + 1) / i; return ans; } }; 

## Populating Next Right Pointers in Each Node

  1 / \ 2 --> 3 / \ / \ 4 5->6 7 class Solution { public: void connect(TreeLinkNode *root) { if (root == NULL) return; connect(root->left); connect(root->right); for (TreeLinkNode *nl = root->left, *nr = root->right; nl; nl->next = nr, nl = nl->right, nr = nr->left); } }; 

## Populating Next Right Pointers in Each Node II

 class Solution { public: TreeLinkNode *next(TreeLinkNode* p) { while (p && !p->left && !p->right) p = p->next; return p; } void connect(TreeLinkNode *root) { TreeLinkNode *nextHead, *preHead, *nextPreHead; for (TreeLinkNode *head = root; head; preHead = head, head = nextHead) { head = next(head); if (head != NULL) nextHead = head->left ? head->left : head->right; else break; for (preHead = NULL; head; preHead = head, head = next(head->next)) { if (head->left && head->right) head->left->next = head->right; if (preHead) { nextPreHead = head->left ? head->left : head->right; if (preHead->right == NULL) preHead->left->next = nextPreHead; else preHead->right->next = nextPreHead; } } } } }; 

## Distinct Subsequences

 class Solution { public: int numDistinct(string S, string T) { int lent = T.length(), lens = S.length(); vector > dp(lent+1, vector(lens+1, 0)); for (int i = 0; i <= lens; i++) dp[0][i] = 1; for (int i = 1; i <= lent; i++) { for (int j = 1; j <= lens; j++) { dp[i][j] = dp[i][j-1]; if (S[j-1] == T[i-1]) dp[i][j] += dp[i-1][j-1]; } } return dp[lent][lens]; } }; 

## Flatten Binary Tree to Linked List

 class Solution { public: void flatten(TreeNode *root) { now = root; dfs(root); } TreeNode *now; void dfs(TreeNode *root) { if (!root) return; TreeNode *right = root->right; if (root->left) { now = now->right = root->left; dfs(root->left); } root->left = NULL; if (right) { now = now->right = right; dfs(right); } } }; 

## Path Sum

DFS，到叶子判断累加值是否等于Sum

 class Solution { public: bool hasPathSum(TreeNode *root, int sum) { return root && dfs(root, 0, sum) ; } bool dfs(TreeNode *node, int now, int sum) { if (node ->left == NULL && node->right == NULL) return now + node->val == sum ; if (node->left && dfs(node->left, now + node->val, sum)) return true; if (node->right && dfs(node->right, now + node->val, sum)) return true; return false; } }; 

## Path Sum II

 class Solution { public: vector > ans; vector path; vector > pathSum(TreeNode *root, int sum) { ans.clear(); path.clear(); if (root) dfs(root, 0, sum) ; return ans; } void dfs(TreeNode *node, int now, int sum) { if (node ->left == NULL && node->right == NULL) { if (now + node->val == sum) { path.push_back(node->val); ans.push_back(path); path.pop_back(); } return; } if (node->left) { path.push_back(node->val); dfs(node->left, now + node->val, sum); path.pop_back(); } if (node->right) { path.push_back(node->val); dfs(node->right, now + node->val, sum); path.pop_back(); } } }; 

## Minimum Depth of Binary Tree

 class Solution { public: int minDepth(TreeNode *root) { if (!root) return 0; if (!root->left && !root->right) return 1; int ltval = -1, rtval = -1; if (root->left) ltval = minDepth(root->left); if (root->right) rtval = minDepth(root->right); return 1+ (ltval == -1 ? rtval : (rtval == -1 ? ltval : min(ltval, rtval))); } }; 

## Balanced Binary Tree

DFS返回该节点的最大深度，过程中比较左孩子和右孩子为根的子树的最大深度即可。

 class Solution { public: bool isBalanced(TreeNode *root) { bool ans = true; dfs(root, ans); return ans; } int dfs(TreeNode *root, bool &ans) { if (!root) return 0; int ltval = dfs(root->left, ans); int rtval = dfs(root->right, ans); if (!(ltval - rtval <= 1 && ltval - rtval >= -1)) ans = false; return max(ltval, rtval) + 1; } }; 

## Convert Sorted List to Binary Search Tree

 class Solution { public: TreeNode *sortedListToBST(ListNode *head) { int len = 0; for (ListNode *p = head; p; p = p->next, len++); return dfs(head, len); } TreeNode *dfs(ListNode *head, int len) { if (len <= 0) return NULL; int mid = (len + 1) >> 1; ListNode *rhead = head; for (int i = 1; i < mid; rhead=rhead->next, i++); TreeNode *node = new TreeNode(rhead->val); node->left = dfs(head, mid - 1); node->right = dfs(rhead->next, len - mid); return node; } }; 

## Convert Sorted Array to Binary Search Tree

 class Solution { public: TreeNode *sortedArrayToBST(vector &num) { return dfs(0, num.size()-1, num); } TreeNode *dfs(int left, int right, vector &num) { if (left > right) return NULL; int mid = (left + right) >> 1; TreeNode *node = new TreeNode(num[mid]); node->left = dfs(left, mid-1, num); node->right = dfs(mid+1, right, num); return node; } }; 

## Binary Tree Level Order Traversal II

BFS当然可以，我是直接DFS然后每一层一个vector存该层的数。。

 class Solution { public: vector > vec; vector > levelOrderBottom(TreeNode *root) { vec.clear(); dfs(root, 0, vec); reverse(vec.begin(), vec.end()); return vec; } void dfs(TreeNode *root, int dep, vector > &vec) { if (root == NULL) return; if (vec.size() <= dep) vec.push_back(vector()); vec[dep].push_back(root->val); dfs(root->left, dep+1, vec); dfs(root->right, dep+1, vec); } }; 

## Construct Binary Tree from Inorder and Postorder Traversal

 class Solution { public: TreeNode *buildTree(vector &inorder, vector &postorder) { return build(0, inorder.size()-1, 0, postorder.size()-1, inorder, postorder); } TreeNode *build(int il, int ir, int pl, int pr, vector &ivec, vector &pvec) { if (pl > pr) return NULL; TreeNode *node = new TreeNode(pvec[pr]); int ip; for (ip = il; ip <= ir; ip++) { if (ivec[ip] == pvec[pr]) break; } if (il<=ip-1) node->left = build(il, ip-1, pl, pl+ip-il-1, ivec, pvec); if (ip+1<=ir) node->right = build(ip+1, ir, pl+ip-il, pr-1, ivec, pvec); return node; } }; 

## Construct Binary Tree from Preorder and Inorder Traversal

 class Solution { public: TreeNode *buildTree(vector &preorder, vector &inorder) { return build(0, preorder.size()-1, 0, inorder.size()-1, preorder, inorder); } TreeNode *build(int pl, int pr, int il, int ir, vector &pvec, vector &ivec) { if (pl > pr) return NULL; TreeNode *node = new TreeNode(pvec[pl]); int ip; for (ip = il; ip <= ir; ip++) { if (ivec[ip] == pvec[pl]) break; } if (il<=ip-1) node->left = build(pl+1, pl+ip-il, il, ip-1, pvec, ivec); if (ip+1<=ir) node->right = build(pl+ip-il+1, pr, ip+1, ir, pvec, ivec); return node; } }; 

## Maximum Depth of Binary Tree

 class Solution { public: int maxDepth(TreeNode *root) { if (root == NULL) return 0; return 1 + max(maxDepth(root->left), maxDepth(root->right)); } }; 

## Binary Tree Zigzag Level Order Traversal

 class Solution { public: vector > vec; vector > zigzagLevelOrder(TreeNode *root) { vec.clear(); dfs(root, 0, vec); for (int i = 1; i < vec.size(); i += 2) reverse(vec[i].begin(), vec[i].end()); return vec; } void dfs(TreeNode *root, int dep, vector > &vec) { if (root == NULL) return; if (vec.size() <= dep) vec.push_back(vector()); vec[dep].push_back(root->val); dfs(root->left, dep+1, vec); dfs(root->right, dep+1, vec); } }; 

## Binary Tree Level Order Traversal

 class Solution { public: vector > vec; vector > levelOrder(TreeNode *root) { vec.clear(); dfs(root, 0, vec); return vec; } void dfs(TreeNode *root, int dep, vector > &vec) { if (root == NULL) return; if (vec.size() <= dep) vec.push_back(vector()); vec[dep].push_back(root->val); dfs(root->left, dep+1, vec); dfs(root->right, dep+1, vec); } }; 

## Symmetric Tree

 class Solution { public: bool isSymmetric(TreeNode *root) { vector now, next; if (root) now.push_back(root); bool flag = true; while(now.size() > 0) { next.clear(); int size = now.size(); for (int l = 0, r = size - 1; l <= r; l++, r--) { if (!nodeeql(now[l]->left, now[r]->right) || !nodeeql(now[l]->right, now[r]->left)) flag = false; } if (!flag) break; for (int i = 0; i < size; i++) { if (now[i]->left) next.push_back(now[i]->left); if (now[i]->right)next.push_back(now[i]->right); } now = next; } return flag; } bool nodeeql(TreeNode *l1, TreeNode *l2) { if ((!l1 && l2) || (l1 && !l2)) return false; return !l1 && !l2 || l1->val == l2->val; } }; 

## Same Tree

 class Solution { public: bool isSameTree(TreeNode *p, TreeNode *q) { if (!p || !q) return !p && !q; if (p->val != q->val) return false; return isSameTree(p->left, q->left) && isSameTree(p->right, q->right); } }; 

## Unique Binary Search Trees

 class Solution { public: int numTrees(int n) { int *dp = new int[n+1]; for (int i = 0; i <= n; i++) dp[i] = (i <= 1 ? 1 : -1); return dpit(dp, n); } int dpit(int dp[], int n) { if (dp[n] != -1) return dp[n]; dp[n] = 0; for (int i = 0; i < n; i++) dp[n] += dpit(dp, i) * dpit(dp, n-i-1); return dp[n]; } }; 

## Binary Tree Inorder Traversal

 class Solution { public: vector vec; vector inorderTraversal(TreeNode *root) { stack st; vec.clear(); TreeNode *T = root; while (T || !st.empty()) { if (T) { st.push(T); T = T->left; } else { T = st.top(); st.pop(); vec.push_back(T->val); T = T->right; } } return vec; } }; 

## Restore IP Addresses

DFS，注意一些细节

 class Solution { public: vector ans; vector restoreIpAddresses(string s) { ans.clear(); dfs(ans, s, "", 0, 0); return ans; } void dfs(vector &ans, string s, string str, int pos, int dep) { if (dep >= 4) { if (dep == 4 && pos == s.length()) ans.push_back(str); } else { for (int i = pos; i < s.length(); i++) { string sub = s.substr(pos, i-pos+1); if (sub.length() <= 3 && stoi(sub) >= 0 && stoi(sub) <= 255 && to_string(stoi(sub)) == sub) { string common = (dep == 3 ? "": "."); dfs(ans, s, str+sub+common, i+1, dep+1); } } } } }; 

## Simplify Path

/为分界，以栈来保存每一段，//.都没有意义，不用管，/..需要退栈，最后记得栈为空要加上根目录/

 class Solution { public: string simplifyPath(string path) { //cout << "here" << endl; vector resVec; string ans = ""; int pos = 0, rpos; while ((rpos = path.find("/", pos+1)) != string::npos) { resVec.push_back(path.substr(pos, rpos-pos)); cal(resVec); pos = rpos; } resVec.push_back(path.substr(pos)); cal(resVec); if (resVec.size() == 0) resVec.push_back("/"); for (auto &s: resVec) ans.append(s); return ans; } void cal(vector &vec) { string s = vec[vec.size() - 1]; if (s == "/" || s == "/.") { vec.pop_back(); } else if (s == "/..") { vec.pop_back(); if (vec.size()) vec.pop_back(); } } }; 

02-16 365
03-03 922
12-16 1686
04-23 252
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客