[READ-ONLY] Mirror of https://github.com/shuuji3/leetcode. 馃搩 Code submitted to LeetCode
leetcode.com/shuuji3/
leetcode
leetcode-solutions
876 B
39 lines
1/*
2 * @lc app=leetcode id=1 lang=golang
3 *
4 * [1] Two Sum
5 *
6 * https://leetcode.com/problems/two-sum/description/
7 *
8 * algorithms
9 * Easy (44.85%)
10 * Total Accepted: 2.4M
11 * Total Submissions: 5.3M
12 * Testcase Example: '[2,7,11,15]\n9'
13 *
14 * Given an array of integers, return indices of the two numbers such that they
15 * add up to a specific target.
16 *
17 * You may assume that each input would have exactly one solution, and you may
18 * not use the same element twice.
19 *
20 * Example:
21 *
22 *
23 * Given nums = [2, 7, 11, 15], target = 9,
24 *
25 * Because nums[0] + nums[1] = 2 + 7 = 9,
26 * return [0, 1].
27 *
28 *
29 */
30func twoSum(nums []int, target int) []int {
31 for i := 0; i < len(nums)-1; i++ {
32 for j := i+1; j < len(nums); j++ {
33 if nums[i] + nums[j] == target {
34 return []int{i, j}
35 }
36 }
37 }
38 return []int{}
39}