1. Two Sum

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.

傳入 數字列表最後答案加總數值,回傳 最後答案加總數值數字列表 的第幾個跟第幾個數字的加總

答案

Golang

func twoSum(nums_list []int, final_sum_value int) []int {
	// 數字反向對應表
	num_reverse_mapping := make(map[int]int)
	for key, value := range nums_list {
		// 判斷是否其他數值存在
		if other_value_key, is_other_value_exist := num_reverse_mapping[final_sum_value-value]; is_other_value_exist {
			// 回傳其他數值鍵值 + 目前數值鍵值
			return []int{other_value_key, key}
		}
		// 設定數值反向對應表:數值 => 鍵值
		num_reverse_mapping[value] = key
	}
	return nil
}

完整程式碼

package main

import (
	"fmt"
)

type question struct {
	// 參數
	parameter
	// 答案
	answer
}

// 參數
type parameter struct {
	nums_list       []int
	final_sum_value int
}

// 答案
type answer struct {
	one []int
}

func main() {

	question_list := []question{
		{
			parameter{[]int{3, 2, 4}, 6},
			answer{[]int{1, 2}},
		},

		{
			parameter{[]int{3, 2, 4}, 5},
			answer{[]int{0, 1}},
		},

		{
			parameter{[]int{0, 8, 7, 3, 3, 4, 2}, 11},
			answer{[]int{1, 3}},
		},

		{
			parameter{[]int{0, 1}, 1},
			answer{[]int{0, 1}},
		},

		{
			parameter{[]int{0, 3}, 5},
			answer{[]int{}},
		},
	}

	fmt.Printf("------------------------Leetcode Problem 1------------------------\n")
	for _, question := range question_list {
		// _, p := question.answer, question.parameter
		param := question.parameter
		fmt.Printf("【input】:%v       【output】:%v\n", param, twoSum(param.nums_list, param.final_sum_value))
	}
}

func twoSum(nums_list []int, final_sum_value int) []int {
	// 數字反向對應表
	num_reverse_mapping := make(map[int]int)
	for key, value := range nums_list {
		// 判斷是否其他數值存在
		if other_value_key, is_other_value_exist := num_reverse_mapping[final_sum_value-value]; is_other_value_exist {
			// 回傳其他數值鍵值 + 目前數值鍵值
			return []int{other_value_key, key}
		}
		// 設定數值反向對應表:數值 => 鍵值
		num_reverse_mapping[value] = key
	}
	return nil
}

Python

from typing import List


class Solution:
    def twoSum(self, nums_list: List[int], final_sum_value: int) -> List[int]:
        # 數字反向對應表
        num_reverse_mapping = {}

        # 列舉數字清單
        enumerate_nums_list = enumerate(nums_list)

        for current_num_key, current_num_value in enumerate_nums_list:
            # 其他鍵值數值
            other_value = final_sum_value - current_num_value
            # 數值是否存在反轉對應表
            is_other_value_exist = (other_value in num_reverse_mapping)

            if is_other_value_exist:
                # 若其他數值有存在反轉表,回傳 key 值
                other_num_key = num_reverse_mapping[other_value]
                return [other_num_key, current_num_key]

            # 沒有找到數值,將數值位置 key 記錄下來
            num_reverse_mapping[current_num_value] = current_num_key


if __name__ == '__main__':
    # begin
    s = Solution()
    print(s.twoSum([3, 2, 4], 6))
    print(s.twoSum([3, 2, 4], 7))

參考資料