STL review
Q1
Leetcode Q1 sol(iterator version)
better understand iterator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { vector<int>::iterator v1, v2; for(v1 = nums.begin(); v1 != nums.end(); v1++) for(v2 = v1 + 1; v2 != nums.end(); v2++){ if(*v1 + *v2 == target){ int p1 = v1 - nums.begin(); int p2 = v2 - nums.begin(); return {p1, p2}; } } return {}; } };
|
Q3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| class Solution { public: int lengthOfLongestSubstring(string s) { string::iterator it = s.begin(), it2; int sum = 0, res = 0; unordered_map<char, bool> m; m.clear(); for(; it != s.end(); it++) { for(it2 = it; it2 != s.end(); it2++) { if(m.empty() || m.find(*it2) == m.end()) { char c = *it2; m[c] = true; sum ++; } else { m.clear(); break; } } if(sum > res) { res = sum; } sum = 0; } return res; } };
|
Optimization:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| class Solution { public: int lengthOfLongestSubstring(string s) { int start(0), end(0), length(0), result(0); int sSize = int(s.size()); unordered_map<char, int> hash; while (end < sSize) { char tmpChar = s[end]; if (hash.find(tmpChar) != hash.end() && hash[tmpChar] >= start) { start = hash[tmpChar] + 1; length = end - start; } hash[tmpChar] = end;
end++; length++; result = max(result, length); } return result; } };
|
Q4
Time complexity: O(m+n)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| class Solution { public: double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { vector<int> tmp; tmp.clear(); int vSize1 = nums1.size(), vSize2 = nums2.size(), i = 0, j = 0; while(i < vSize1 || j < vSize2) { if(i < vSize1 && j < vSize2 && nums1[i] <= nums2[j]) { tmp.push_back(nums1[i]); i++; } else if(i < vSize1 && j < vSize2 && nums1[i] > nums2[j] ){ tmp.push_back(nums2[j]); j++; } else if(i == vSize1) { tmp.push_back(nums2[j]); j++; } else if(j == vSize2){ tmp.push_back(nums1[i]); i++; } } double mid = (vSize1 + vSize2)/2; if((vSize1 + vSize2)%2 == 0) return ((double)(tmp[(int)mid] + (double)tmp[(int)mid - 1])/2); else return (double)(tmp[(vSize1 + vSize2 - 1)/2]); } };
|
Optimization (Binary Search) :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| class Solution { public: int getKthElement(const vector<int>& nums1, const vector<int>& nums2, int k) {
int m = nums1.size(); int n = nums2.size(); int index1 = 0, index2 = 0;
while (true) { if (index1 == m) { return nums2[index2 + k - 1]; } if (index2 == n) { return nums1[index1 + k - 1]; } if (k == 1) { return min(nums1[index1], nums2[index2]); }
int newIndex1 = min(index1 + k / 2 - 1, m - 1); int newIndex2 = min(index2 + k / 2 - 1, n - 1); int pivot1 = nums1[newIndex1]; int pivot2 = nums2[newIndex2]; if (pivot1 <= pivot2) { k -= newIndex1 - index1 + 1; index1 = newIndex1 + 1; } else { k -= newIndex2 - index2 + 1; index2 = newIndex2 + 1; } } }
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { int totalLength = nums1.size() + nums2.size(); if (totalLength % 2 == 1) { return getKthElement(nums1, nums2, (totalLength + 1) / 2); } else { return (getKthElement(nums1, nums2, totalLength / 2) + getKthElement(nums1, nums2, totalLength / 2 + 1)) / 2.0; } } };
|
Q6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| class Solution { public: string convert(string s, int numRows) {
if (numRows == 1) return s;
int sSize = s.size(); string res; res.clear(); for (int i = 0; i < numRows; i++) { if(i == 0 || i == numRows - 1){ for (int j = 0; i + j * (2 * numRows - 2) < sSize; j++) { res.push_back(s[i + j * (2 * numRows - 2)]); } } else { int out = i, cnt = 0; while (out < sSize) { res.push_back(s[out]); if (cnt % 2 == 0) { out = out + (2 * numRows - 2 * (i + 1)); } else { out = out + 2 * i; } cnt++; } } } return res; } };
|
Q27
双指针
1 2 3 4 5 6 7 8 9 10 11 12 13
| class Solution { public: int removeElement(vector<int>& nums, int val) { int slowIndex = 0; int vSize = nums.size(); for(int fastIndex = 0; fastIndex < vSize; fastIndex ++) { if(!(nums[fastIndex] == val)) { nums[slowIndex++] = nums[fastIndex]; } } return slowIndex; } };
|