Two Sum
来源:互联网 发布:软件开发工具包下载 编辑:程序博客网 时间:2024/05/12 10:10
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
public class Solution { public int[] twoSum(int[] nums, int target) { int index1,index2; int length = nums.length; int[] index = new int[2]; for(index1 = 0; index1 < length; index1++) { for(index2 = index1 + 1; index2 < length; index2++) { if(nums[index1] + nums[index2] == target) { index[0] = index1 + 1; index[1] = index2 + 1; return index; } } } }}
public class Solution { public int[] twoSum(int[] nums, int target) { int index1,index2; int length = nums.length; int[] index = new int[2]; for(index1 = 0; index1 < length; index1++) { for(index2 = index1 + 1; index2 < length; index2++) { if(nums[index1] + nums[index2] == target) { index[0] = index1 + 1; index[1] = index2 + 1; break; } } } return index; }}
其他的方法以后慢慢补充
0 0
- Two Sum
- Two Sum
- Two Sum
- two sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Android打包失败Proguard returned with error code 1. See console
- 2015年最新苹果开发者账号注册流程详解
- $().each和$.each()的区别和用法
- android 内存溢出定位及解决
- Dijkstra算法
- Two Sum
- Android轻量级ORM框架ActiveAndroid入门教程(转)
- soundtouch变速wsola算法之改进
- iOS开发过程中的各种坑
- PLSQL Developer 的安装与使用,各种详细配置!(mss)
- android加载大量大图导致OOM处理方法
- Google Protocol Buffer 的使用和原理
- 单词匹配算法
- java中接口的定义与实现