0001. Two Sum [Easy]
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.
Example 1:Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Output: Because nums[0] + nums[1] == 9, we return [0, 1].
Example 2:Input: nums = [3,2,4], target = 6
Output: [1,2]
Example 3:Input: nums = [3,3], target = 6
Output: [0,1]
.
Constraints:
- 2 <= nums.length <= 104
- -109 <= nums[i] <= 109
- -109 <= target <= 109
- Only one valid answer exists.
# 方法一
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
# initiate 一个dict去存 target与当前值的差,和当前的index
# 例如 {5:0} 当前值在等一个5,来与之共同凑成target
temp = {}
# 用emuerate遍历nums数组
for i, n in enumerate(nums):
# 如果n在temp中,证明n刚好是之前遇到的某树缺的那个能与之求和等于target的值
if n in temp:
return [temp[n], i]
# 否则,就把当前值在等的那个数,和它自己的index存在dict中
temp[target-n] = i
# 方法二(一样的)
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
temp = {}
for i, n in enumerate(nums):
if target-n in temp:
return [temp[target-n], i]
temp[n] = i
时间复杂度:因为遍历了一遍nums,所以是O(n)
空间复杂度:用了额外的dict去存储,所以也是O(n)
1929. Concatenation of Array [Easy]
Given an integer array
nums
of lengthn
, you want to create an arrayans
of length2n
whereans[i] == nums[i]
andans[i + n] == nums[i]
for0 <= i < n
(0-indexed).Specifically,
ans
is the concatenation of twonums
arrays.Return the array
ans
.
Example 1:Input: nums = [1,2,1]
Output: [1,2,1,1,2,1]
Explanation: The array ans is formed as follows
ans = [nums[0],nums[1],nums[2],nums[0],nums[1],nums[2]]
ans = [1,2,1,1,2,1]Example 2:
Input: nums = [1,3,2,1]
Output: [1,3,2,1,1,3,2,1]
Explanation: The array ans is formed as follows:
ans = [nums[0],nums[1],nums[2],nums[3],nums[0],nums[1],nums[2],nums[3]]
ans = [1,3,2,1,1,3,2,1]Constraints:
- n == nums.length
- 1 <= n <= 1000
- 1 <= nums[i] <= 1000
# 方法一
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
ans = []
for i in range(2):
for n in nums:
ans.append(n)
return ans
# 方法二
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
ans = nums + nums
return ans
# 方法三
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
return nums*2
方法一和二比较慢,用时大约40ms,第三种方法快很多,可能是python在底层做了优化不是用的循环遍历吧。总的来说,循环遍历O(n), 空间复杂度不算输出arr的话是O(1)
0771. Jewels and Stones [Easy]
You’re given strings jewels representing the types of stones that are jewels, and stones representing the stones you have. Each character in stones is a type of stone you have. You want to know how many of the stones you have are also jewels.
Letters are case sensitive, so “a” is considered a different type of stone from “A”.
Example 1:
Input: jewels = “aA”, stones = “aAAbbbb”
Output: 3Example 2:
Input: jewels = “z”, stones = “ZZ”
Output: 0Constraints:
- 1 <= jewels.length, stones.length <= 50
- jewels and stones consist of only English letters.
- All the characters of jewels are unique.
class Solution:
def numJewelsInStones(self, jewels: str, stones: str) -> int:
# initiate 最后的返回值
res = 0
# 遍历string stones,看每颗s是否是jewel
for s in stones:
# 如果s是jewel,则res+1
if s in jewels:
res += 1
# 这里对s不是jewel就不写了,因为不在乎
return res
时间复杂度:遍历了一遍stone算O(n),对每次s遍历了一遍jewel(一次遍历jewel算O(m)的话),那总共是O(mn)
空间复杂度:O(1)
因为用s去和jewel比对的时候花费了较多不必要的时间,我们可以针对这一点去优化。比如说找一个看s是不是在jewel里面这种情况是O(1)的数据结构去替代掉原始的jewel。那set就立马跳出来啦,因为我们知道在 x in set的平均时间复杂度是O(1)
快了大概10ms
class Solution:
def numJewelsInStones(self, jewels: str, stones: str) -> int:
jewels = set(jewels)
res = 0
for s in stones:
if s in jewels:
res += 1
return res