Skip to content

Commit

Permalink
add 7,15,283,345,347,349,389,453,492,538,598,606,657
Browse files Browse the repository at this point in the history
modify 448
  • Loading branch information
cbowu committed Aug 18, 2017
1 parent e7bf6ef commit 3bb56a0
Show file tree
Hide file tree
Showing 15 changed files with 316 additions and 8 deletions.
6 changes: 4 additions & 2 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
* Created by cuibowu on 2017/7/2.
*/

public class Main {
public static void main(String[] args) {
Solution279 solution279 = new Solution279();
int result = solution279.numSquares(13);
Solution347 solution347 = new Solution347();
String s = "(a)())()";
List<String> list = solution347.removeInvalidParentheses(s);
}
}
34 changes: 34 additions & 0 deletions src/Solution15.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
* Created by cuibowu on 2017/8/17.
*/
public class Solution15 {
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> lists = new LinkedList<>();
for (int i = 0; i < nums.length; i++) {
if (i == 0 || (i > 0 && nums[i] != nums[i - 1])) {
int sum = - nums[i];
for(int low=i+1,high=nums.length-1;low<high;){
if(nums[low]+nums[high]==sum){
lists.add(Arrays.asList(nums[i],nums[low],nums[high]));
while (low<high&&nums[low]==nums[low+1])
low++;
while ((low<high&&nums[high]==nums[high-1]))
high--;
low++;
high--;
}else if(nums[low]+nums[high]>sum){
high--;
}else if (nums[low]+nums[high]< sum){
low++;
}
}
}
}
return lists;
}
}
16 changes: 16 additions & 0 deletions src/Solution283.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Created by cuibowu on 2017/8/14.
*/
public class Solution283 {
public void moveZeroes(int[] nums) {
int count = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 0)
count++;
else
nums[i - count] = nums[i];
}
for (int i = nums.length - 1; i > nums.length - 1 - count; i--)
nums[i] = 0;
}
}
42 changes: 42 additions & 0 deletions src/Solution345.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.locks.ReentrantLock;

/**
* Created by cuibowu on 2017/8/18.
*/
public class Solution345 {
public String reverseVowels(String s) {
Set<Character> set = new HashSet<>();
set.add('a');
set.add('e');
set.add('i');
set.add('o');
set.add('u');
set.add('A');
set.add('E');
set.add('I');
set.add('O');
set.add('U');

StringBuilder ss = new StringBuilder(s);

for(int i=0,j=ss.length()-1;i<j;){
if(!set.contains(ss.charAt(i))){
i++;
continue;
}
if(!set.contains(ss.charAt(j))){
j--;
continue;
}
char temp = ss.charAt(i);
ss.replace(i,i+1,String.valueOf(ss.charAt(j)));
ss.replace(j,j+1,String.valueOf(temp));
i++;
j--;
}
return ss.toString();

}
}
30 changes: 30 additions & 0 deletions src/Solution347.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import java.util.ArrayList;
import java.util.List;

/**
* Created by cuibowu on 2017/8/18.
*/
public class Solution347 {
public List<String> removeInvalidParentheses(String s) {
List<String> ans = new ArrayList<>();
remove(s, ans, 0, 0, new char[]{'(', ')'});
return ans;
}

public void remove(String s, List<String> ans, int last_i, int last_j, char[] par) {
for (int count = 0, i = last_i; i < s.length(); ++i) {
if (s.charAt(i) == par[0]) count++;
if (s.charAt(i) == par[1]) count--;
if (count >= 0) continue;
for (int j = last_j; j <= i; ++j)
if (s.charAt(j) == par[1] && (j == last_j || s.charAt(j - 1) != par[1]))
remove(s.substring(0, j) + s.substring(j + 1, s.length()), ans, i, j, par);
return;
}
String reversed = new StringBuilder(s).reverse().toString();
if (par[0] == '(')
remove(reversed, ans, 0, 0, new char[]{')', '('});
else
ans.add(reversed);
}
}
27 changes: 27 additions & 0 deletions src/Solution349.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.IntStream;

