Given a string containing just the characters '('
and ')'
, find the length of the longest valid (well-formed) parentheses substring.
Example 1:
Input: "(()"
Output: 2
Explanation: The longest valid parentheses substring is "()"
Example 2:
Input: ")()())
" Output: 4 Explanation: The longest valid parentheses substring is"()()"
#include<stdio.h> #include<iostream> #include<vector> #include<algorithm> #include<stack> #include<string> #include<memory> #include<memory.h> #include<hash_map> #include<map> #include<set> #include<unordered_map> using namespace std; class Solution { public: int longestValidParentheses(string s) { stack<int> q; int start=0; int ans=0; for(int i=0;i<s.length();i++) { if(s[i]=='(') q.push(i); else { if(q.empty()) start=i+1; else { int temp=q.top();q.pop(); //+1 is to add the first( ans=max(ans,q.empty()?i-start+1:i-q.top()); } } } return ans; } }; int main() { string res={"(())"}; Solution s; int n; n=s.longestValidParentheses(res); cout<<n<<endl; return 0; }
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., [0,1,2,4,5,6,7]
might become [4,5,6,7,0,1,2]
).
You are given a target value to search. If found in the array return its index, otherwise return -1
.
You may assume no duplicate exists in the array.
Your algorithm's runtime complexity must be in the order of O(log n).
Example 1:
Input: nums = [4,5,6,7,0,1,2]
, target = 0
Output: 4
Example 2:
Input: nums = [4,5,6,7,0,1,2]
, target = 3 Output: -1
ATTENTION POINT:
please clarify > >= whether add = can cause largely difference in many situations
in this code if you not write l<=r ,not =,will cause a lot of problem
#include<stdio.h> #include<iostream> #include<vector> #include<algorithm> #include<stack> #include<string> #include<memory> #include<memory.h> #include<hash_map> #include<map> #include<set> #include<unordered_map> using namespace std; class Solution { public: int search(vector<int>& nums, int target) { int l=0,r=nums.size()-1; while(l<=r) { int mid=(l+r)/2; if(nums[mid]==target) return mid; if(nums[mid]<nums[r]) { if(target>nums[mid]&&target<=nums[r]) l=mid+1; else r=mid-1; } else { if(target>=nums[l]&&target<nums[mid]) { r=mid-1; } else { l=mid+1; } } } return -1; } }; int main() { vector<int> nums={4,5,6,7,0,1,2}; int target=0,res=0; Solution s; res=s.search(nums,target); cout<<res<<endl; return 0; }
The count-and-say sequence is the sequence of integers with the first five terms as following:
1. 1 2. 11 3. 21 4. 1211 5. 111221
1
is read off as "one 1"
or 11
.11
is read off as "two 1s"
or 21
.21
is read off as "one 2
, then one 1"
or 1211
.
Given an integer n where 1 ≤ n ≤ 30, generate the nth term of the count-and-say sequence.
Note: Each term of the sequence of integers will be represented as a string.
Example 1:
Input: 1 Output: "1"
Example 2:
Input: 4 Output: "1211"
ATTENTION:
1.for loop attention the begin and end;for(i=0/1;i</<=n;i++)
2.string has special action to solve,such as string+=a,then string add a behind it.
3.string number '3' and real number 3 the distance is '0',so it need to minus '0' before
it to add into calculation.
#include<stdio.h> #include<iostream> #include<vector> #include<algorithm> #include<stack> #include<string> #include<memory> #include<memory.h> #include<hash_map> #include<map> #include<set> #include<unordered_map> using namespace std; class Solution { public: string countAndSay(int n) { string ans="1"; for(int i=1;i<n;i++) { ans=say(ans); } return ans; } private: string say(const string& n) { string ans; int s=0,l=n.length(); for(int e=1;e<=l;e++) { if(e==l||n[s]!=n[e]) { int count=e-s; ans+='0'+count; ans+=n[s]; s=e; } } return ans; } }; int main() { string res; Solution s; int n=4; res=s.countAndSay(n); cout<<res<<endl; return 0; }