1、给定一个字符串,请你找出其中不含有重复字符的最长子串的长度(无重复字符的最长子串)
代码如下:
public int lengthOfLongestSubstring(String s){
int n = s.length();
int ans = 0;
for(int i=0; i<n; i++){
for(int j=i+1; j<=n; j++){
if(allUnique(s, i, j)){
ans=Math.max(ans, j-i);
}
}
}
return ans;
}
public boolean allUnique(String s, int start, int end){
Set<Character> set = new HashSet<>();
for(int i=start; i<end; i++){
Character ch = s.charAt(i);
if(set.contains(ch)){
return false;
}
set.add(ch);
}
return true;
}
2、操作给定的二叉树,将其变换为源二叉树的镜像。
解法1:
    /**
      * 递归实现
      * 关键就在于把大问题转化为子问题即可
      * @param root
      */
     public static void Mirror1(TreeNode root) {
         if(root != null) {
             TreeNode temp = root.left;
             root.left = root.right;
             root.right = temp;
             Mirror1(root.left);
             Mirror1(root.right);
         }
     }
解法2:
    /**
      * 非递归实现, 借助栈或者列表遍历树中每一个非叶子节点
      * 交换其左右子树即可
      * @param root
      */
     public static void Mirror2(TreeNode root) {
         if(root == null) {
             return;
         }
         Stack<TreeNode> stack = new Stack<>();
         stack.push(root);
         while(!stack.isEmpty()) {
             TreeNode node = stack.pop();
             TreeNode temp = node.left;
             node.left = node.right;
             node.right = temp;
             if(node.left != null) {
                 stack.push(node.left);
             }
             if(node.right != null) {
                 stack.push(node.right);
             }
         }
     }

京公网安备 11010502036488号