0.9
This commit is contained in:
1 parent
a41ef5b99d
commit
8a0a7ccabb
2 files changed
+81
No files matched your search
@@ -0,0 +1,46 @@
|
|||||||
|
package com.hong.www;
|
||||||
|
|
||||||
|
public class A32 {
|
||||||
|
public int longestValidParentheses(String s) {
|
||||||
|
int left = 0, right = 0, max = 0;
|
||||||
|
|
||||||
|
// 从左往右:处理右括号多余的情况
|
||||||
|
for (int i = 0; i < s.length(); i++) {
|
||||||
|
if (s.charAt(i) == '(') {
|
||||||
|
left++;
|
||||||
|
} else {
|
||||||
|
right++;
|
||||||
|
}
|
||||||
|
if (left == right) {
|
||||||
|
max = Math.max(max, 2 * right);
|
||||||
|
} else if (right > left) {
|
||||||
|
left = 0;
|
||||||
|
right = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 从右往左:处理左括号多余的情况
|
||||||
|
left = 0;
|
||||||
|
right = 0;
|
||||||
|
for (int i = s.length() - 1; i >= 0; i--) {
|
||||||
|
if (s.charAt(i) == '(') {
|
||||||
|
left++;
|
||||||
|
} else {
|
||||||
|
right++;
|
||||||
|
}
|
||||||
|
if (left == right) {
|
||||||
|
max = Math.max(max, 2 * left);
|
||||||
|
} else if (left > right) {
|
||||||
|
left = 0;
|
||||||
|
right = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return max;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
A32 a32 = new A32();
|
||||||
|
System.out.println(a32.longestValidParentheses("(()"));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
package com.hong.www;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class A41 {
|
||||||
|
public int firstMissingPositive(int[] nums) {
|
||||||
|
int x=0;
|
||||||
|
for(int i=0;i<nums.length;i++){
|
||||||
|
while(nums[i]!=i+1&&nums[i]>0&&nums[i]<=nums.length){
|
||||||
|
if(nums[i]==nums[nums[i]-1]){
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
int temp=nums[nums[i]-1];
|
||||||
|
nums[nums[i]-1]=nums[i];
|
||||||
|
nums[i]=temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* for(int i=0;i<nums.length;i++){`
|
||||||
|
/* while (nums[i]!=i+1){
|
||||||
|
nums[i] = nums[nums[i]-1];
|
||||||
|
}
|
||||||
|
} */
|
||||||
|
for(int i=0;i<nums.length;i++){
|
||||||
|
if(nums[i]!=i+1){
|
||||||
|
return i+1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nums.length+1;
|
||||||
|
}
|
||||||
|
public static void main(String args[]){
|
||||||
|
A41 a41 = new A41();
|
||||||
|
int[] nums = {1,1};
|
||||||
|
System.out.println(a41.firstMissingPositive(nums));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user