Next permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.
If Such arrangement is not a possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).
The replacement must is in-place, do not allocate extra memory.
Here is some examples. Inputs is in the left-hand column and its corresponding outputs is in the right-hand column.
1,2,3
→1,3,2
3,2,1
→1,2,3
1,1,5
→1,5,1
Gives a sequence of numbers that changes to the next sequence in the dictionary order
In the current sequence, two adjacent elements are searched forward from the end, the previous one is *i, the latter is *ii, and *i < *ii are satisfied. And then looking for another element from the end *j, if *i < *j is satisfied, the first element is swapped with the J element, and all elements after the second element (including II) are reversed, the next sequence is obtained.
Class Solution {public: void Nextpermutation (vector<int>& nums) { int len=nums.size (); int mark; if (len<=1) return; int i; for (i=len-1;i>0;i--) { if (nums[i]>nums[i-1]) { mark=i; break; } } if (i==0) {for (i=0;i<len/2;i++) swap (nums[i],nums[len-i-1]); return; } for (i=len-1;i>mark;i--) if (nums[i]>nums[mark-1]) break; Swap (nums[i],nums[mark-1]); For (I=mark;i<len && i< (mark+len)/2;i++) swap (nums[i],nums[len-1+mark-i]);} };
longest Valid parentheses
Given A string containing just the characters ‘(‘
‘)‘
and, find the length of the longest valid (well-formed) parenthe SES substring.
for " (() "
, the longest valid parentheses substring is " () "
, which has length = 2.
Another example ")()())"
is, where the longest valid parentheses substring "()()"
are, which has length = 4.
The length of the string that outputs the longest consecutive fully matched parentheses
First preprocessing the string, marking all the characters that can be matched, and then finding the longest possible gamete sequence.
Class Solution {public: int longestvalidparentheses (string s) { int len=s.length (); BOOL *a=new Bool[len]; memset (A,false,len); stack<int>st; for (int i=0;i<len;i++) if (s[i]== ' (') st.push (i); else { if (!st.empty ()) { a[st.top ()]=true; A[i]=true; St.pop (); } } int ans=0,cnt=0;; for (int i=0;i<len;i++) { if (a[i]==true) cnt++; else cnt=0; Ans=max (ans,cnt); } return ans; };
Search in rotated Sorted Array
Suppose a sorted array is rotated on 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 is given a target value to search. If found in the array is return its index, otherwise return-1.
Assume no duplicate exists in the array.
Finds the specified value in a string with a loop increment array
In the middle of the two points to determine whether the target belongs to the left or right interval can
Class Solution {public: int search (vector<int>& nums, int target) { int len=nums.size (); if (len==0) return-1; int l,r,mid; l=0; r=len-1; while (l<=r) { mid= (l+r)/2; if (Nums[mid]==target) return mid; else if (nums[mid]<target) { if (Nums[r]>=target | | nums[r]<nums[mid]) l=mid+1; else r=mid-1; } else { if (Nums[l]<=target | | nums[l]>nums[mid]) r=mid-1; else l=mid+1; } } return-1; }};
Search for a Range
Given a sorted array of integers, find the starting and ending position of a Given target value.
Your algorithm ' s runtime complexity must is in the order of O(log n).
if The target is not found in the array, Return [-1,-1]
.
For example,
Given [5, 7, 7, 8, 8, 10]
and target value 8,
Return [3, 4]
.
output find the range of numbers
Find the leftmost and most right end of the number, respectively.
Class Solution {public:vector<int> Searchrange (vector<int>& nums, int target) {Vector<i nt>ans; int len=nums.size (); if (len==0) {ans.push_back (-1); Ans.push_back (-1); return ans; } int l,r,mid; l=0; r=len-1; while (l<=r) {mid= (l+r)/2; if (nums[mid]==target) {if (L+1==r | | l==r) break; else if (nums[mid-1]==target) r=mid-1; else {l=r=mid; Break }} else if (nums[mid]>target) r=mid-1; else l=mid+1; } if (Nums[l]!=target) {ans.push_back (-1); Ans.push_back (-1); return ans; } ans.push_back (L); l=0; r=len-1; while (L<=R) {mid= (l+r+1)/2; if (nums[mid]==target) {if (L+1==r | | l==r) break; else if (Mid+1<len && nums[mid+1]==target) l=mid+1; else {l=r=mid; Break }} else if (nums[mid]>target) r=mid-1; else l=mid+1; } ans.push_back (R); return ans; }};
Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would is if it were inserted in order.
Assume no duplicates in the array.
Here is few examples.
[1,3,5,6]
, 5→2
[1,3,5,6]
, 2→1
[1,3,5,6]
, 7→4
[1,3,5,6]
, 0→0
Returns the position in which the target is inserted sequentially into the sequence
Simple two-point
Class Solution {public: int Searchinsert (vector<int>& nums, int target) { if (nums.size () ==0) return 0; int l,r,mid; l=0; R=nums.size ()-1; while (l<=r) { mid= (l+r)/2; if (Nums[mid]==target) return mid; if (nums[mid]<target) l=mid+1; else r=mid-1; } return l; }};
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Leetcode 31-35 Questions of solving code