![](https://img-blog.csdnimg.cn/20201014180756922.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode
春_
life
展开
-
LeetCode每日打卡 - 4的幂
不使用循环来今天解题 public boolean isPowerOfFour(int n) { return n > 0 && (n & (n-1)) == 0 && n % 3==1; }首先去做了2的幂这道题,并了解了n&(n-1)的过程,然后思考一下这道题:求二进制中1的个数,理解了n&(n-1)这个题就好做了。 public static int xxxxx(int n) { .原创 2021-05-31 09:28:37 · 128 阅读 · 0 评论 -
LeetCode每日打卡 - 汉明距离总和
有点慢,两层循环也可以完成,就是换个方向,外层遍历32大小的bits数组,里层遍历nums的数字每次右移一位,计算方法类似。class Solution { public int totalHammingDistance(int[] nums) { //int 是4byte , 每个byte对应8位(比特),所以一个int是32位 int [] bits = new int[32]; int res=0, len = nums.length; .原创 2021-05-28 09:54:18 · 83 阅读 · 0 评论 -
LeetCode每日打卡 - 反转每对括号间的子串
题解中有个更好的思路,stack存入的不是(的坐标,存入每次到左括号的字符串,拿到右括号就对其进行反转,更妙一些。import java.util.Stack;class Solution { public String reverseParentheses(String s) { Stack<Integer> stack = new Stack<>(); char [] cs = s.toCharArray(); Str.原创 2021-05-27 09:11:24 · 86 阅读 · 0 评论 -
LeetCode每日打卡 - 汉明距离
就是二进制下相同位的数不同就+1我就按照最简单的逻辑写了,没优化class Solution { public int hammingDistance(int x, int y) { int num = x ^ y; int res = 0; int remainder = 0; while(num != 0){ if(num == 1){ res++; ..原创 2021-05-27 09:06:30 · 86 阅读 · 0 评论