mirror of
https://github.com/huihut/interview.git
synced 2026-03-13 10:02:55 +08:00
First Update
This commit is contained in:
14
Problems/LeetcodeProblems/best-time-to-buy-and-sell-stock.h
Normal file
14
Problems/LeetcodeProblems/best-time-to-buy-and-sell-stock.h
Normal file
@@ -0,0 +1,14 @@
|
||||
class Solution {
|
||||
public:
|
||||
int maxProfit(vector<int>& prices) {
|
||||
int left = 0, right = 0;
|
||||
int max = 0, temp = 0;
|
||||
for(auto i = 0; i < prices.size(); ++i) {
|
||||
right = i;
|
||||
temp = prices[right] - prices[left];
|
||||
if(temp < 0) left = i;
|
||||
if(max < temp) max = temp;
|
||||
}
|
||||
return max;
|
||||
}
|
||||
};
|
||||
22
Problems/LeetcodeProblems/container-with-most-water.h
Normal file
22
Problems/LeetcodeProblems/container-with-most-water.h
Normal file
@@ -0,0 +1,22 @@
|
||||
class Solution {
|
||||
public:
|
||||
int maxArea(vector<int>& height) {
|
||||
int left = 0, right = height.size()-1;
|
||||
int max = 0, temp = 0;
|
||||
while(left < right) {
|
||||
temp = (right - left) * (height[left] < height[right] ? height[left] : height[right]);
|
||||
if(max < temp) max = temp;
|
||||
if(height[left] < height[right]) {
|
||||
do{
|
||||
left++;
|
||||
} while(left < right && height[left-1] >= height[left]);
|
||||
}
|
||||
else {
|
||||
do{
|
||||
right--;
|
||||
} while(left < right && height[right] <= height[right+1]);
|
||||
}
|
||||
}
|
||||
return max;
|
||||
}
|
||||
};
|
||||
26
Problems/LeetcodeProblems/majority-element.h
Normal file
26
Problems/LeetcodeProblems/majority-element.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#include <map>
|
||||
class Solution {
|
||||
public:
|
||||
int majorityElement(vector<int>& nums) {
|
||||
int n = nums.size();
|
||||
map<int, int> m_times;
|
||||
int moreThanTimes = n / 2;
|
||||
for (auto i = 0; i < n; ++i) {
|
||||
auto it = m_times.find(nums[i]);
|
||||
if (it == m_times.end()) {
|
||||
// No existence in map, insert
|
||||
m_times.insert(std::pair<int, int>(nums[i], 1));
|
||||
it = m_times.find(nums[i]);
|
||||
}
|
||||
// Is it more than ⌊ n/2 ⌋ times ?
|
||||
if ((*it).second > moreThanTimes) {
|
||||
return (*it).first;
|
||||
}
|
||||
else {
|
||||
(*it).second++;
|
||||
}
|
||||
}
|
||||
// Can't find
|
||||
return -1;
|
||||
}
|
||||
};
|
||||
13
Problems/LeetcodeProblems/maximum-subarray.h
Normal file
13
Problems/LeetcodeProblems/maximum-subarray.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#define MIN_INT -2147483648
|
||||
class Solution {
|
||||
public:
|
||||
int maxSubArray(vector<int>& nums) {
|
||||
int max = MIN_INT, temp = 0;
|
||||
for(auto i = 0; i < nums.size(); ++i) {
|
||||
temp += nums[i];
|
||||
if(temp > max) max = temp;
|
||||
if(temp < 0) temp = 0;
|
||||
}
|
||||
return max;
|
||||
}
|
||||
};
|
||||
12
Problems/LeetcodeProblems/move-zeroes.h
Normal file
12
Problems/LeetcodeProblems/move-zeroes.h
Normal file
@@ -0,0 +1,12 @@
|
||||
class Solution {
|
||||
public:
|
||||
void moveZeroes(vector<int>& nums) {
|
||||
int left = 0, right = 0;
|
||||
for(; left < nums.size() && nums[left] != 0; left++);
|
||||
for(right = left; right < nums.size(); right++) {
|
||||
if(nums[right] == 0) continue;
|
||||
nums[left++] = nums[right];
|
||||
}
|
||||
while(left < nums.size()) nums[left++] = 0;
|
||||
}
|
||||
};
|
||||
18
Problems/LeetcodeProblems/two-sum.h
Normal file
18
Problems/LeetcodeProblems/two-sum.h
Normal file
@@ -0,0 +1,18 @@
|
||||
class Solution {
|
||||
public:
|
||||
vector<int> twoSum(vector<int>& nums, int target) {
|
||||
vector<int> sum;
|
||||
for(auto i = 0; i < nums.size(); i++)
|
||||
{
|
||||
for(auto j = i+1; j < nums.size(); j++)
|
||||
{
|
||||
if(i == j) continue;
|
||||
if(nums[i] + nums[j] != target) continue;
|
||||
sum.push_back(i);
|
||||
sum.push_back(j);
|
||||
return sum;
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user