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))

参考资料