/**
* Created by cuibowu on 2017/8/15.
*/
public class Solution349 {
public int[] intersection(int[] nums1, int[] nums2) {
Set<Integer> result = new HashSet<>();
Set<Integer> set = new HashSet<>();
for(int i=0;i<nums1.length;i++){
set.add(nums1[i]);
}
for(int i = 0 ;i<nums2.length;i++){
if(set.contains(nums2[i])){
result.add(nums2[i]);
}else {
continue;
}
}
int[] result0 = result.stream().mapToInt(integer->integer.intValue()).toArray();
return result0;
}
}
15 changes: 15 additions & 0 deletions src/Solution389.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Created by cuibowu on 2017/8/14.
*/
public class Solution389 {
public char findTheDifference(String s, String t) {
char result =0;
for(int i =0; i<s.length();i++){
result^=s.charAt(i);
}
for(int i=0;i<t.length();i++){
result^=t.charAt(i);
}
return result;
}
}
14 changes: 8 additions & 6 deletions src/Solution448.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
*/
public class Solution448 {
public List<Integer> findDisappearedNumbers(int[] nums) {
List<Integer> list = new ArrayList<>();
for(int i=1;i<=nums.length;i++){
list.add(i);
List<Integer> result = new ArrayList<>();
for(int i=0;i<nums.length;i++){
if(nums[Math.abs(nums[i])-1]>0)
nums[Math.abs(nums[i])-1]=-nums[Math.abs(nums[i])-1];
}
for(int i=0;i<nums.length;i++){
Integer a =new Integer(nums[i]);
list.remove(a);
if(nums[i]>0)
result.add(i+1);
}
return list;
return result;

}
}
12 changes: 12 additions & 0 deletions src/Solution453.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import java.util.stream.IntStream;

/**
* Created by cuibowu on 2017/8/15.
*/
public class Solution453 {
public int minMoves(int[] nums) {
int min = IntStream.of(nums).min().getAsInt();
int sum = IntStream.of(nums).sum();
return sum-min*nums.length;
}
}
20 changes: 20 additions & 0 deletions src/Solution492.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Created by cuibowu on 2017/8/15.
*/
public class Solution492 {
public int[] constructRectangle(int area) {
int x=area,y=1;
int min=x-y;
for(int i=1;i<=Math.sqrt(area);i++){
if(area%i==0){
if(area/i-i<min){
x=area/i;
y=i;
min=x-y;
}
}
}
int[] result = {x,y};
return result;
}
}
17 changes: 17 additions & 0 deletions src/Solution538.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Created by cuibowu on 2017/8/14.
*/


public class Solution538 {
int sum=0;
public TreeNode convertBST(TreeNode root) {
if(root==null)
return root;
root.right=convertBST(root.right);
root.val+=sum;
sum=root.val;
root.left=convertBST(root.left);
return root;
}
}
16 changes: 16 additions & 0 deletions src/Solution598.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Created by cuibowu on 2017/8/15.
*/
public class Solution598 {
public int maxCount(int m, int n, int[][] ops) {
int mMin=m,nMin=n;
for(int i=0;i<ops.length;i++){
if(ops[i][0]<mMin)
mMin=ops[i][0];
if(ops[i][1]<nMin)
nMin=ops[i][1];
}
return mMin*nMin;

}
}
26 changes: 26 additions & 0 deletions src/Solution606.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Created by cuibowu on 2017/8/14.
*/
public class Solution606 {
StringBuilder result = new StringBuilder();

public String tree2str(TreeNode t) {
newTree2str(t);
return result.toString();

}
void newTree2str(TreeNode t) {
result.append(t.val);
result.append("(");
if (t.left != null)
result.append(tree2str(t.left));
result.append(")");
if (t.right != null) {
result.append("(");
result.append(tree2str(t.right));
result.append(")");
}
}
}


23 changes: 23 additions & 0 deletions src/Solution657.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Created by cuibowu on 2017/8/14.
*/
public class Solution657 {
public boolean judgeCircle(String moves) {
int x = 0, y = 0;
for (int i = 0; i < moves.length(); i++) {
if (moves.charAt(i) == 'U') {
y++;
} else if (moves.charAt(i) == 'D') {
y--;
} else if(moves.charAt(i)=='L'){
x--;
}else if(moves.charAt(i)=='R'){
x++;
}
}
if(x==0&&y==0)
return true;
else
return false;
}
}
26 changes: 26 additions & 0 deletions src/Solution7.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Created by cuibowu on 2017/8/17.
*/
public class Solution7 {
public int reverse(int x) {
long y =x;
if (y == 0)
return 0;
StringBuilder stringBuilder = new StringBuilder();
if (y < 0) {
stringBuilder.append("-");
y=Math.abs(y);
}
while (y != 0) {
long temp = y % 10;
stringBuilder.append(temp);
y=(y-temp)/10;
}
String string = stringBuilder.toString();
long a = Long.parseLong(string);
if(a>2147483647||a<-2147483648)
return 0;
else
return (int)a;
}
}

0 comments on commit 3bb56a0

Please sign in to comment.