Skip to content

Commit

Permalink
add some new file
Browse files Browse the repository at this point in the history
  • Loading branch information
cbowu committed Aug 31, 2017
1 parent 411e8c5 commit 882b3b2
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@

public class Main {
public static void main(String[] args) {
int[] input ={9,8,7,6,5,4,3,2,1,0};
int[] res = new Solution66().plusOne(input);
String a ="11";
String b ="1";
String c = new Solution67().addBinary(a,b);
}

}
29 changes: 29 additions & 0 deletions src/Solution312.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Created by cuibowu on 2017/8/31.
*/
public class Solution312 {
public int maxCoins(int[] nums) {
if (nums == null || nums.length == 0) return 0;

int[][] dp = new int[nums.length][nums.length];
for (int len = 1; len <= nums.length; len++) {
for (int start = 0; start <= nums.length - len; start++) {
int end = start + len - 1;
for (int i = start; i <= end; i++) {
int coins = nums[i] * getValue(nums, start - 1) * getValue(nums, end + 1);
coins += i != start ? dp[start][i - 1] : 0;
coins += i != end ? dp[i + 1][end] : 0;
dp[start][end] = Math.max(dp[start][end], coins);
}
}
}
return dp[0][nums.length - 1];
}

private int getValue(int[] nums, int i) {
if (i < 0 || i >= nums.length) {
return 1;
}
return nums[i];
}
}
30 changes: 30 additions & 0 deletions src/Solution5.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Created by cuibowu on 2017/8/31.
*/
public class Solution5 {
public String longestPalindrome(String s) {
int dp=0;
String res ="";
for(int i=0;i<s.length();i++){
if(isPalindrome(s.substring(i-dp,i+1))){
res= s.substring(i-dp,i+1);
dp+=2;
}else if(isPalindrome(s.substring(i-dp-1,i+1))){
res = s.substring(i-dp-1,i+1);
dp++;
}
}
return res;
}
private boolean isPalindrome(String s ){
if(s.length()==1)
return true;
else {
for(int i=0,j=s.length()-1;i<j;){
if(s.charAt(i--)!=s.charAt(j--))
return false;
}
}
return true;
}
}
38 changes: 38 additions & 0 deletions src/Solution556.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import java.util.Arrays;

/**
* Created by cuibowu on 2017/8/31.
*/
public class Solution556 {
public int nextGreaterElement(int n) {
char[] number = String.valueOf(n).toCharArray();

int i=0,j=0;
for(i=number.length-1;i>0;i--){
if(number[i-1]<number[i]){
break;
}
}
if(i==0)
return -1;
int x=number[i-1], min =i;
for(j=i+1;i<number.length;j++){
if(number[j]>x&&number[j]<=number[min]){
min=j;
}
}

char temp = number[i-1];
number[i-1]=number[min];
number[min]= temp;

Arrays.sort(number,i,number.length);

long res = Long.parseLong(new String(number));
if(res>Integer.MAX_VALUE)
return -1;
else
return (int)res;

}
}
46 changes: 46 additions & 0 deletions src/Solution67.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Created by cuibowu on 2017/8/31.
*/
public class Solution67 {
public String addBinary(String a, String b) {
StringBuilder s = new StringBuilder();
int lengthA = a.length(), lengthB = b.length();
int i = lengthA - 1, j = lengthB - 1;
int flag = 0;
while (i >= 0 || j >= 0) {
int sum = 0;
if (i >= 0 && j < 0) {
sum = Integer.parseInt("" + a.charAt(i)) + flag;
} else if (i < 0 && j >= 0) {
sum = Integer.parseInt("" + b.charAt(j)) + flag;
} else if (i >= 0 && j >= 0) {
sum = Integer.parseInt("" + a.charAt(i)) + Integer.parseInt("" + b.charAt(j)) + flag;
}
switch (sum) {
case 0:
s.insert(0, '0');
flag = 0;
break;
case 1:
s.insert(0, '1');
flag = 0;
break;
case 2:
s.insert(0, '0');
flag = 1;
break;
case 3:
s.insert(0, '1');
flag = 1;
break;
default:
break;
}
i--;
j--;
}
if (flag == 1)
s.insert(0, flag);
return s.toString();
}
}

0 comments on commit 882b3b2

Please sign in to comment.