Commit d423a8df authored by yaozc's avatar yaozc
Browse files

keep going!

parent e972cfe3
File added
......@@ -3,47 +3,47 @@
// O(n^2)时间,double for loop
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
for(int outer=0; outer<nums.size()-1; outer++){
vector<int> result;
result.push_back(outer);
int temp = target;
temp -= nums[outer];
for(int inner=outer+1; inner<nums.size(); inner++){
if(temp-nums[inner] == 0){
result.push_back(inner);
return result;
}
}
vector<int> twoSum(vector<int>& nums, int target) {
for(int outer=0; outer<nums.size()-1; outer++){
vector<int> result;
result.push_back(outer);
int temp = target;
temp -= nums[outer];
for(int inner=outer+1; inner<nums.size(); inner++){
if(temp-nums[inner] == 0){
result.push_back(inner);
return result;
}
vector<int> result;
return result;
}
}
vector<int> result;
return result;
}
};
// solution 2
// O(n)时间,using hash table to find
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
// first is the rest, second is the indice
unordered_map<int, int> hash;
for(int i=0; i<nums.size(); i++){
int rest = target - nums[i];
auto iter = hash.find(rest);
if (iter != hash.end()) {
std::vector<int> result;
result.push_back(i);
result.push_back(iter->second);
return result;
}
hash[nums[i]] = i;
}
vector<int> twoSum(vector<int>& nums, int target) {
// first is the rest, second is the indice
unordered_map<int, int> hash;
for(int i=0; i<nums.size(); i++){
int rest = target - nums[i];
auto iter = hash.find(rest);
if (iter != hash.end()) {
std::vector<int> result;
result.push_back(i);
result.push_back(iter->second);
return result;
}
hash[nums[i]] = i;
}
std::vector<int> result;
return result;
}
};
......@@ -53,45 +53,109 @@ public:
// 然后依次判断左右高低,高的保留,低的跳过,左右筛选后再计算water值,直到最后计算出最大water
class Solution {
public:
int maxArea(vector<int>& height) {
int left = 0;
int right = height.size() -1;
int water = 0;
while(left < right){
water = max(water, ((right - left) * min(height[left], height[right])));
if(height[left] < height[right]){
left++;
}
else{
right--;
}
}
return water;
int maxArea(vector<int>& height) {
int left = 0;
int right = height.size() -1;
int water = 0;
while(left < right){
water = max(water, ((right - left) * min(height[left], height[right])));
if(height[left] < height[right]){
left++;
}
else{
right--;
}
}
};
return water;
}
};
// 3Sum Closest
// almost same as 3 sum,但是在最后的三个if else语句中的判断有逻辑上的区别
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
sort(nums.begin(), nums.end());
int closest = INFINITY;
int result;
for (int outer = 0; outer < nums.size()-1; outer++) {
int left = outer+1;
int right = nums.size()-1;
while (left < right) {
int sum = nums[outer] + nums[left] + nums[right];
int diff = target - sum;
if (abs(diff) < closest) {
closest = abs(diff);
result = sum;
}
if (diff < 0) {
right--;
}
else if (diff > 0) {
left++;
}
else{
return result;
}
}
}
return result;
}
};
// Implement strStr()
// KMP 算法的实现
// step 1. pre-process两个string,建立最长prefix的array
// step 2.若遇到不符,skip多少个由查ips表所知
/*
lps[i] = the longest proper prefix of pat[0..i]
which is also a suffix of pat[0..i].
For the pattern “ABCDE”,
pat = "AAAA"
lps[] is [0, 0, 0, 0, 0]
For the pattern “AABAACAABAA”,
pat = "AAAA"
lps[] is [0, 1, 0, 1, 2, 0, 1, 2, 3, 4, 5]
*/
// Rotate Image
// 矩阵旋转:
// 首先上下swap
// 然后对称线swap
// temp作为对角线终点变量(每一行到哪里停)
/*
* clockwise rotate
* first reverse up to down, then swap the symmetry
* 1 2 3 7 8 9 7 4 1
* 4 5 6 => 4 5 6 => 8 5 2
* 7 8 9 1 2 3 9 6 3
*/
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
swap(matrix.begin(), matrix.end());
int temp = 1;
for (int outer = 0; outer < matrix.size(); outer++) {
for (int inner = 0; inner < temp; inner++) {
swap(matrix[outer][inner], matrix[inner][outer]);
}
temp++;
}
}
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment