八月份抄题
191. 位1的个数 8/4 晚11:00
思路
1.return Integer.bitCount(n);
2.https://leetcode-cn.com/problems/number-of-1-bits/solution/fu-xue-ming-zhu-xiang-jie-wei-yun-suan-f-ci7i/

n & (n - 1) 的技巧可以消除二进制中最后一个 1
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int res = 0;
while (n != 0) {
res += 1;
n &= n - 1;
}
return res;
}
}
118. 杨辉三角 8/6 下午两点二十
代码实现
class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> ret = new ArrayList<List<Integer>>();
for (int i = 0; i < numRows; ++i) {
List<Integer> row = new ArrayList<Integer>();
for (int j = 0; j <= i; ++j) {
if (j == 0 || j == i) {
row.add(1);
} else {
row.add(ret.get(i - 1).get(j - 1) + ret.get(i - 1).get(j));
}
}
ret.add(row);
}
return ret;
}
}


浙公网安备 33010602011771号