【LeetCode018-020】4Sum,单链表Nth去除,符号检测//指针与引用,stack用法
来源:互联网 发布:电脑逆战刷箱子软件 编辑:程序博客网 时间:2024/05/17 01:03
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() ; }};
去吃饭吃饭~
- 【LeetCode018-020】4Sum,单链表Nth去除,符号检测//指针与引用,stack用法
- leetcode018 Four Sum
- 引用“ & " 的用法 与 指针的用法
- const与引用,指针的初级用法
- 指针:指针与引用
- c++引用具体用法及与指针的差别
- 弱符号与强符号,弱引用与强引用
- 弱符号与弱引用
- JVM中符号引用与直接引用
- 走进java_符号引用与直接引用
- 指针与指针的引用
- 指针与指针的引用
- 指针与指针的引用-
- 指针与指针的引用
- 指针与二维指针、引用
- C++ 引用符号的用法 &
- 关于PHP引用(符号&)的用法
- 指针与引用
- 全选CheckBox删除操作,再次点击取消全选。得到每一个循环模块的ID值,用于后台SQL语句删除
- TranslateMessage
- MDK编译出现__use_no_semihosting was requested, but _ttywrch was referenced
- 关于git push命令中的matching和simple
- Leetcode-442. Find All Duplicates in an Array-思路详解-C++
- 【LeetCode018-020】4Sum,单链表Nth去除,符号检测//指针与引用,stack用法
- Android视频全屏SetRequestedOrientation
- 由前序+中序,输出二叉树
- Java TreeMap
- 历史的回顾,今日的惊喜
- 6-oracle中where 子句和having子句中的区别
- CenterOS下的mysql安装及初始化:
- Bitmap六种压缩模式
- java反射覆盖toString()方法