Commit 6540bf0a authored by yaozc's avatar yaozc
Browse files

keep going

parent b55d7630
......@@ -1087,38 +1087,225 @@ public:
Generate Parentheses
//我草真的高兴啊!!!
//第一题没看答案我自己写出来的啊!!!
class Solution {
public:
vector<string> generateParenthesis(int n) {
vector<string> res;
if(n==0) return res;
vector<char> all_parentheses;
for(int i=0; i<n; i++){
all_parentheses.push_back('(');
all_parentheses.push_back(')');
}
helper(res,"", 0, all_parentheses);
helper(res, "", 0, 0, n);
return res;
}
void helper(vector<string>& res, string current_res, int current_used, vector<char>& all_parentheses){
if(current_used == all_parentheses.size()) res.push_back(current_res);
else{
for(int i=0; i<all_parentheses.size(); i++){
char add_one = all_parentheses[current_used];
current_res.push_back(add_one);
helper(res, current_res ,current_used+1, all_parentheses);
current_res.pop_back();
}
void helper(vector<string>& res, string current_res, int left_num, int right_num, int n){
if(left_num==n &&right_num==n) {res.push_back(current_res); return;}
else{
if(left_num < n && left_num >= right_num){
current_res.push_back('(');
helper(res, current_res, left_num+1, right_num, n);
current_res.pop_back();
}
if(right_num < n){
current_res.push_back(')');
helper(res, current_res, left_num, right_num+1, n);
current_res.pop_back();
}
}
}
};
Word Search
// DFS
// BFS用queue做会有找不到是否访问过的问题,非常难解决
class Solution {
public:
bool exist(vector<vector<char>>& board, string word) {
if (word=="") return true;
for(int outer=0; outer<board.size(); outer++){
for(int inner=0; inner<board[0].size(); inner++){
if(board[outer][inner] == word[0]){
if (DFS(board, word, 0, outer, inner)) return true;
}
}
}
return false;
}
bool DFS(vector<vector<char>>& board, string &word, int level, int current_y, int current_x ){
if(level==word.size()) return true;
if(current_y<0 || current_y >=board.size() || current_x<0 || current_x>=board[0].size() || board[current_y][current_x] != word[level])
return false;
char temp = board[current_y][current_x];
board[current_y][current_x] = ' ';
bool found = DFS(board, word, level+1, current_y+1, current_x) ||
DFS(board, word, level+1, current_y-1, current_x) ||
DFS(board, word, level+1, current_y, current_x+1) ||
DFS(board, word, level+1, current_y, current_x-1);
board[current_y][current_x] = temp;
return found;
}
};
Median of Two Sorted Arrays
//https://www.youtube.com/watch?v=LPFhl65R7ww&t=428s
//解: 找出两个array中的两个index,将两个array分为4部分,称之为 x1, x2, y1, y2
//两个index使得: len(x1) + len(y1) == len(x2) + len(y2)
// max(x1) <= min(y2), min(x2) >= max(y1);
//array1的x的index称为partition_x, array2的称为partition_y
//只需要找到xpartition_x。因为 partition_y = (len(array1) + len(array2) + 1) / 2
//这题太难了,不想修代码了,这段跑不起来的
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int x = cal_partition_x(0, nums1.size()-1, nums1, nums2);
int y = (nums1.size() + nums2.size() + 1)/2 - x;
return max(nums1[x], nums2[y]);
}
int cal_partition_x(int start, int end, vector<int>& nums1, vector<int>& nums2){
int partition_x = (start + end)/2;
int partition_y = (nums1.size() + nums2.size() + 1)/2 - partition_x;
if(nums1[partition_x]<=nums2[partition_y+1] && nums2[partition_y]<=nums1[partition_x+1]) return partition_x;
else{
if(nums1[partition_x]>nums2[partition_y+1]){
return cal_partition_x(0, partition_x, nums1, nums2);
}
else{
return cal_partition_x(partition_x, nums1.size()-1 , nums1, nums2);
}
}
}
};
Search in Rotated Sorted Array
// not working, gave up
class Solution {
public:
int search(vector<int>& nums, int target) {
int lo = 0;
int high = nums.size()-1;
while(lo<high){
int mid = (lo+high)/2;
if(nums[lo]>high) lo=mid;
else high=mid;
}
int pivot = lo;
if(target>=nums[0] && target<=nums[pivot]){
lo = 0, high = pivot;
while(lo<=high){
int mid = (lo+high)/2;
if(nums[mid]==target) return mid;
else if(nums[mid]>target) high=mid;
else lo=mid;
}
}
else{
lo = pivot+1, high = nums.size()-1;
while(lo<=high){
int mid = (lo+high)/2;
if(nums[mid]==target) return mid;
else if(nums[mid]>target) high=mid;
else lo=mid;
}
}
return -1;
}
};
Merge Intervals
//因为这题要用到sort,且sort的内容是vector inside vector,所以此题交了python的答案
//想法很简单,以list第一个element为sort内容,sort一遍整个2d list
//遍历ins(intervals)。如果当前元素的第二个小于下一个元素的第一个(两个interval有交集), 那么就合并
//不然统一push到res里去
//c++标准答案
vector<Interval> merge(vector<Interval>& ins) {
if (ins.empty()) return vector<Interval>{};
vector<Interval> res;
sort(ins.begin(), ins.end(), [](Interval a, Interval b){return a.start < b.start;});
res.push_back(ins[0]);
for (int i = 1; i < ins.size(); i++) {
if (res.back().end < ins[i].start) res.push_back(ins[i]);
else
res.back().end = max(res.back().end, ins[i].end);
}
return res;
}
// 我提交的答案
/*
class Solution(object):
def merge(self, intervals):
"""
:type intervals: List[List[int]]
:rtype: List[List[int]]
"""
if not intervals:
return []
intervals = sorted(intervals,key=lambda l:l[0])
res = [intervals[0]]
for part in intervals[1:]:
if(res[-1][1] < part[0]):
res.append(part)
else:
res[-1][1] = max(part[1], res[-1][1])
return res
*/
Two Sum II - Input array is sorted
//这也忒简单了呵呵呵
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
vector<int> res;
int left=0, right=numbers.size()-1;
while(left < right) {
int current = numbers[left] + numbers[right];
if(current == target){
res.push_back(left+1);
res.push_back(right+1);
return res;
}
else if(current > target) right--;
else left++;
}
return res;
}
};
......@@ -1129,6 +1316,17 @@ public:
......
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