</pre>给一个整数数组,找到两个数使得他们的和等于一个给定的数<span style="box-sizing: border-box;">target</span>。<p></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;">你需要实现的函数<code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6000003814697px; padding: 2px 4px; color: rgb(199, 37, 78); white-space: nowrap; border-radius: 4px; background-color: rgb(249, 242, 244);">twoSum</code>需要返回这两个数的下标, 并且第一个下标小于第二个下标。注意这里下标的范围是 1 到 <span style="box-sizing: border-box;">n</span>,不是以 <code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6000003814697px; padding: 2px 4px; color: rgb(199, 37, 78); white-space: nowrap; border-radius: 4px; background-color: rgb(249, 242, 244);">0</code>开头。</p><div class="alert alert-info" style="box-sizing: border-box; padding: 0px 10px; margin-bottom: 20px; border: 1px solid rgb(188, 232, 241); border-radius: 4px; color: rgb(49, 112, 143); font-size: 12px; box-shadow: rgba(255, 255, 255, 0.2) 0px 1px 0px inset; background-color: rgb(217, 237, 247);"><h5 style="box-sizing: border-box; font-family: inherit; font-weight: 500; line-height: 1.1; color: inherit; margin-top: 10px; margin-bottom: 10px; font-size: 14px;"><span class="fa fa-info" style="box-sizing: border-box; display: inline-block; font-family: FontAwesome; line-height: 1; -webkit-font-smoothing: antialiased; font-stretch: normal;font-size:undefined; text-rendering: auto;"></span> 注意事项</h5><div class="m-t-sm" style="box-sizing: border-box; margin-top: 10px;"><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;">你可以假设只有一组答案。</p></div></div></div><div class="panel-footer bg-light lter" style="box-sizing: border-box; padding: 10px 15px; border-top-width: 1px; border-top-style: solid; border-color: rgb(235, 239, 246); border-radius: 0px 0px 2px 2px; background-color: rgb(249, 250, 252);"><span class="text-sm" style="box-sizing: border-box; font-size: 12px;">您在真实的面试中是否遇到过这个题?</span> <div class="btn-group" data-toggle="buttons" style="box-sizing: border-box; position: relative; display: inline-block; vertical-align: middle;"><div class="btn btn-white btn-xs" id="yes" 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 rgba(150, 160, 180, 0.298039); border-radius: 3px; box-shadow: rgba(0, 0, 0, 0.0470588) 0px -1px 1px inset; position: relative; float: left; margin-left: 0px; background-image: none; background-color: rgb(255, 255, 255); background-clip: padding-box;">Yes</div></div></div></div><div class="m-t-lg m-b-lg" style="box-sizing: border-box; margin-top: 20px; margin-bottom: 20px; color: rgb(113, 113, 113); font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;"><span style="box-sizing: border-box;">样例</span><div class="m-t-sm" style="box-sizing: border-box; margin-top: 10px;"><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;">给出 numbers = <code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6000003814697px; padding: 2px 4px; color: rgb(199, 37, 78); white-space: nowrap; border-radius: 4px; background-color: rgb(249, 242, 244);">[2, 7, 11, 15]</code>, target = <code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6000003814697px; padding: 2px 4px; color: rgb(199, 37, 78); white-space: nowrap; border-radius: 4px; background-color: rgb(249, 242, 244);">9</code>, 返回 <code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6000003814697px; padding: 2px 4px; color: rgb(199, 37, 78); white-space: nowrap; border-radius: 4px; background-color: rgb(249, 242, 244);">[1, 2]</code>.</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;">显然效率越高越好,以空间换时间</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;">Java代码</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;"><pre name="code" class="java">public class Solution {
/*
* @param numbers : An array of Integer
* @param target : target = numbers[index1] + numbers[index2]
* @return : [index1 + 1, index2 + 1] (index1 < index2)
*/
public int[] twoSum(int[] numbers, int target) {
// write your code here
HashMap<Integer,Integer> nums = new HashMap<Integer,Integer>();
for(int i = 0;i <numbers.length; i++){
if(nums.containsKey(numbers[i])){
int index = nums.get(numbers[i]);
int[] res = {++index,i+1};
return res;
}
else {
nums.put(target - numbers[i],i); //当下次target - numbers[i]出现时就是结果
}
}
return null;
}
}
两数之和
最新推荐文章于 2021-12-13 21:16:53 发布