LeetCode 494.Target Sum & 516.Longest Palindromic Subsequence

来源:互联网 发布:什么是正定矩阵 编辑:程序博客网 时间:2024/05/15 03:00

494.Target Sum

You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symbols + and -. For each integer, you should choose one from + and - as its new symbol.

Find out how many ways to assign symbols to make sum of integers equal to target S.

Example 1:

Input: nums is [1, 1, 1, 1, 1], S is 3. Output: 5Explanation: -1+1+1+1+1 = 3+1-1+1+1+1 = 3+1+1-1+1+1 = 3+1+1+1-1+1 = 3+1+1+1+1-1 = 3There are 5 ways to assign symbols to make the sum of nums be target 3.

Note:

  1. The length of the given array is positive and will not exceed 20.
  2. The sum of elements in the given array will not exceed 1000.
  3. Your output answer is guaranteed to be fitted in a 32-bit integer.

解题思路:

1. 不需要记录每一个等式,只需要计算出能够等于结果的等式的个数

2. 每一个数有两种选择,一种是加另一种是减,所以题目的要求可以看成计算一个完全二叉树叶子结点中,值等于sum的个数

3. 所以利用深度搜索DFS,搜索每一个叶子节点的值,如果发现等于sum,则计数器加一,搜索完整个二叉树后,返回计数器的值即可。


代码如下:

public class Solution {    int count = 0;    public int findTargetSumWays(int[] nums, int S) {        find(nums,S,0,0);        return count;    }        public void find(int[] nums,int S,int pos,int sum){                if(sum == S && pos == nums.length){            count++;            return;        }        if(pos >= nums.length) return;        find(nums,S,pos +1,sum + nums[pos]);        find(nums,S,pos +1,sum - nums[pos]);    }}



516. Longest Palindromic Subsequence

Given a string s, find the longest palindromic subsequence's length in s. You may assume that the maximum length of s is 1000.

Example 1:
Input:

"bbbab"
Output:
4
One possible longest palindromic subsequence is "bbbb".

Example 2:
Input:

"cbbd"
Output:
2
One possible longest palindromic subsequence is "bb".


解题思路:

1. 要寻找字符串的最长回文串子序列,可以考虑回文串的性质——正反都是一样的,所以可以根据这点,将问题转换为,逆转字符串,寻找这两个字符串的最长相同子序列

2. 利用动态规划的思想,两个字符串S1、S2,用length[i][j]表示S1.substring(0, i) 和S2.substring(0, j)两个子串的最长相同子序列的长度,动态转移方程为:

length[i][j] =  max{ length[i -1][j], length[i][j-1] }  ( S1.charAt(i) != S2.charAt(j))

 =  length[i -1][j - 1] + 1  ( S1.charAt(i) == S2.charAt(j))


代码如下:

public class Solution {    public int longestPalindromeSubseq(String s) {    String s2 = reverse(s);    int length[][] = new int [s.length()][s.length()];        for(int i = 0;i < s.length();i++){    for(int j = 0;j < s.length();j++){        if(s.charAt(i) == s2.charAt(j)){        if(i == 0 || j == 0){            length[i][j] = 1;        }        else{            length[i][j] = length[i-1][j-1] + 1;        }        }     else{        if(i == 0 && j == 0){            length[i][j] = 0;        }        else{            if(j == 0){                length[i][j] = length[i - 1][j];            }            else if(i == 0){                length[i][j] = length[i][j -1];            }            else{                length[i][j] = length[i][j-1] > length[i-1][j]? length[i][j-1] : length[i-1][j];            }                    }        }        }    }    return length[s.length() - 1][s.length() - 1];    }    public String reverse(String str){    StringBuilder sb = new StringBuilder(str).reverse();    return sb.toString();    }}



0 0
原创粉丝点击