爬到第 x 级台阶的方案数是爬到第 x−1 级台阶的方案数和爬到第 x−2 级台阶的方案数的和,f(0)=1,f(1)=1,对空间的优化使用滚动数组
1 2 3 4 5 6 7 8 9 10 11 12 13
classSolution { public: intclimbStairs(int n){ int sPre = 0, pre = 0, cur = 1; for (int i = 1; i <= n; ++i) { sPre = pre; // 赋值顺序不能反 pre = cur; cur = sPre + pre; } return cur; } };
1 2 3 4 5 6 7 8 9 10 11 12
classSolution { publicintclimbStairs(int n) { intsPre=0, pre = 0, cur = 1; for (inti=1; i <= n; ++i) { sPre = pre; pre = cur; cur = sPre + pre; } return cur; } }
1 2 3 4 5 6 7 8
classSolution: defclimbStairs(self, n: int) -> int: sPre, pre, cur = 0, 0, 1# 赋值 for _ inrange(n): # 遍历,_不使用 sPre = pre pre = cur cur = sPre + pre return cur
defmultiplication(self, a, b): c = [[1, 1], [1, 0]] for i inrange(2): for j inrange(2): c[i][j] = a[i][0] * b[0][j] + a[i][1] * b[1][j] return c
def_matrixPow(self, a, n): unit = [[1, 0], [0, 1]] while n > 0: if ((n & 1) == 1): unit = self.multiplication(unit, a) a = self.multiplication(a, a) n >>= 1 return unit
classSolution { public List<List<Integer>> generate(int numRows) { List<List<Integer>> result = newArrayList<List<Integer>>(); for (int i= 0; i < numRows; ++i) { List<Integer> row = newArrayList<>(); for (intj=0; j <= i; ++j) { if (j == 0 || j == i) row.add(1); else row.add(result.get(i - 1).get(j - 1) + result.get(i - 1).get(j)); } result.add(row); } return result; } }
1 2 3 4 5 6 7 8 9 10
classSolution: defgenerate(self, numRows: int) -> List[List[int]]: result = [] for i inrange(numRows): row = [] for j inrange(0, i + 1): # 0-i if j == 0or j == i: row.append(1) else: row.append(result[i - 1][j] + result[i - 1][j - 1]) result.append(row) return result
四:只出现一次的数字
1. 位运算
题目要求使用空间复杂度为常量,因此常规解法不可用。这里采用异或,性质如下
1 2 3 4 5 6 7 8 9
classSolution { public: intsingleNumber(vector<int>& nums){ int result = 0; for (auto num : nums) result ^=num; return result; } };
1 2 3 4 5 6 7 8
classSolution { publicintsingleNumber(int[] nums) { intresult=0; for (int num : nums) // 没有auto result ^=num; return result; } }
1 2 3 4 5 6
classSolution: defsingleNumber(self, nums: List[int]) -> int: result = 0 for num in nums: result ^=num return result # return reduce(lambda x, y: x ^ y, nums) 使用lambda,reduce从可迭代对象中取出第一个和第二个元素,用指定的函数进行计算,将计算结果与第三个元素再次进行计算,直到所有元素处理完毕
classSolution { public: intmajorityElement(vector<int>& nums){ while (true) { int count = 0; int right = nums[rand() % nums.size()]; // 也可以假设第一个为多数,依次尝试,比随机时间稍低,因为不会重复 for (auto num : nums) if (num == right) ++count; if (count > nums.size() / 2) return right; } } };
1 2 3 4 5 6 7 8 9 10 11 12 13
classSolution { publicintmajorityElement(int[] nums) { Randomrand=newRandom(); while (true) { intright= nums[rand.nextInt(nums.length)]; intcount=0; for (int num : nums) if (num == right) ++count; if (count > nums.length / 2) return right; } } }
1 2 3 4 5 6 7 8
classSolution: defmajorityElement(self, nums: List[int]) -> int: whileTrue: right = random.choice(nums) count = 0 for num in nums: # 或if sum(1 for elem in nums if elem == candidate) > majority_count: if num == right: count += 1 if count > len(nums) // 2: return right
intrecursion(vector<int>& nums, int left, int right) { if (left == right) return nums[left]; int mid = ((right - left) >> 1) + left; int leftNum = recursion(nums, left, mid); int rightNum = recursion(nums, mid + 1, right); // 两者相等可以直接返回了leftNum == rightNum if (countNum(nums, leftNum, left, right) > (right - left + 1) / 2) return leftNum; if (countNum(nums, rightNum, left, right) > (right - left + 1) / 2) return rightNum; return-1; }
intcountNum(vector<int>& nums, int target, int lo, int hi) { int count = 0; for (int i = lo; i <= hi; ++i) if (nums[i] == target) ++count; return count; } };
classSolution { public: intmajorityElement(vector<int>& nums){ int right = -1; int count = 0; for (auto num : nums) { if (count == 0) right = num; if (num == right) ++count; else --count; } return right; } };
1 2 3 4 5 6 7 8 9 10 11 12 13
classSolution { publicintmajorityElement(int[] nums) { intright= -1; intcount=0; for (int num : nums) { if (count == 0) right = num; if (num == right) ++count; else --count; } return right; } }
1 2 3 4 5 6 7 8 9
classSolution: defmajorityElement(self, nums: List[int]) -> int: right = -1 count = 0 for num in nums: if (count == 0): right = num if (num == right): count += 1 else: count -= 1 return right