-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
cbowu
committed
Aug 31, 2017
1 parent
411e8c5
commit 882b3b2
Showing
5 changed files
with
146 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |