Given an array of integers, find two numbers such that they add up to a
</pre> specific target number.</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;">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.</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;">You may assume that each input would have exactly one solution.</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; font-family: monospace;"><span style="box-sizing: border-box; font-weight: 700;">Input:</span> numbers={2, 7, 11, 15}, target=9<br style="box-sizing: border-box;" /><span style="box-sizing: border-box; font-weight: 700;">Output:</span> index1=1, index2=2</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;"></p><div id="tags" class="btn btn-xs btn-warning" style="box-sizing: border-box; display: inline-block; padding: 1px 5px; margin-bottom: 0px; font-size: 12px; line-height: 1.5; text-align: center; white-space: nowrap; vertical-align: middle; cursor: pointer; -webkit-user-select: none; border: 1px solid rgb(238, 162, 54); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; color: rgb(255, 255, 255); width: 80px; background-image: none; background-color: rgb(240, 173, 78);">Show Tags</div></div></div></div></div></div><div id="interviewed_div" style="box-sizing: border-box; padding-left: 20px; padding-top: 12px; padding-bottom: 2px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;"><form id="interviewed_form" method="post" action="https://leetcode.com/interviewed/" class="ng-pristine ng-valid" style="box-sizing: border-box;"></form><div id="survey" style="box-sizing: border-box;"><div id="general_questions" style="box-sizing: border-box;"><span style="box-sizing: border-box;">Have you met this question in a real interview?</span> <span class="text-info chosen_option text-bold" style="box-sizing: border-box; color: rgb(49, 112, 143); font-weight: bold;"></span><span id="yes_or_no_options" style="box-sizing: border-box;"></span><div value="1" id="yes" follow_up="#yes_questions" class="submit btn btn-default btn-xs" style="box-sizing: border-box; display: inline-block; padding: 1px 5px; margin-bottom: 0px; line-height: 1.5; text-align: center; white-space: nowrap; vertical-align: middle; cursor: pointer; -webkit-user-select: none; border: 1px solid rgb(204, 204, 204); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; background-image: none;">Yes</div> <div value="2" id="no" class="submit btn btn-default btn-xs" follow_up="#no_questions" style="box-sizing: border-box; display: inline-block; padding: 1px 5px; margin-bottom: 0px; line-height: 1.5; text-align: center; white-space: nowrap; vertical-align: middle; cursor: pointer; -webkit-user-select: none; border: 1px solid rgb(204, 204, 204); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; background-image: none;">No</div></div></div><div id="interviewed_state" style="box-sizing: border-box; line-height: 22px;"></div></div><p></p><p class="action" style="box-sizing: border-box; margin-top: 12px; margin-bottom: 10px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;"><a target=_blank class="btn btn-success btn-pad" href="https://leetcode.com/discuss/questions/oj/two-sum" style="box-sizing: border-box; color: rgb(255, 255, 255); text-decoration: none; display: inline-block; padding: 6px 20px; margin-bottom: 0px; font-size: 16px; line-height: 1.42857143; text-align: center; white-space: nowrap; vertical-align: middle; cursor: pointer; -webkit-user-select: none; border: 1px solid rgb(76, 174, 76); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background: none 0px 0px rgb(92, 184, 92);">Discuss</a></p><p class="action" style="box-sizing: border-box; margin-top: 12px; margin-bottom: 10px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;">最近又重新开始刷leetcode的题话不多说,直接上代码---第一题Two Sum</p>其实就是简单的判断数组中两个数组相加是否等于一个数,但是不要想得太简单,因为如果用两次循环是行不通的,<p><pre name="code" class="java"><pre name="code" class="java">/*
* n2的复杂度
public int[] twoSum(int[] numbers, int target)
{ int i,j;
int [] sum=new int[2];
for(i=0;i<numbers.length;i++)
{
for(j=i+1;j<numbers.length;j++)
{
if(numbers[i]+numbers[j]==target)
{
sum[0]=i;
sum[1]=j;
}
}
}
return sum;
}
我试过只有O(n)才行,主要就是用一个哈希表实现一次循环判断。
//n的复杂度
public static int[] twoSum(int[] numbers, int target)
{
HashMap<Integer,Integer> hashMap=new HashMap();
for(int i=0;i<numbers.length;i++)
{ boolean ishave=false;
if( hashMap.get(numbers[i])==null)
{
hashMap.put(numbers[i], i);
ishave=true;
}
if(hashMap.get(target-numbers[i])!=null)
{
if(i!=hashMap.get(target-numbers[i]))
{
numbers[1]=i+1;
numbers[0]=hashMap.get(target-numbers[i])+1;
}
}
}
return numbers;
}