leetcode: Two Sum - 点缀星辰 - ITeye技术网站
Given an array of integers, 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 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
You may assume that each input would have exactly one solution.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
原问题链接:https://oj.leetcode.com/problems/two-sum/
问题分析
这个问题相对来说比较简单,在给定一组数字的时候,我们需要去找两个数字之和为给定数字的一个组合。所以从最开始来说,一个最简单直接的方法就是二重循环遍历数组,找到匹配的就返回。这种方法的实现如下:
Read full article from leetcode: Two Sum - 点缀星辰 - ITeye技术网站
No comments:
Post a Comment