leetcode 数组类型题
1 // ConsoleApplication1.cpp : 定义控制台应用程序的入口点。 2 // 3 4 #include "stdafx.h" 5 #include <Windows.h> 6 #include <iostream> 7 #include <vector> 8 #include <string> 9 #include <unordered_set> 10 #include <unordered_map> 11 #include <algorithm> 12 #include <numeric> 13 14 using namespace std; 15 void show(const vector<int>& nums){ 16 for (int i = 0; i < nums.size(); i++){ 17 cout << '\t' << nums[i]; 18 } 19 cout << endl; 20 } 21 22 int removeDuplicate(vector<int>& nums){ 23 if (nums.empty()) return 0; 24 int index = 1; 25 for (int i = 1; i < nums.size(); i++){ 26 if (nums[i - 1] != nums[i]) 27 nums[index++] = nums[i]; 28 } 29 return index; 30 } 31 32 vector<int> twoSum(vector<int>& nums, int target){ 33 unordered_map<int, int> mapping; 34 vector<int> res; 35 for (int i = 0; i < nums.size(); i++) 36 mapping[nums[i]] = i; 37 for (int i = 0; i < nums.size(); i++){ 38 int gap = target - nums[i]; 39 if (mapping.find(gap) != mapping.end()){ 40 res.push_back(i + 1); 41 res.push_back(mapping.find(gap)->second + 1); 42 break; 43 } 44 } 45 return res; 46 } 47 48 vector<vector<int>> threeSum(vector<int>& nums){ 49 vector<vector<int>> result; 50 const int target = 0; 51 if (nums.size() < 3) return result; 52 sort(nums.begin(), nums.end()); 53 auto last = nums.end(); 54 for (auto i = nums.begin(); i < last - 2; ++i){ 55 while (i>nums.begin() && *i == *(i - 1)) continue; 56 auto j = i + 1; 57 auto k = last - 1; 58 while (j < k){ 59 if (*i + *j + *k < target) { 60 ++j; 61 while (*j == *(j - 1) && j < k) ++j; 62 } 63 else if (*i + *j + *k > target){ 64 --k; 65 while (*k == *(k + 1) && j < k) --k; 66 } 67 else{ 68 result.push_back({ *i, *j, *k }); 69 ++j; 70 --k; 71 while (*j == *(j - 1) && *k == *(k + 1) && j < k) ++j; 72 } 73 } 74 } 75 return result; 76 } 77 78 int threeSumClose(vector<int>& nums, int target){ 79 int result = 0; 80 int min_gap = INT_MAX; 81 sort(nums.begin(), nums.end()); 82 auto last = nums.end(); 83 for (auto i = nums.begin(); i < last - 2; ++i){ 84 auto k = last - 1; 85 auto j = i + 1; 86 while (j < k){ 87 int sum = *i + *j + *k; 88 int gap = abs(sum - target); 89 if (gap < min_gap){ 90 min_gap = gap; 91 result = sum; 92 } 93 if (sum < target) ++j; 94 else --k; 95 } 96 } 97 return result; 98 } 99 100 void next_Permutation(vector<int>& nums){ 101 int i, j, k = nums.size(); 102 for (i = k - 2; i >= 0; i--){ 103 if (nums[i] < nums[i + 1]){ 104 for (j = k - 1; j > i; j--){ 105 if (nums[j] > nums[i]) 106 break; 107 } 108 swap(nums[i], nums[j]); 109 reverse(nums.begin() + i + 1, nums.end()); 110 return; 111 } 112 } 113 reverse(nums.begin(), nums.end()); 114 } 115 116 int trap_Water(vector<int>& hights){ 117 int result = 0; 118 int l = 0, r = hights.size() - 1; 119 while (l < r){ 120 int mn = min(hights[l], hights[r]); 121 if (mn == hights[l]){ 122 ++l; 123 while (mn > hights[l] && l < r) 124 result += mn - hights[l++]; 125 } 126 else{ 127 --r; 128 while (mn > hights[r] && l < r) 129 result += mn - hights[r--]; 130 } 131 } 132 return result; 133 } 134 135 int trap_WaterII(vector<int>& heights) { 136 vector<int> dpLeft(heights.size(), 0); 137 int maxLeft = 0; 138 for (int i = 0; i<heights.size(); ++i){ 139 dpLeft[i] = maxLeft; 140 maxLeft = max(heights[i], maxLeft); 141 } 142 show(dpLeft); 143 int maxRight = 0; 144 vector<int> dpRight(heights.size(), 0); 145 for (int j = heights.size() - 1; j >= 0; --j){ 146 dpRight[j] = maxRight; 147 maxRight = max(heights[j], maxRight); 148 } 149 show(dpRight); 150 int result = 0; 151 for (int k = 0; k<heights.size(); ++k){ 152 int minDiff = min(dpLeft[k], dpRight[k]); 153 if (minDiff > heights[k]) 154 result += minDiff - heights[k]; 155 } 156 return result; 157 } 158 159 void rotate(vector<vector<int>>& matrix) { 160 const int n = matrix.size(); //matrix 的行数 161 for (int i = 0; i < n;++i) 162 for (int j = 0; j < n - i; ++j) 163 swap(matrix[i][j], matrix[n - 1 - i][n - 1 - j]); // 副对角线反转 164 165 for (int i = 0; i < n / 2;++i) 166 for (int j = 0; j < n; ++j) 167 swap(matrix[i][j], matrix[n - 1 - i][j]);//水平中线反转 168 } 169 170 vector<int> plusOne(vector<int>& digits){ 171 int c = 1;//表示进位 carry 172 for (auto it = digits.rbegin(); it != digits.rend(); ++it){ 173 *it += c; 174 c = *it / 10; 175 *it = *it % 10; 176 } 177 if (c > 0) digits.insert(digits.begin(), 1); 178 return digits; 179 } 180 181 int climbStairs(int n){ //迭代 182 int pre = 0; 183 int cur = 1; 184 for (int i = 1; i <= n; i++){ 185 int tmp = cur; 186 cur += pre; 187 pre = tmp; 188 } 189 return cur; 190 } 191 192 vector<int> grayCode(int n){ 193 int size = 1 << n; //2^n 194 vector<int> result; 195 result.reserve(size); 196 for (int i = 0; i < size; ++i) 197 result.push_back(i ^ (i >> 1)); 198 return result; 199 } 200 201 void setZeroes(vector<vector<int>>& matrix){ 202 const int rows = matrix.size(); 203 const int columns = matrix[0].size(); 204 vector<bool> rowFlag(rows, false); 205 vector<bool> columnFlag(columns, false); 206 for (int i = 0; i < rows; ++i){ 207 for (int j = 0; j < columns; ++j){ 208 if (matrix[i][j] == 0) 209 rowFlag[i] = columnFlag[j] = true; 210 } 211 } 212 for (int i = 0; i < rows; ++i){ 213 if (rowFlag[i]){ 214 for (int j = 0; j < columns; ++j) 215 matrix[i][j] = 0; 216 } 217 } 218 for (int i = 0; i < columns; ++i){ 219 if (columnFlag[i]){ 220 for (int j = 0; j < rows; ++j) 221 matrix[j][i] = 0; 222 } 223 } 224 } 225 226 int canCompleteCircuit(vector<int>& gas, vector<int>& cost){ 227 int total = 0,sum = 0,start = 0; 228 for (int i = 0; i < gas.size(); ++i){ 229 total += gas[i] - cost[i]; 230 sum += gas[i] - cost[i]; 231 if (sum < 0){ 232 start = i + 1; 233 sum = 0; 234 } 235 } 236 return total >= 0 ? start : -1; 237 } 238 239 int candy(vector<int>& ratings){ 240 const int n = ratings.size(); 241 vector<int> nums(n, 1); 242 for (int i = 0; i < n-1; ++i){ 243 if (ratings[i] < ratings[i+1]) 244 nums[i+1] = nums[i] + 1; 245 } 246 for (int j = n - 1; j>0; --j){ 247 if (ratings[j] < ratings[j - 1]) 248 nums[j - 1] = max(nums[j - 1], nums[j] + 1); 249 } 250 return accumulate(nums.begin(), nums.end(),0); 251 } 252 253 int candyII(vector<int>& ratings) { 254 if (ratings.size() == 0) return 0; 255 vector<int> minLeft(ratings.size(), 1); 256 for (int i = 1; i<ratings.size(); ++i){ 257 if (ratings[i]>ratings[i - 1]) 258 minLeft[i] = minLeft[i - 1] + 1; 259 } 260 vector<int> minRight(ratings.size(), 1); 261 for (int j = ratings.size() - 2; j >= 0; --j){ 262 if (ratings[j]>ratings[j + 1]) //如果左边的等级高,而且左边的糖果又少的话 263 minRight[j] = max(minLeft[j], (minRight[j + 1] + 1)); 264 } 265 int result = 0; 266 for (int k = 0; k<ratings.size(); ++k) 267 result += max(minLeft[k], minRight[k]); //取从左边和右边都最小的值中的最大值,这样就满足所有条件了。 268 return result; 269 } 270 271 int singleNumber(vector<int>& nums){ 272 int x = 0; 273 for (int i = 0; i < nums.size(); ++i) 274 x ^= nums[i]; 275 return x; 276 } 277 278 int singleNumberII(vector<int>& nums) { 279 int result = 0; 280 for (int i = 0; i < 32; ++i){ 281 int sum = 0; 282 for (int j = 0; j < nums.size(); ++j){ 283 sum += (nums[j] >> i) & 1; 284 } 285 result += (sum % 3) << i;// result |= (sum % 3) << i; 286 } 287 return result; 288 } 289 290 291 292 int main() 293 { 294 int myints[] = { 0,1,1,0,2,0,1}; 295 vector<int> myvec(myints, myints + sizeof(myints) / sizeof(int)); 296 cout << "result:" << singleNumberII(myvec) << endl; 297 system("pause"); 298 return 0; 299 }
题目参考教材:https://github.com/soulmachine/leetcode(leetcode-cpp.pdf)
所有博文均为原著,如若转载,请注明出处!