asdasdsada
class Solution {
int sumCount = 0;
public int findTargetSumWays(int[] nums, int target) {
calculate(nums, target , 0 , 0);
return sumCount;
}
public void calculate(int[] nums , int target , int curVal , int i){
if(i == nums.length){
if(curVal == target)
sumCount++;
}else{
calculate(nums , target , curVal+nums[i] , i+1);
calculate(nums , target , curVal-nums[i] , i+1);
}
}
}
class Solution {
public static int findTargetSumWays(int[] nums, int s) {
int sum = 0;
for (int i = 0; i < nums.length; i++)
sum += nums[i];
if (Math.abs(s) > Math.abs(sum)) return 0;
int len = nums.length;
int range = sum * 2 + 1;
int[][] dp = new int[len][range];
dp[0][sum + nums[0]] += 1;
dp[0][sum - nums[0]] += 1;
for (int i = 1; i < len; i++) {
for (int j = -sum; j <= sum; j++) {
if((j+nums[i]) > sum) {
dp[i][j+sum] = dp[i-1][j-nums[i]+sum]+0;
}else if((j-nums[i]) < -sum) {
dp[i][j+sum] = dp[i-1][j+nums[i]+sum]+0;
}else {
dp[i][j+sum] = dp[i-1][j+nums[i]+sum]+dp[i-1][j-nums[i]+sum];
}
}
}
return dp[len - 1][sum + s];
}
}
asdasdsada
class Solution {
public boolean isMatch(String s, String p) {
int sLen = s.length();
int pLen = p.length();
boolean[][] dp = new boolean[sLen+1][pLen+1];
dp[0][0] = true;
for(int j = 2 ; j<=pLen ; j+=2){
if(p.charAt(j-1) == '*'){
dp[0][j] = dp[0][j-2];
}
}
for(int i=1 ; i<=sLen ; i++){
for(int j=1 ; j<=pLen ; j++){
int sCurChar = s.charAt(i-1);
int pCurChar = p.charAt(j-1);
if(sCurChar == pCurChar || pCurChar == '.'){
dp[i][j] = dp[i-1][j-1];
}else if(pCurChar == '*'){
if(dp[i][j-2]){
dp[i][j] = dp[i][j-2];
}else if(sCurChar == p.charAt(j-2) || p.charAt(j-2) == '.'){
dp[i][j] = dp[i-1][j];
}
}
}
}
return dp[sLen][pLen];
}
}
|