指定数组的下标,如果数组元素等于指定值
2016-12-17 13:15
169 查看
Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution.Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
package com.dengpf.TwoNumbers; /** * Created by kobe73er on 16/12/17. */ public class Solution { public int[] twoSum(int[] nums, int target) { int[] indexs = new int[2]; int i = 0; for (; i < nums.length; i++) { int j = i + 1; for (; j < nums.length;) { if ((nums[i] + nums[j]) == target) { indexs[0] = i; indexs[1] = j; return indexs; } j++; } } return indexs; } public static void main(String args[]) { int[] nums = {-3,4,3,90}; Solution solution = new Solution(); int[] result = solution.twoSum(nums, 0); for (int i = 0; i < result.length; i++) { System.out.println(result[i]); } } }
相关文章推荐
- WinEdt && LaTex(五)—— 内容的排版
- Apache+Tomcat配置集群组
- 通过Javascript 对 CSS属性的设置 与 获取
- Springmvc构造RESTful详细讲解
- 读《从Paxos到Zookeeper 分布式一致性原理与实践》笔记之服务器角色介绍
- dedecms的一点小问题总结
- svn diff-r参数的使用
- Core java第九版第四章对象与类
- Git 在团队中的最佳实践--如何正确使用Git Flow
- hadoop 中hdfs-site.xml RPC HTTP TCP/IP 服务器的用途
- 六大Web负载均衡原理与实现
- 初学My Batis之入门
- 加法运算替换
- apache整合tomcat部署集群
- rsync 服务快速部署手册
- LeetCode 350.Intersection_of_TwoArrays2
- [置顶] 【交流平台】欢迎加入“视频开发与编码技术”QQ群
- APP UI设计及切图规范
- codeforce 742 D. Arpa's weak amphitheater and Mehrdad's valuable Hoses (分组背包+并查集))
- java日常笔记2016-12-17