
leetcode
Nightmare_ak
蒟蒻的求生之路
展开
-
leetcode 5. Longest Palindromic Substring
马拉车求最长回文子串class Solution { const static int N=1e5+5; char ss[N]; int L[N];public: string longestPalindrome(string s) { int len=0; ss[len++]='$'; for(int i=0;i...原创 2019-02-27 16:05:51 · 116 阅读 · 0 评论 -
leetcode 32. Longest Valid Parentheses
class Solution {public: int longestValidParentheses(string s) { stack<int> stk; vector<int> num(s.size(),0); int mxx=0; for(int i=0;i<s.size();++i){...原创 2019-08-24 21:58:14 · 141 阅读 · 0 评论 -
leetcode 31. Next Permutation
class Solution {public: void nextPermutation(vector<int>& nums) { int p=-1; for(int i=(int)nums.size()-2;i>=0;i--){ if(nums[i]<nums[i+1]){ ...原创 2019-08-24 20:22:07 · 127 阅读 · 0 评论 -
leetcode 30. Substring with Concatenation of All Words
双指针class Solution {public: vector<int> findSubstring(string s, vector<string>& words) { if(s==""||!words.size()) return vector<int>(); unordered_map<stri...原创 2019-08-24 09:12:57 · 152 阅读 · 0 评论 -
leetcode 241. Different Ways to Add Parentheses
枚举每个符号作为中间符号,递归计算两边所有可能,组合在一起即可class Solution {public: vector<int> num; vector<char> op; vector<int> dfs(int l,int r) { if(l==r) return {num[l/2]}; ...原创 2019-03-14 18:56:24 · 335 阅读 · 1 评论 -
leetcode 160. Intersection of Two Linked Lists
链表,参考标解,非常巧妙/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ...原创 2019-03-14 16:23:12 · 134 阅读 · 0 评论 -
leetcode 18. 4Sum
思路和上上题一致,复杂度O(n3)O(n^3)O(n3)class Solution {public: vector<vector<int>> fourSum(vector<int>& nums, int target) { sort(nums.begin(),nums.end()); vector<ve...原创 2019-03-04 19:18:04 · 134 阅读 · 0 评论 -
leetcode 16. 3Sum Closest
思路与上一题一致,每次都为达到targettargettarget而l++l++l++或r−−r--r−−,在过程中记录差值最小的值即可,复杂度O(N2)O(N^2)O(N2)class Solution {public: int threeSumClosest(vector&lt;int&gt;&amp; nums, int target) { sort(nums.b...原创 2019-03-04 18:43:22 · 142 阅读 · 0 评论 -
leetcode 75. Sort Colors
双指针维护,2都往右放,0都往左放class Solution {public: void sortColors(vector<int>& nums) { int n=nums.size(); int l=0,r=n-1; for(int i=0;i<n;i++) { wh...原创 2019-03-10 10:47:19 · 139 阅读 · 0 评论 -
leetcode 215. Kth Largest Element in an Array
分别写了堆排序和快速排序class Solution {public: int findKthLargest(vector<int>& nums, int k) { int n=nums.size(); auto modify=[&](int l,int r) { while(2*l+1&...原创 2019-03-10 09:46:06 · 140 阅读 · 0 评论 -
leetcode 69. Sqrt(x)
二分,为了不使用long long,过程中需要一些技巧class Solution {public: int mySqrt(int x) { if(x<=1) return x; int l=0,r=x; while(l<=r) { int m=(l+r)>>1; ...原创 2019-03-14 15:32:15 · 142 阅读 · 0 评论 -
leetcode 15. 3Sum
标解O(n2)O(n^2)O(n2),我的方法看上去是O(n2logn)O(n^2logn)O(n2logn)class Solution {public: struct Tuple { int a,b,c; bool operator<(const Tuple&t)const { if(a!...原创 2019-03-03 10:56:25 · 149 阅读 · 0 评论 -
leetcode 11. Container With Most Water
标解O(n),开了题解才明白。。。自己的方法是O(nlogn)的,从大到小枚举,二分+树状数组得到最小或最大的下标。class Solution{public: int maxArea(vector&lt;int&gt;&amp; height) { int n=height.size(); int l=0,r=n-1,mxx=0; ...原创 2019-03-02 21:21:24 · 204 阅读 · 0 评论 -
leetcode 10. Regular Expression Matching
区间dp,虽然AC了,但是有个疑问abc和.**我觉得是YES吧,标程输出NO,可能题目不考虑多个*的情况吧。class Solution {public: bool isMatch(string s, string p) { int n=s.length(),m=p.length(); vector< vector<bool> >...原创 2019-02-28 10:44:22 · 161 阅读 · 0 评论 -
leetcode 8. String to Integer (atoi)
sscanf()的使用class Solution {public: typedef long long ll; int myAtoi(string str) { ll x=0; sscanf(str.c_str(),"%lld",&x); if(x<-(1LL<<31)) x=-(1LL<<...原创 2019-02-27 21:27:41 · 127 阅读 · 0 评论 -
leetcode 7. Reverse Integer
简单的题目反而坑点多啊,用long long防止溢出class Solution {public: typedef long long ll; int reverse(int x) { int f=1; ll xx=x; if(x<0) f=-1,xx=-xx; ll ans=0; while...原创 2019-02-27 21:13:04 · 119 阅读 · 0 评论 -
leetcode 6. ZigZag Conversion
class Solution {public: string convert(string s, int numRows) { int len=s.size(); int x=1,dirx=0; string v[numRows+1]; for(int i=0;i&lt;len;i++) { ...原创 2019-02-27 20:59:23 · 146 阅读 · 0 评论 -
leetcode 33. Search in Rotated Sorted Array
class Solution {public: int search(vector<int>& nums, int target) { int l=0,r=nums.size(); while(l<r){ int m=l+r>>1; if(target==nums[m])...原创 2019-08-24 22:20:36 · 145 阅读 · 0 评论