算法(两数之和)

func twoSum(nums []int, target int) []int {
    array1 := make([]int, 0)
    final := 0
    for i := 0; i < len(nums); i++ {
        for j := 0; j < len(nums); j ++ {
            if (nums[i] + nums[j] == target && i != j) {
                array1 = append(array1, i)
                array1 = append(array1, j)
                final = 1;
            }
        }
        if (final == 1) {
            break;
        }
    }
    return array1
}
func twoSum(nums []int, target int) []int {
    m := make(map[int]int)
    for i , v := range nums {
        if k , ok := m[target - v]; ok {
            return []int{i, k}
        }
        m[v] = i
    }
    return nil
}

给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。

你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。

示例:

给定 nums = [2, 7, 11, 15], target = 9

因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/two-sum
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

原文地址:https://www.cnblogs.com/cjjjj/p/12444209.html

时间: 2024-07-30 04:50:54

算法(两数之和)的相关文章

算法练习:两数之和

题目:给定一个整型数组,是否能找出两个数使其和为指定的某个值?注:整型数组中不存在相同的数. 一.解题方法 1.暴力破解法(时间复杂度O(n^2) ) 这是最容易想到的一种方法,即使用两层循环,从数组里取出一个数,然后在此数之后部分找出另外一个数,计算两数之和,判断是否等于指定值.如下: //直观的办法,使用两个循环 bool IsExistSumOfTwoNum( int nArray[], int nCount, int nSum ) { bool bRet = false; for ( i

[算法] LeetCode 1.两数之和

LeetCode 1.两数之和(python) 1.朴素解法 最朴素的两个for循环大法: 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[i] + nums[j] == target: return [i, j] 但注意,不要用enumerate函数写,会超时

前端与算法 leetcode 1. 两数之和

目录 # 前端与算法 leetcode 1. 两数之和 题目描述 概要 提示 解析 解法一:暴力法 解法二:HashMap法 算法 传入[2,7,11,1,12,34,4,15],19的运行结果 执行结果 GitHub仓库 # 前端与算法 leetcode 1. 两数之和 题目描述 给定一个整数数组 nums?和一个目标值 target,请你在该数组中找出和为目标值的那?两个?整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 示例: 给

LeetCode算法题——两数之和(python)

两数之和: 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数.你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用.例如:给定 nums = [2, 7, 11, 15], target = 9.由nums[0] + nums[1] = 2 + 7 = 9,所以返回 [0, 1] 原文地址:http://blog.51cto.com/13921683/2318829

【数据结构与算法】双指针思想——两数之和

两数之和 II - 输入有序数组 LeetCode:两数之和 II - 输入有序数组 题目描述: 给定一个已按照升序排列?的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1?必须小于?index2. 示例: 输入: numbers = [2, 7, 11, 15], target = 9 输出: [1,2] 解释: 2 与 7 之和等于目标数 9 .因此 index1 = 1, index2 = 2 . 思想: 使用双

给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X

题目:给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X 思路一: 1,先采用归并排序对这个数组排序, 2,然后寻找相邻<k,i>的两数之和sum,找到恰好sum>x的位置,如果sum=x则返回true, 3,找到位置后,保持i不变,从k处向前遍历,直到找到A[k]+A[i]等于x,并返回TRUE,如果找不到,则返回false. 论证步骤3:当前找到的位置恰好A[k]+A[i]>x,且前一位置的sum<x: 所以A[i]前面的数(不包括A[i])无论取哪两个数都

LeetCode 167. Two Sum II - Input array is sorted (两数之和之二 - 输入的是有序数组)

Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 m

【LeetCode】 两数之和 twoSum

两数之和 (简单) 题目描述 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数: 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 例如: 给定 nums = [2,7,11,15] ,target = 9 因为 nums[0] + nums[1] = 9: 因此返回 [0,1]: v1.0代码如下: 正数.0或重复值通过测试: 异常用例: [-1, -2, -3, -4, -5] -8; 输出 [] /** * @param {number[]} nums * @par

LeetCode167 两数之和 II - 输入有序数组

给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明: 返回的下标值(index1 和 index2)不是从零开始的. 你可以假设每个输入只对应唯一的答案,而且你不可以重复使用相同的元素. 示例: 输入: numbers = [2, 7, 11, 15], target = 9 输出: [1,2] 解释: 2 与 7 之和等于目标数 9 .因此 index1 = 1

Leetcode(1)两数之和

Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 第一种方法:暴力 执行用时:5352 ms: 内存消耗:12.9MB 效果:非常差 class Solution(object): def twoSum(self, nums, target): """ :type nums: