leetcode-7.30

  • Hamming Distance
    • 整数的二进制形式 相对应的位置 bit位(0、1)不同的有几位?
    • 考察二进制位运算
    • Integer 与二进制
public class Solution {
    public int hammingDistance(int x, int y) {
        return Integer.bitCount(x^y);

    }
}
  • Merge Two Binary Trees
    • 计算相应位置的二叉树节点的和,若有一个节点不存在,则默认值为0
    • 递归遍历二叉树
    • 考察递归
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
        if(t1==null && t2 == null){
            return  null;
        }else {
            int val = (t1==null?0:t1.val) + (t2==null?0:t2.val);
            TreeNode treeNode = new TreeNode(val);
            treeNode.left = mergeTrees(t1==null?null:t1.left,t2==null?null:t2.left);//注意判断空
            treeNode.right = mergeTrees(t1==null?null:t1.right,t2==null?null:t2.right);
            return treeNode;
        }
    }
}
  • Array Partition I
    • 给定一个2n个整数组成的数组,计算∑min(a1,a2)使这个数最大,其中数字任意组合,但只能使用一次。
    • 考察逻辑能力 计算和最大,其实是变相的计算两两差最小,所以演变成一个排序问题。
      • Assume in each pair i, bi >= ai.
        • Denote Sm = min(a1, b1) + min(a2, b2) + … + min(an, bn). The biggest Sm is the answer of this problem. Given 1, Sm = a1 + a2 + … + an.
        • Denote Sa = a1 + b1 + a2 + b2 + … + an + bn. Sa is constant for a given input.
        • Denote di = |ai - bi|. Given 1, di = bi - ai. Denote Sd = d1 + d2 + … + dn.
        • So Sa = a1 + a1 + d1 + a2 + a2 + d2 + … + an + an + di = 2Sm + Sd => Sm = (Sa - Sd) / 2.
        • To get the max Sm, given Sa is constant, we need to make Sd as small as possible.
          So this problem becomes finding pairs in an array that makes sum of di (distance between ai and bi) as small as possible. Apparently, sum of these distances of adjacent elements is the smallest. If that’s not intuitive enough, see attached picture. Case 1 has the smallest Sd.
          这里写图片描述
public class Solution {
    public int arrayPairSum(int[] nums) {
          Arrays.sort(nums);
        int result = 0;
        for (int i = 0; i < nums.length; i += 2) {
            result += nums[i];
        }
        return result;
    }
}
  • Number Complement
    • 位运算,计算一个整数的NoLeading形式的补码
    • 正则+Stream
Integer.toBinaryString(~5).substring(Integer.numberOfLeadingZeros(5))
  • Keyboard Row
    • 给出一个字符串,判断它所有的元素是不是都在键盘的一行上

这里写图片描述

  • Reverse Words in a String III
    • 翻转字符串中的单词
    • Stream+StringBuilder
public class Solution {
      public String[] findWords(String[] words) {
       return Stream.of(words).filter(a->a.toLowerCase().matches("[qwertyuiop]*|[asdfghjkl]*|[zxcvbnm]*")).toArray(String[]::new);
    }
}
  • Reshape the Matrix
    • 数组重构 将一个n*m的数组转成 x*y的数组。
    • 时间复杂度要求O(n)
    • 对数组下标的理解。
    • 当前索引对数组长宽取余数。
public class Solution {
    public int[][] matrixReshape(int[][] nums, int r, int c) {
     int m = nums.length, n = nums[0].length;
       if(m*n != r*c){
           return  nums;
       }else {
           int[][] result = new int[r][c];
           for (int i = 0; i < m * n; i++) {
               result[i/c][i%c] = nums[i/n][i%n];
           }
           return result;
       }

    }
}
  • Distribute Candies
    • 哥哥妹妹分糖问题,妹妹获得糖的种类最多
    • 逻辑推理能力
    • 计算糖的种类数,妹妹哥哥平分,所以妹妹获得最多一半的糖,如果种类数>总数/2则结果为种类数/2,否则为种类数。
public class Solution {
    public int distributeCandies(int[] candies) {
         long count = Arrays.stream(candies).distinct().count();
        return candies.length/2 <= count ?candies.length/2 : (int) count;
    }
}
  • Fizz Buzz
    • Stream
public class Solution {
    public List<String> fizzBuzz(int n) {
         ArrayList<String> result = new ArrayList<>();
        for (int i = 1; i <= n; i++) {
            if (i%3==0 && i%5==0){
                result.add("FizzBuzz");
            }else if(i%3==0){
                result.add("Fizz");
            }else if(i%5==0){
                result.add("Buzz");
            }else {
                result.add(i+"");
            }
        }
        return result;
    }
}
  • Average of Levels in Binary Tree
    • 计算二叉树每一层的平均数
    • 对队列的理解
    • BLF
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List<Double> averageOfLevels(TreeNode root) {
         List<Double> result = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        if (queue == null) return result;
        queue.add(root);
        while (!queue.isEmpty()) {
            int n = queue.size();
            double total = 0.0;
            for (int i = 0; i < n; i++) {
                TreeNode node = queue.poll();
                total += node.val;
                if (node.left != null) queue.offer(node.left);
                if (node.right != null) queue.offer(node.right);
            }
            result.add(total / n);
        }
        return result;
    }
}
  • Island Perimeter
    • 计算岛屿周长
    • 对二维数组的理解
    • 对数组下标的理解
public class Solution {
  public int islandPerimeter(int[][] grid) {
        int islands = 0, neighbours = 0;

        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[i].length; j++) {
                if (grid[i][j] == 1) {
                    islands++; // count islands
                    if (i < grid.length - 1 && grid[i + 1][j] == 1) neighbours++; // count down neighbours
                    if (j < grid[i].length - 1 && grid[i][j + 1] == 1) neighbours++; // count right neighbours
                }
            }
        }

        return islands * 4 - neighbours * 2;
    }
}
  • Nim Game
    • 逻辑思维找规律
    • 非四的倍数
public class Solution {
    public boolean canWinNim(int n) {
        return  n>>2<<2!=n;  
    }
}
  • Longest Uncommon Subsequence I
    • 最长的不同的字符串
    • 逻辑思维能力
      • 如果A、B不同,则结果为AB中最长的那个。
      • 如果AB相同,则不存在这样的Uncommon子串。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值