LeetCode/1. Two Sum
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:
Can you come up with an algorithm that is less than O(n2) time complexity?
Approach 2: Two-pass Hash Table
Complexity Analysis Time complexity: O(n)
A hash table.
ハッシュ表.勉強するべきでしょう.
In the first iteration, we add each element's value as a key and its index as a value to the hash table
最初の繰り返し文では、
To the hashテーブルは,各データをキー値とし,インデックスを値とする.
Then, in the second iteration, we check if each element's complement (
2番目の繰り返し文では、
検索する数値が存在するかどうかを確認します.
インデックス番号を比較して、異なるデータを確認
If it does exist, we return current element's index and its complement's index. Beware that the complement must not be
存在する場合、
現在回転しているやつと検索する数字のインデックス番号を返します.
Approach 3: One-pass Hash Table
Complexity Analysis Time complexity: O(n)
れんが1枚
range()
整数シーケンスの を生成する.
anobjectにおける物品数 リストのアイテム数のrange!! バイナリファイルにキー値(真/偽)があるかどうかを確認する
1日に1つの道しるべ.
0.2個も解けない
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]
Explanation: 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 <= 10^4
- 10^9 <= nums[i] <= 10^9
- 10^9 <= target <= 10^9
- Only one valid answer exists.
Follow-up:Can you come up with an algorithm that is less than O(n2) time complexity?
Approach 2: Two-pass Hash Table
Complexity Analysis
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
딕셔너리 = {}
for i in range(len(nums)) :
딕셔너리[nums[i]] = i
for i in range(len(nums)) :
찾는값 = target - nums[i]
if 찾는값 in 딕셔너리 and 딕셔너리[찾는값] != i
return [i, 딕셔너리[찾는값]]
What is the best way to maintain a mapping of each element in the array to its index?A hash table.
ハッシュ表.勉強するべきでしょう.
In the first iteration, we add each element's value as a key and its index as a value to the hash table
最初の繰り返し文では、
To the hashテーブルは,各データをキー値とし,インデックスを値とする.
Then, in the second iteration, we check if each element's complement (
target - nums[i]
) exists in the hash table. 2番目の繰り返し文では、
検索する数値が存在するかどうかを確認します.
インデックス番号を比較して、異なるデータを確認
If it does exist, we return current element's index and its complement's index. Beware that the complement must not be
nums[i]
itself!存在する場合、
現在回転しているやつと検索する数字のインデックス番号を返します.
Approach 3: One-pass Hash Table
Complexity Analysis
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
hashmap = {}
for i in range(len(nums)) :
complement = target - nums[i]
if complement in hashmap :
return [i, hashmap[complement]]
hashmap[nums[i]] = i
きれい...れんが1枚
range()
整数シーケンス
for i in range(4):
print(i) #0 1 2 3
for i in range(0, 4):
print(i) # 0 1 2 3
for i in range(2, 6):
print(i) # 2 3 4 5
len()anobject
list = ['a', 'b', 'c', 'd']
printme = len(list)
print(printme) # 4
range(len(リスト))list = ['a', 'b', 'c', 'd']
for i in list :
print(i) # a b c d
for i in range(len(list)) :
print(i) # 0 1 2 3
キーの検索#
찾는key in 딕셔너리
#
if 찾는key in 딕셔너리 : print('있네염')
else : print('없네여')
小毛の子.1日に1つの道しるべ.
0.2個も解けない
Reference
この問題について(LeetCode/1. Two Sum), 我々は、より多くの情報をここで見つけました https://velog.io/@onthekaki/LeetCode1.Two-Sumテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol