18. 4Sum
- Total Accepted: 100564
- Total Submissions: 391517
- Difficulty: Medium
- Contributors: Admin
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
Note: The solution set must not contain duplicate quadruplets.
For example, given array S = [1, 0, -1, 0, -2, 2], and target = 0. A solution set is: [ [-1, 0, 0, 1], [-2, -1, 1, 2], [-2, 0, 0, 2] ]
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
vector<vector<int>> result;
vector<int> temp;
if(nums.size()<4)return result;
sort(nums.begin(), nums.end());//#include<algorithm>
for (int i = 0; i<nums.size(); i++) {//四个的剔除掉
temp.push_back(nums[i]);
}
// temp.push_back(nums[nums.size() - 3]);
// temp.push_back(nums[nums.size() - 2]);
// temp.push_back(nums[nums.size() - 1]);
for (int i = 0; i < temp.size(); i++) {
for (int j = temp.size() - 1; j > i + 2; j--) {
int begin = i + 1, end = j - 1;
while (begin < end) {
if(temp[i]+temp[j]+temp[begin]+temp[end]==target){
vector<int>M;
M.push_back(temp[i]); M.push_back(temp[begin]);
M.push_back(temp[end]); M.push_back(temp[j]);
if(result.size()==0||result[result.size()-1]!=M)
result.push_back(M);
begin++; end--;
}
if (temp[i] + temp[j] + temp[begin] + temp[end] > target)end--;
if (temp[i] + temp[j] + temp[begin] + temp[end] < target)begin++;
}
while(nums[j]==nums[j-1])j--;
}
while(nums[i]==nums[i+1])i++;
}
return result;
}
};
19. Remove Nth Node From End of List
- Total Accepted: 153567
- Total Submissions: 477873
- Difficulty: Easy
- Contributors: Admin
Given a linked list, remove the nth node from the end of list and return its head.
For example,
Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5.
Note:
Given n will always be valid.
Try to do this in one pass.
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode *test;
ListNode *result;
test = head;
result = head;
int nums = 0;
if(head->next==NULL)return NULL;
while (test != NULL) {
test = test->next;
nums++;
}
for (int i = 0; i < nums - n-1; i++) {
head = head->next;
}
if (n == nums)result = result->next;
if(head->next->next==NULL)head->next = NULL;
else head->next = head->next->next;
return result;
}
};
- Total Accepted: 161385
- Total Submissions: 502409
- Difficulty: Easy
- Contributors: Admin
Given a string containing just the characters '('
, ')'
, '{'
, '}'
, '['
and ']'
, determine if the input string is valid.
The brackets must close in the correct order, "()"
and "()[]{}"
are all valid but "(]"
and "([)]"
are not.
Subscribe to see which companies asked this question
//利用char相减为1或2……,虽然好像很慢…
class Solution {
public:
bool isValid(string s) {
bool result=true;
if(s.size()%2==1)return false;
else if(s.size()==0)return true;
else {
int add=1;
for(int i=1;i<s.size();i++){
if(s[i]==s[0])add++;
if(s[i]==s[0]+1||s[i]==s[0]+2){
add--;
if(!add){
result=isValid(s.substr(1,i-1))*isValid(s.substr(i+1,s.size()-i-1));
break;
}
}
else{
result=false;
}
}
}
return result;
}
};
结果喜人,击败了0.8%的人,哈哈哈哈,能这么慢也是蛮好玩的
附上优雅的2ms的方法://stack的用法
class Solution {
public:
bool isValid(string s) {
stack<char> paren;
for (char& c : s) {
switch (c) {
case '(':
case '{':
case '[': paren.push(c); break;
case ')': if (paren.empty() || paren.top()!='(') return false; else paren.pop(); break;
case '}': if (paren.empty() || paren.top()!='{') return false; else paren.pop(); break;
case ']': if (paren.empty() || paren.top()!='[') return false; else paren.pop(); break;
default: ; // pass
}
}
return paren.empty() ;
}
};
去吃饭吃饭~