#31
public class No31 {
public static void main(String[] args) {
nextPermutation(new int[]{1,2,3,6,4,6,5});
}
public static void nextPermutation(int[] nums) {
int i = 0, j = 0, k = 0, left = 0,right = nums.length - 1;
for (int x = nums.length - 1; x > 0; --x) {
if (nums[x] > nums[x-1]){
i=x-1;
j=x;
break;
}
}
boolean desc = (i == j);
if (!desc){
for(int y=nums.length-1;y>=j;--y){
if(nums[y]>nums[i]){
k=y;
break;
}
}
int temp=nums[k];
nums[k]=nums[i];
nums[i]=temp;
left=j;
}
while(left<right){
int temp1=nums[left];
nums[left]=nums[right];
nums[right]=temp1;
right--;
left++;
}
}
}
#32
@Test
public void test(){
String s = "(";
System.out.println(this.longestValidParentheses(s));
}
public int longestValidParentheses(String s) {
int max = 0;
if (s.length() == 0){
return max;
}
char[] chars = s.toCharArray();
int count = 0;
int p = 0;
char[] mystack = new char[chars.length];
int ps = -1;
int match=0;
while (p < chars.length) {
if (chars[p] == '('){
match++;
mystack[++ps]='(';
} else if (chars[p] == ')'){
match--;
mystack[++ps]=')';
if (match >= 0){
count+=2;
}else if(match < 0){
max = Math.max(max,count);
count=0;
match=0;
ps=-1;
}
}
p++;
}
match=0;
count=0;
while (ps >=0){
if (mystack[ps] == ')'){
match++;
}else if (mystack[ps] == '('){
match--;
if(match>=0){
count+=2;
}else if (match<0){
max=Math.max(max,count);
count=0;
match=0;
}
}
ps--;
}
max = Math.max(max,count);
return max;
}
}