283. Move Zeroes
class Solution {
public:
void swap(vector<int>& nums, int a, int b) {
int tmp = nums[a];
nums[a] = nums[b];
nums[b] = tmp;
}
void moveZeroes(vector<int>& nums) {
int len = nums.size();
for(int i = 0, k = 0; i < len; i++) {
if(nums[i]) {
if(k != i) {
swap(nums, i, k);
}
k++;
}
}
}
};
27. Remove Element
class Solution {
public:
void swap(vector<int>& nums, int a, int b) {
int tmp = nums[a];
nums[a] = nums[b];
nums[b] = tmp;
}
int removeElement(vector<int>& nums, int val) {
int len = nums.size();
int new_len = 0;
for(int idx = 0; idx < len; idx++) {
if(nums[idx] != val) {
if(idx != new_len) {
swap(nums, idx, new_len);
}
new_len++;
}
}
return new_len;
}
};
26. Remove Duplicates from Sorted Array
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int len = nums.size();
int new_len = 1;
if(!len) {
return 0;
}
int tmp = nums[0];
for(int idx = 1; idx < len; idx++) {
if(tmp != nums[idx]) {
if(new_len != idx) {
nums[new_len] = nums[idx];
}
new_len++;
}
tmp = nums[idx];
}
return new_len;
}
};
80. Remove Duplicates from Sorted Array II
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int len = nums.size();
int new_len = 1;
if(!len) {
return 0;
}
int tmp = nums[0];
bool isDiff = true;
for(int idx = 1; idx < len; idx++) {
if(tmp != nums[idx]) {
if(new_len != idx) {
nums[new_len] = nums[idx];
}
isDiff = true;
new_len++;
}
else {
if(isDiff) {
nums[new_len] = nums[idx];
new_len++;
isDiff = false;
}
}
tmp = nums[idx];
}
return new_len;
}
};
75. Sort Colors
最简单的就是计数排序,但是需要遍历两次:
class Solution {
public:
void sortColors(vector<int>& nums) {
int len = nums.size();
int count[3] = {0};
for(int idx = 0; idx < len; idx++) {
assert(nums[idx] >= 0 && nums[idx] <= 2);
count[nums[idx]]++;
}
int stt = 0;
for(int idx = 0; idx < 3; idx++) {
for(int idx2 = 0; idx2 < count[idx]; idx2++) {
nums[stt+idx2] = idx;
}
stt += count[idx];
}
}
};
三路快排的写法:
class Solution {
public:
void sortColors(vector<int>& nums) {
int len = nums.size();
int zero = -1;
int two = len;
for(int idx = 0; idx < two;) {
if(nums[idx] == 1) {
idx++;
}
else if(nums[idx] == 2) {
two--;
nums[idx] = nums[two];
nums[two] = 2;
}
else {
assert(nums[idx] == 0);
zero++;
nums[idx] = nums[zero];
nums[zero] = 0;
idx++;
}
}
}
};
88. Merge Sorted Array
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
assert(nums1.size() >= m+n);
for(int idx = m - 1; idx >= 0; idx--) {
nums1[idx+n] = nums1[idx];
}
int idx1 = n;
int idx2 = 0;
int len = m+n;
int idx = 0;
while(idx1 < len && idx2 < n) {
if(nums1[idx1] > nums2[idx2]) {
nums1[idx++] = nums2[idx2++];
}
else {
nums1[idx++] = nums1[idx1++];
}
}
if(idx2 < n) {
for(int i = idx2; idx2 < n; idx2++) {
nums1[idx++] = nums2[idx2];
}
}
}
};
167. Two Sum II - Input array is sorted
法一:使用二分查找
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
int len = numbers.size();
int stt = 0;
int fin = len - 1;
vector<int> res;
for(int idx1 = 0; idx1 < len - 1; idx1++) {
while(idx1 > 0 && idx1 < len - 1 && numbers[idx1-1] == numbers[idx1]) {
idx1++;
}
int tar = target - numbers[idx1];
int stt2 = idx1+1;
int fin2 = fin;
while(stt2 <= fin2) {
int mid = stt2+((fin2-stt2)>>1);
if(numbers[mid] < tar) {
stt2++;
}
else if(numbers[mid] > tar) {
fin2--;
}
else {
res.push_back(idx1+1);
res.push_back(mid+1);
return res;
}
}
}
throw invalid_argument("No solution!");
return res;
}
};
法二:使用对撞指针,也叫做夹逼法。
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
int len = numbers.size();
int stt = 0;
int fin = len - 1;
vector<int> res;
while(stt < fin) {
int sum = numbers[stt] + numbers[fin];
if(sum < target) {
stt++;
}
else if(sum > target) {
fin--;
}
else {
res.push_back(stt+1);
res.push_back(fin+1);
return res;
}
}
throw invalid_argument("No solution!");
return res;
}
};
125. Valid Palindrome
class Solution {
public:
bool isChar(char a) {
return (a >= 'a'&& a <= 'z') || (a >= 'A' && a <= 'Z');
}
bool isPalindrome(string s) {
int len = s.size();
int stt = 0;
int fin = len - 1;
while(stt <= fin) {
while(stt<=fin) {
if(!isdigit(s[stt]) && !isChar(s[stt])) {
stt++;
if(stt > fin) {
return true;
}
}
else {
break;
}
}
while(stt<=fin) {
if(!isdigit(s[fin]) && !isChar(s[fin])) {
fin--;
if(fin < stt) {
return true;
}
}
else {
break;
}
}
if(tolower(s[stt]) == tolower(s[fin])) {
stt++;
fin--;
}
else {
return false;
}
}
return true;
}
};
344. Reverse String
class Solution {
public:
string reverseString(string s) {
int len = s.size();
int stt = 0;
int fin = len - 1;
while(stt < fin) {
char tmp = s[stt];
s[stt] = s[fin];
s[fin] = tmp;
stt++;
fin--;
}
return s;
}
};
345. Reverse Vowels of a String
class Solution {
public:
bool isVowel(char a) {
return a == 'a' || a == 'e' || a == 'i'
|| a == 'o' || a == 'u' || a == 'A'
|| a == 'E' || a == 'I' || a == 'O'
|| a == 'U';
}
string reverseVowels(string s) {
int len = s.size();
int stt = 0;
int fin = len - 1;
while(stt < fin) {
while(stt < fin) {
if(!isVowel(s[stt])) {
stt++;
}
else {
break;
}
if(stt >= fin) {
return s;
}
}
while(fin > stt) {
if(!isVowel(s[fin])) {
fin--;
}
else {
break;
}
if(stt >= fin) {
return s;
}
}
char tmp = s[stt];
s[stt] = s[fin];
s[fin] = tmp;
stt++;
fin--;
}
return s;
}
};
11. Container With Most Water
class Solution {
public:
int maxArea(vector<int>& height) {
int size = height.size();
assert(size >= 2);
int stt = 0;
int fin = size - 1;
int higher = height[stt] > height[fin]?1:0;
int res = higher?height[fin]*(fin-stt):height[stt]*(fin-stt);
int left_max = height[stt];
int right_max = height[fin];
while(stt < fin) {
if(!higher) {
while(height[stt] <= left_max) {
stt++;
if(stt >= fin) {
return res;
}
}
left_max = height[stt];
}
else {
while(height[fin] <= right_max) {
fin--;
if(stt >= fin) {
return res;
}
}
right_max = height[fin];
}
higher = left_max > right_max?1:0;
int area = (higher?right_max:left_max)*(fin-stt);
if(area > res) {
res = area;
}
}
return res;
}
};
209. Minimum Size Subarray Sum
class Solution {
public:
int minSubArrayLen(int s, vector<int>& nums) {
int len = nums.size();
int res = len + 1;
int stt = 0;
int fin = -1;
int sum = 0;
while(stt < len) {
if(sum < s && fin + 1 < len) {
fin++;
sum += nums[fin];
}
else {
sum -= nums[stt];
stt++;
}
if(sum >= s) {
res = min(fin-stt+1, res);
}
}
return res==len+1?0:res;
}
};
3. Longest Substring Without Repeating Characters
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int len = s.size();
int idx1 = 0;
int idx2 = -1;
int rec[512] = {0};
int res = 0;
while(idx1 < len) {
if(idx2+1 < len && rec[s[idx2+1]] == 0) {
idx2++;
rec[s[idx2]]++;
}
else {
rec[s[idx1]]--;
idx1++;
}
res = max(idx2-idx1+1, res);
}
return res;
}
};