voidmerge(vector<int>& nums1, int m, vector<int>& nums2, int n){ vector<int> b; b.clear(); int i = 0, j = 0; for( ; i < m || j < n;) { if((j == n) || ( i < m && nums1[i] <= nums2[j])) { b.push_back(nums1[i++]); } else b.push_back(nums2[j++]); }
for(i = 0; i < n+m; i++) nums1[i] = b[i]; }
26.删除有序数组中的重复项
按照条件筛选元素,条件为不与前驱相同的元素。
用覆盖的思路处理即可。
1 2 3 4 5 6 7 8 9 10 11 12
classSolution { public: intremoveDuplicates(vector<int>& nums){ int k = 1; for(int i = 1; i < nums.size(); i++) { if(nums[i] != nums[i-1]) { nums[k++] = nums[i]; } } return k; } };
80.删除有序数组中的重复项 II
在上一题基础上加一个前驱的前驱即可。
1 2 3 4 5 6 7 8 9 10 11 12 13
classSolution { public: intremoveDuplicates(vector<int>& nums){ if(nums.size() <= 1) return nums.size(); int k = 2; for(int i = 2; i < nums.size(); i++) { if(nums[i] != nums[k-1] || nums[i] != nums[k-2]) { nums[k++] = nums[i]; } } return k; } };
169.多数元素
求众数方法。计数,相同则加,不同则减,减到0更换预选数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution { public: intmajorityElement(vector<int>& nums){ int ans = nums[0]; int k = 1; for(int i = 1; i < nums.size(); i++) { if(ans == nums[i]) k++; else k--; if(!k) { k = 1; ans = nums[i]; } } return ans; } };
189.轮转数组
前后调换,三次逆转即可。
121.买卖股票的最佳时机
维护一个前缀最小值,动态更新答案。
122.买卖股票的最佳时机 II
贪心。记录折现转折点,在最低点买入最高点卖出即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution { public: intmaxProfit(vector<int>& prices){ int n = prices.size(); if(n <= 1) return0; int l = prices[0], r = prices[0], ans = 0; for(int i = 1; i < n; i++) { if(prices[i] >= prices[i-1]) { r = prices[i]; } else { ans += r-l; l = r = prices[i]; } } ans += r-l; return ans; } };
intindexOfVal(int val){ int H = hash(val); if(vec[H].empty() || value.empty()) return-1; for(int i = 0; i < vec[H].size(); i++) { int index = vec[H][i]; if(index == -1) continue; if(value[index] == val) return index; } return-1; }
boolinsert(int val){ if(indexOfVal(val) != -1) returnfalse; value.push_back(val); int index = value.size()-1; int H = hash(val); vec[H].push_back(index); returntrue; }
boolremove(int val){ int index = indexOfVal(val); if(index == -1) returnfalse; int n = value.size(); int last = value[n-1];
int H = hash(last); for(int i = 0; i < vec[H].size(); i++) { if(vec[H][i] == n-1) vec[H][i] = index; }
H = hash(val); for(int i = 0; i < vec[H].size(); i++) { if(vec[H][i] == index) vec[H][i] = -1; }
classSolution { public: int ch[40005][26]; int size[40005]; int cnt[40005]; vector<int> vec[40006]; int num = 0; intnewNode(){ return ++num; } voidinsert(string s){ int u = 0; int n = s.size(); for(int i = 0; i < n; i++) { int c = s[i]-'a'; if(!ch[u][c]) { int v = newNode(); ch[u][c] = v; size[u]++; vec[u].push_back(v); } cnt[u]++; u = ch[u][c]; } }
string longestCommonPrefix(vector<string>& strs){ memset(ch, 0, sizeof(ch)); int n = strs.size(); for(int i = 0; i < n; i++) insert(strs[i]);
int u = 0, len = 0; while(size[u] == 1 && cnt[u] == n) { u = vec[u][0]; len++; }
string ans = ""; for(int i = 0; i < len; i++) ans += strs[0][i];
return ans; } };
151.反转字符串中的单词
使用python即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution(object): defreverseWords(self, s): """ :type s: str :rtype: str """ lst = s.strip().split(' ') n = len(lst) ans = '' for i inreversed(lst): if(i.strip() == ''): continue ans = ans + ' ' + i.strip() return ans.strip()
classSolution { public: structNode{ int val, idx; Node(int val, int idx):val(val), idx(idx){} booloperator < (const Node &b) const{ return val > b.val; } }; intabs(int x){ return x > 0 ? x : x*-1; } intmaxArea(vector<int>& height){ int n = height.size(); vector<Node> nodes; for(int i = 0; i < n; i++) { nodes.push_back(Node(height[i], i)); } sort(nodes.begin(), nodes.end()); vector<int> preMax, preMin; preMax.push_back(nodes[0].idx); preMin.push_back(nodes[0].idx); for(int i = 1; i < n; i++) { preMax.push_back(max(preMax[i-1], nodes[i].idx)); preMin.push_back(min(preMin[i-1], nodes[i].idx)); }
int ans = 0; for(int i = n-1; i >= 1; i--) { int idx = nodes[i].idx; int dst = max(abs(idx-preMax[i-1]), abs(idx-preMin[i-1])); ans = max(ans, nodes[i].val * dst); } return ans; } };
classSolution { public: int b[5005]; intcheck(string s, vector<string>& words){ int n = words.size(); for (int i = 0; i < n; i++) { if (!b[i] && s == words[i]) { b[i] = 1; return i; } } for (int i = 0; i < n; i++) { if (s == words[i]) return-2; } return-1; }
vector<int> findSubstring(string s, vector<string>& words){ int n = s.size(); vector<int> ans; if (n == 0 || words.size() == 0) return ans; int m = words[0].size(); queue<int> que; for (int i = 0; i < m; i++) { int p = i, l = i; memset(b, 0, sizeof(b)); while(que.size()) que.pop(); while (p < n) { string ss = s.substr(p, m); int idx = check(ss, words); if (idx == -2) { int h = que.front(); que.pop(); b[h] = 0;
l += m; continue; } if(idx == -1) { memset(b, 0, sizeof(b)); while(!que.empty()) que.pop(); p += m; l = p;
continue; } b[idx] = 1; p += m; que.push(idx); if (que.size() == words.size()) { ans.push_back(l); b[que.front()] = 0; que.pop();
classSolution { public: int ch[305]; int b[305]; string minWindow(string s, string t){ int n = s.size(), m = t.size(); int K = 0; for (int i = 0; i < m; i++){ ch[t[i]]++; if(ch[t[i]] == 1) K++; }
int k = 0; int l = 0, r = 0; string ans; int minL = 0x3f3f3f3f; while (!ch[s[r]] && r < n) { l++; r++; } if (r == n) return ans;
classSolution { public: vector<string> split(string s){ string x; int n = s.size(); vector<string> vec; for (int i = 0; i < n; i++) { if (s[i] == '/') { if (!x.empty()) vec.push_back(x); x.clear(); continue; } x += s[i]; } if (!x.empty()) vec.push_back(x); return vec; }
string simplifyPath(string path){ vector<string> vec = split(path); stack<string> st; int n = vec.size(); for (int i = 0; i < n; i++) { if (vec[i] == ".." && !st.empty()) { st.pop(); continue; } if (vec[i] != ".." && vec[i] != ".") st.push(vec[i]); } vec.clear(); while (!st.empty()) { vec.push_back(st.top()); st.pop(); } string s; n = vec.size(); for (int i = n - 1; i >= 0; i--) { s += '/'; s += vec[i]; } if (s.empty()) s = "/"; return s; } };
classSolution { public: intsearch(vector<int>& nums, int target){ int n = nums.size(); int k = 0, p = 1; while(p) { if(k+p < n && nums[k+p] > nums[k]) { k += p; p <<= 1; } else p >>= 1; } k = n-1-k; int l = 0, r = n-1; int ans = -1; while(l <= r) { int mid = (l+r)/2; int idx = (mid-k+n)%n; if(nums[idx] == target) { ans = idx; break; } if(nums[idx] < target) l = mid+1; else r = mid-1; } return ans; } };