0
0

LeetCodeでコーディング試験対策#2

Last updated at Posted at 2024-04-08

解いた問題

自力で解けたか

はい

解説・学んだところ

自分の解答

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        
        ans = []
        for i in range(len(nums)):
            for j in range(len(nums)):
                if i == j:
                    continue
                
                if nums[i] + nums[j] != target:
                    continue
                ans.append(i)
                ans.append(j)
                break
            if ans != []:
                break
        
        return ans

模範解答

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        for i in range(len(nums)):
            for j in range(i + 1, len(nums)):
                if nums[j] == target - nums[i]:
                    return [i, j]

感想

  • 答えが出たらそのままリターンすればよかった
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0