class Solution {
public int translateNum(int num) {
String str = String.valueOf(num);
int len = str.length();
if(len<2) return 1;
int[] dp = new int[len+1];
dp[0] = dp[1] = 1;
for(int i=2 ; i<=len ; i++){
String subStr = str.substring(i-2,i);
if(subStr.compareTo("10")>=0 && subStr.compareTo("25")<=0)
dp[i] = dp[i-2] +dp[i-1];
else
dp[i] = dp[i-1];
}
return dp[len];
}
}
class Solution {
public int lengthOfLongestSubstring(String s) {
int maxDifStr = 0;
int lef = 0, rig = 0;
char[] c_array = s.toCharArray();
int len = c_array.length;
Set<Character> set = new HashSet<>();
while(rig<len){
while(set.contains(c_array[rig])){
set.remove(c_array[lef++]);
}
set.add(c_array[rig]);
maxDifStr = Math.max(maxDifStr ,rig-lef+1);
rig++;
}
return maxDifStr;
}
}
class Solution {
public boolean isSubStructure(TreeNode A, TreeNode B) {
if(A==null || B == null)
return false;
return dfs(A, B) || isSubStructure(A.left , B) || isSubStructure(A.right , B);
}
private boolean dfs(TreeNode A , TreeNode B){
if(B==null)
return true;
if(A == null)
return false;
return A.val == B.val && dfs(A.left , B.left) && dfs(A.right , B.right);
}
}
dsadas
class Solution {
Map<Integer, Integer> map;
public TreeNode buildTree(int[] preorder, int[] inorder) {
int len = preorder.length;
if(len == 0)
return null;
map = new HashMap<>();
for(int i=0 ; i<len ; i++){
map.put(inorder[i] , i);
}
return myBuildTree(preorder,inorder,0,len-1,0,len-1);
}
private TreeNode myBuildTree (int[] preorder , int[] inorder , int pre_lef , int pre_rig , int inor_lef, int inor_rig){
if(pre_lef > pre_rig)
return null;
int preorder_root = pre_lef;
int intorder_root = map.get(preorder[preorder_root]);
TreeNode root = new TreeNode(preorder[preorder_root]);
int size_leftTree = intorder_root-inor_lef;
root.left = myBuildTree(preorder , inorder , preorder_root+1 , preorder_root+size_leftTree , inor_lef, intorder_root-1);
root.right = myBuildTree(preorder , inorder , preorder_root+size_leftTree+1 , pre_rig , intorder_root+1 , inor_rig);
return root;
}
}
class Solution {
Node head,pre;
public Node treeToDoublyList(Node root) {
if(root == null)
return null;
dfs(root);
head.left = pre;
pre.right = head;
return head;
}
private void dfs(Node cur){
if(cur == null)
return;
dfs(cur.left);
if(pre!=null)
pre.right = cur;
else
head = cur;
cur.left = pre;
pre = cur;
dfs(cur.right);
}
}
|