mirror of
https://github.com/huihut/interview.git
synced 2024-03-22 13:10:48 +08:00
删除LeetcodeProblems
This commit is contained in:
parent
296d3cf894
commit
85910c51f2
|
@ -1,18 +0,0 @@
|
|||
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;
|
||||
}
|
||||
};
|
|
@ -1,22 +0,0 @@
|
|||
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;
|
||||
}
|
||||
};
|
|
@ -1,21 +0,0 @@
|
|||
class Solution {
|
||||
public:
|
||||
vector<vector<int>> generate(int numRows) {
|
||||
vector<vector<int>> triangle;
|
||||
for(int i = 0; i < numRows; i++){
|
||||
vector<int> v;
|
||||
if(i==0){
|
||||
v.push_back(1);
|
||||
}
|
||||
else{
|
||||
v.push_back(1);
|
||||
for(int j = 0; j < triangle[i-1].size()-1; j++){
|
||||
v.push_back(triangle[i-1][j] + triangle[i-1][j+1]);
|
||||
}
|
||||
v.push_back(1);
|
||||
}
|
||||
triangle.push_back(v);
|
||||
}
|
||||
return triangle;
|
||||
}
|
||||
};
|
|
@ -1,13 +0,0 @@
|
|||
class Solution {
|
||||
public:
|
||||
vector<int> getRow(int rowIndex) {
|
||||
vector<int> v(rowIndex + 1, 0);
|
||||
v[0] = 1;
|
||||
for (int i = 0; i < rowIndex; i++){
|
||||
for(int j = i + 1; j > 0; j--){
|
||||
v[j] += v[j - 1];
|
||||
}
|
||||
}
|
||||
return v;
|
||||
}
|
||||
};
|
|
@ -1,14 +0,0 @@
|
|||
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;
|
||||
}
|
||||
};
|
|
@ -1,26 +0,0 @@
|
|||
class Solution {
|
||||
public:
|
||||
int maxProfit(vector<int>& prices) {
|
||||
int max=0, begin=0, end=0;
|
||||
bool up=false, down=false;
|
||||
for (int i=1; i<prices.size(); i++) {
|
||||
if (prices[i] > prices[i-1] && up==false){ // goes up
|
||||
begin = i-1;
|
||||
up = true;
|
||||
down = false;
|
||||
}
|
||||
if (prices[i] < prices[i-1] && down==false) { // goes down
|
||||
end = i-1;
|
||||
down = true;
|
||||
up = false;
|
||||
max += (prices[end] - prices[begin]);
|
||||
}
|
||||
}
|
||||
// edge case
|
||||
if (begin < prices.size() && up==true){
|
||||
end = prices.size() - 1;
|
||||
max += (prices[end] - prices[begin]);
|
||||
}
|
||||
return max;
|
||||
}
|
||||
};
|
|
@ -1,26 +0,0 @@
|
|||
#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;
|
||||
}
|
||||
};
|
|
@ -1,14 +0,0 @@
|
|||
class Solution {
|
||||
public:
|
||||
int removeDuplicates(vector<int>& nums) {
|
||||
int n = nums.size(), pos = 0;
|
||||
if(n <= 1)
|
||||
return n;
|
||||
for(int i = 0; i < n-1; ++i) {
|
||||
if(nums[i] != nums[i+1]) {
|
||||
nums[++pos] = nums[i+1];
|
||||
}
|
||||
}
|
||||
return pos+1;
|
||||
}
|
||||
};
|
|
@ -1,12 +0,0 @@
|
|||
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;
|
||||
}
|
||||
};
|
|
@ -1,34 +0,0 @@
|
|||
class Solution {
|
||||
public:
|
||||
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
|
||||
int len1 = nums1.size();
|
||||
int len2 = nums2.size();
|
||||
int len = len1 + len2;
|
||||
if(len & 1)
|
||||
return findKth(nums1, 0, nums2, 0, len / 2 + 1);
|
||||
else
|
||||
return (findKth(nums1, 0, nums2, 0, len / 2) + findKth(nums1, 0, nums2, 0, len / 2 + 1)) / 2;
|
||||
}
|
||||
|
||||
// find kth number of two sorted array
|
||||
double findKth(vector<int>& nums1, int i1, vector<int>& nums2, int i2, int k) {
|
||||
if (i1 >= nums1.size()) {
|
||||
return nums2[i2 + k - 1];
|
||||
}
|
||||
if (i2 >= nums2.size()) {
|
||||
return nums1[i1 + k - 1];
|
||||
}
|
||||
if (k == 1) {
|
||||
return min(nums1[i1], nums2[i2]);
|
||||
}
|
||||
int key1 = i1 + k / 2 - 1 >= nums1.size() ? INT_MAX : nums1[i1 + k / 2 - 1];
|
||||
int key2 = i2 + k / 2 - 1 >= nums2.size() ? INT_MAX : nums2[i2 + k / 2 - 1];
|
||||
if (key1<key2) {
|
||||
return findKth(nums1, i1 + k / 2, nums2, i2, k - k / 2);
|
||||
}
|
||||
else {
|
||||
return findKth(nums1, i1, nums2, i2 + k / 2, k - k / 2);
|
||||
}
|
||||
}
|
||||
|
||||
};
|
|
@ -1,13 +0,0 @@
|
|||
#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;
|
||||
}
|
||||
};
|
|
@ -1,15 +0,0 @@
|
|||
class Solution {
|
||||
public:
|
||||
vector<int> plusOne(vector<int>& digits) {
|
||||
int carry = 1;
|
||||
for(int i = digits.size() - 1; i >= 0; --i) {
|
||||
int add = digits[i] + carry;
|
||||
digits[i] = add % 10;
|
||||
carry = add / 10;
|
||||
}
|
||||
if(carry > 0) {
|
||||
digits.insert(digits.begin(), carry);
|
||||
}
|
||||
return digits;
|
||||
}
|
||||
};
|
|
@ -1,20 +0,0 @@
|
|||
class Solution {
|
||||
public:
|
||||
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
|
||||
int i1 = m - 1, i2 = n - 1;
|
||||
for(int i = m + n - 1; i >= 0; i--) {
|
||||
if(i1 >= 0 && i2 < 0)
|
||||
break;
|
||||
if(i1 < 0 && i2 >= 0) {
|
||||
nums1[i] = nums2[i2--];
|
||||
}
|
||||
if(i1 >= 0 && i2 >= 0) {
|
||||
if(nums1[i1] > nums2[i2]) {
|
||||
nums1[i] = nums1[i1--];
|
||||
} else {
|
||||
nums1[i] = nums2[i2--];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue
Block a user