剑指 Offer 66. 构建乘积数组
https://leetcode-cn.com/problems/gou-jian-cheng-ji-shu-zu-lcof/
static class Solution {
public int[] constructArr(int[] a) {
if (a == null || a.length <= 0) {
return new int[0];
}
// 左边所有数字的乘积 乘以 右边所有数字的乘积(右边的乘积可以通过左边的乘积算出)
int[] res = new int[a.length];
res[0] = 1;
for (int i = 1; i < a.length; i++) {
res[i] = res[i - 1] * a[i - 1];
}
int R = 1;
for (int i = a.length - 1; i >= 0; i--) {
res[i] = res[i] * R;
R = R * a[i];
}
return res;
}
}
- 时间:O(n)
- 空间:O(1)
剑指 Offer 44. 数字序列中某一位的数字
https://leetcode-cn.com/problems/shu-zi-xu-lie-zhong-mou-yi-wei-de-shu-zi-lcof/
思路: