第25天--算法(Leetcode 91,101,102)
91.解码方法
public int numDecodings(String s) { char s1[] = s.toCharArray(); int N = s1.length; int dp[] = new int[N + 1]; dp[N] = 1; for(int index = N - 1;index >= 0;index --) { if(s1[index] == '0') { continue; } int ways = dp[index + 1]; if(index + 1 >= s1.length) { dp[index] = ways; continue; } int num = (s1[index] - '0') * 10 + s1[index + 1] - '0'; if(num <= 26) { ways += dp[index + 2]; } dp[index] = ways; } return dp[0]; } 101.对称二叉树 public boolean isSymmetric(TreeNode root) { return isMirror(root,root); } public boolean isMirror(TreeNode t1,TreeNode t2) { if(t1 == null && t2 == null) { return true; } if(t1 != null && t2 != null) { return t1.val == t2.val && isMirror(t1.left,t2.right) && isMirror(t1.right,t2.left); } return false; } 102.二叉树的层序遍历 public List- > levelOrder(TreeNode root) {
- > ans = new ArrayList<>();