题目
解析
一道经典的DP题目.
dp[i] = min(dp[i - coins[0]], dp[i - coins[1]] ... dp[i - coins[n]])
dp[0] = 0
代码
c++
1 | class Solution { |
一道经典的DP题目.
dp[i] = min(dp[i - coins[0]], dp[i - coins[1]] ... dp[i - coins[n]])
dp[0] = 0
1 | class Solution { |
将字母与数字的映射从大到小排列, 然后贪心的减去最大的值并拼接字符串.
1 | func intToRoman(num int) string { |
常规的level order traversal即可, 没什么难度.
1 | /** |
参考wiki, 实现HashMap
, 需要考虑的有如下几点:
面试能把这几点说清楚就差不多了. 这题不应该标记简单…
1 | // template <typename K, typename V> |
因为题中说到words.size() <= 7
, 所以可以按照字符串长度给words
排序, 复杂度几乎可以忽略不计. 之后再逐个尝试插入即可.
当words.size()
很大时, 就需直接用前缀树了.
1 | class Solution { |
1 | class Trie { |
标准的level order traversal, 没啥好讲的.
1 | /** |
用两个指针同时开始遍历两个list, 在任意一个指针走到头时开始切换到另一个head继续遍历. 再两指针相等时退出循环. 此时如果两指针不为空则停在了交点处, 否则代表两个list没有交点.
1 | /** |
Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.
Follow up: Could you implement a solution using only O(1) extra space complexity and O(n) runtime complexity?
Example 1:
Input: nums = [3,0,1]
Output: 2
Explanation: n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums.
Example 2:
Input: nums = [0,1]
Output: 2
Explanation: n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums.
Constraints:
求和,异或都行.非常简单.
1 | class Solution { |
1 | public int missingNumber(int[] nums) { |
You have a set of integers s, which originally contains all the numbers from 1 to n. Unfortunately, due to some error, one of the numbers in s got duplicated to another number in the set, which results in repetition of one number and loss of another number.
You are given an integer array nums representing the data status of this set after the error.
Find the number that occurs twice and the number that is missing and return them in the form of an array.
Example 1:
Input: nums = [1,2,2,4]
Output: [2,3]
Example 2:
Input: nums = [1,1]
Output: [1,2]
Constraints:
nums.length
<= $10^4$nums[i]
<= $10^4$根据题意,要返回一个长度为2的数组, 其中res[0]
为重复出现的值, res[1]
为缺少的值.
对于任意整数i, 有 i ^ i == 0
=> 1^2^3^4 ^ 1^2^3^4 == 0
=> 1^2^3^4 ^ 1^2^2^4 == 2 ^ 3
, 将异或结果暂存在res[1]
;
统计每个元素的出现次数, 将出现两次的数记录在res[0]
, 此时res[1] = res[1] ^ res[0]
.
1 | class Solution { |