LeetCodeアルゴリズム:2つのSum
3456 ワード
https://leetcode.com/problems/two-sum/
)
Given an array of integers
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:を抽出し、2つの数の和は このため、リスト内の2つの異なる数値をダブルfor文で順次比較し、インデックス値をリストに戻すことができます. は2つの異なる数ではない同じ値を加算できないので、iとjの値が同じであれば、
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:
Output: [0,1]
Output: Because nums[0] + nums[1] == 9, we return [0, 1].
Example 2:Output: [1,2]
Example 3:Output: [0,1]```
プール)class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
for i in range(len(nums)):
for j in range(len(nums)):
if i == j:
continue
if nums[i] + nums[j] == target:
num_index = [i, j]
return num_index
2つのtarget
に等しい.continue
を通って次のfor文iterに入ります.Reference
この問題について(LeetCodeアルゴリズム:2つのSum), 我々は、より多くの情報をここで見つけました https://velog.io/@tk_kim/LeetCode-알고리즘-Two-Sumテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol