博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode -- Two Sum
阅读量:6375 次
发布时间:2019-06-23

本文共 3447 字,大约阅读时间需要 11 分钟。

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

第一种方案是两重循环,时间复杂度为O(n^2)

 

public class Solution {     public int[] twoSum(int[] numbers, int target) {         // Start typing your Java solution below         // DO NOT write main() function                  //int index1 = 0;         //int index2 = 0;                  int[] results = new int[2];                  for(int i = 0; i < numbers.length - 1; i++){             for(int j = i + 1; j < numbers.length; j++){                 if(numbers[i] + numbers[j] == target){                     results[0] = i + 1;                     results[1] = j + 1;                     return results;                 }             }         }         return results;              } }
1 class Solution { 2 public: 3     vector
twoSum(vector
&numbers, int target) { 4 vector
::iterator first, second; 5 first = numbers.begin(); 6 7 vector
indics; 8 for(;first < numbers.end() - 1; first ++) { 9 for(second = first + 1; second < numbers.end(); second ++) {10 if(*first + *second == target) {11 indics.push_back((first - numbers.begin()) + 1);12 indics.push_back((second - numbers.begin()) + 1);13 }14 }15 } 16 17 return indics;18 }19 };

 

第二种解决方法:hash 将所有元素都遍历一遍放入Map中,再扫描一遍即可 时间复杂度O(n)

1 class Solution { 2 public: 3     vector
twoSum(vector
&numbers, int target) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 map
mapping; 7 vector
result; 8 for(int i = 0; i < numbers.size(); i++){ 9 mapping[numbers[i]] = i;10 }11 12 for(int i = 0; i < numbers.size(); i++){13 int searched = target - numbers[i];14 if(mapping.find(searched) != mapping.end()){15 result.push_back(i + 1);16 result.push_back(mapping[searched] + 1);17 break;18 }19 }20 return result;21 22 }23 };

 

1 class Solution { 2 public: 3     vector
twoSum(vector
&numbers, int target) { 4 vector
::iterator first, second; 5 first = numbers.begin(); 6 7 map
mapping; 8 for(;first < numbers.end(); first ++) { 9 mapping[*first] = (first - numbers.begin()) + 1;10 }11 12 map
::iterator it;13 vector
indics;14 for(first = numbers.begin(); first < numbers.end(); first ++) {15 int remain = target - *first;16 it = mapping.find(remain);17 if(it != mapping.end() && (first - numbers.begin() + 1) != it->second) {18 indics.push_back(first - numbers.begin() + 1);19 indics.push_back(it->second);20 break;21 }22 }23 24 return indics;25 }26 };

 

 

转载地址:http://oztqa.baihongyu.com/

你可能感兴趣的文章
基于Lumisoft.NET组件和.NET API实现邮件发送功能的对比
查看>>
C#数据库访问技术之DATAREADER对象读取数据
查看>>
各种排序方法
查看>>
编译时程序透彻理解异常并合理使用异常
查看>>
2013年5月18日星期六
查看>>
js 字符串操作函数集合
查看>>
nullnullCF 312B(Archer-等比数列极限求和)
查看>>
消息函数windows 程序设计 第三章 (下)
查看>>
java中调用web中的jsp或servlet去通知它们做一些操作
查看>>
Javascript 坦克大战
查看>>
JavaScript自动设置IFrame高度(兼容各主流浏览器)
查看>>
Linux内核中__init, __initdata, __initfunc(), asmlinkage, ENTRY(), FASTCALL()等作用
查看>>
leetcode -- Two Sum
查看>>
Windows多线程
查看>>
Resolve PSExec "Access is denied"
查看>>
C语言局部变量和全局变量问题汇总
查看>>
android 下的网络图片加载
查看>>
Paip.语义分析----情绪情感词汇表总结
查看>>
Linux下软件安装,卸载,管理
查看>>
View Programming Guide for iOS_读书笔记[正在更新……]
查看>>