mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-11-04 14:18:20 +08:00 
			
		
		
		
	* docs: add Japanese documents (`ja/docs`) * docs: add Japanese documents (`ja/codes`) * docs: add Japanese documents * Remove pythontutor blocks in ja/ * Add an empty at the end of each markdown file. * Add the missing figures (use the English version temporarily). * Add index.md for Japanese version. * Add index.html for Japanese version. * Add missing index.assets * Fix backtracking_algorithm.md for Japanese version. * Add avatar_eltociear.jpg. Fix image links on the Japanese landing page. * Add the Japanese banner. --------- Co-authored-by: krahets <krahets@163.com>
		
			
				
	
	
		
			42 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			42 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
"""
 | 
						|
File: two_sum.py
 | 
						|
Created Time: 2022-11-25
 | 
						|
Author: krahets (krahets@163.com)
 | 
						|
"""
 | 
						|
 | 
						|
 | 
						|
def two_sum_brute_force(nums: list[int], target: int) -> list[int]:
 | 
						|
    """方法一:ブルートフォース列挙"""
 | 
						|
    # 二重ループ、時間計算量は O(n^2)
 | 
						|
    for i in range(len(nums) - 1):
 | 
						|
        for j in range(i + 1, len(nums)):
 | 
						|
            if nums[i] + nums[j] == target:
 | 
						|
                return [i, j]
 | 
						|
    return []
 | 
						|
 | 
						|
 | 
						|
def two_sum_hash_table(nums: list[int], target: int) -> list[int]:
 | 
						|
    """方法二:補助ハッシュテーブル"""
 | 
						|
    # 補助ハッシュテーブル、空間計算量は O(n)
 | 
						|
    dic = {}
 | 
						|
    # 単一ループ、時間計算量は O(n)
 | 
						|
    for i in range(len(nums)):
 | 
						|
        if target - nums[i] in dic:
 | 
						|
            return [dic[target - nums[i]], i]
 | 
						|
        dic[nums[i]] = i
 | 
						|
    return []
 | 
						|
 | 
						|
 | 
						|
"""ドライバーコード"""
 | 
						|
if __name__ == "__main__":
 | 
						|
    # ======= テストケース =======
 | 
						|
    nums = [2, 7, 11, 15]
 | 
						|
    target = 13
 | 
						|
 | 
						|
    # ====== ドライバーコード ======
 | 
						|
    # 方法一
 | 
						|
    res: list[int] = two_sum_brute_force(nums, target)
 | 
						|
    print("方法一の結果 =", res)
 | 
						|
    # 方法二
 | 
						|
    res: list[int] = two_sum_hash_table(nums, target)
 | 
						|
    print("方法二の結果 =", res) |