Two Sum Leetcode Java
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].
UPDATE (2016/2/13):
The return format had been changed to zero-based indices. Please read the above updated description carefully.
public class Solution {
public int[] twoSum(int[] nums, int target) {
int[] copy = new int[nums.length];
int index1 = 0;
int index2 = nums.length - 1;
int first = -1;
int second = -1;
int[] result = new int[2];
System.arraycopy(nums, 0, copy, 0, nums.length);
Arrays.sort(copy);
while (index1 < index2) {
if (copy[index1] + copy[index2] == target) {
result[0] = copy[index1];
result[1] = copy[index2];
break;
}else if(copy[index1] + copy[index2] < target){
index1++;
}else{
index2--;
}
}
for(int i = 0; i < nums.length; i++){
if (result[0] == nums[i] && first == -1){
first = i;
}else if (result[1] == nums[i] && second == -1){
second = i;
}
}
result[0] = first;
result[1] = second;
return result;
}
}
Two Sum Leetcode Java的更多相关文章
- 【LeetCode】Path Sum ---------LeetCode java 小结
Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ...
- Minimum Path Sum leetcode java
题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right w ...
- Binary Tree Maximum Path Sum leetcode java
题目: Given a binary tree, find the maximum path sum. The path may start and end at any node in the tr ...
- Path Sum leetcode java
题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...
- 4 Sum leetcode java
题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...
- 3 Sum leetcode java
题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find al ...
- Combination Sum leetcode java
题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C ...
- N-Queens II leetcode java
题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...
- LeetCode算法题-Path Sum(Java实现)
这是悦乐书的第169次更新,第171篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第28题(顺位题号是112).给定二叉树和整数sum,确定树是否具有根到叶路径,使得沿路 ...
随机推荐
- 杂项之pymysql连接池
杂项之pymysql连接池 本节内容 本文的诞生 连接池及单例模式 多线程提升 协程提升 后记 1.本文的诞生 由于前几天接触了pymysql,在测试数据过程中,使用普通的pymysql插入100W条 ...
- SmallPtAMP: C++ AMP implementation of SmallPt / SmallPtGPU
前段时间突然对SmallPtGPU很感兴趣,于是自己也照葫芦画瓢搞了一个C++AMP的版本,整体跟SmallPtGPU基本相同,但是去掉了SmallPtGPU中显式计算直接光照的步骤,因而实现更加接近 ...
- css的继承性
为了减少css的代码量,很多时候,我们必须要非常重视css的继承性 1.首先,有些属性是不能继承的.这没有任何原因,只是因为它就是这么设置的.举个例子来说:border属性,大家都知道,border属 ...
- vue 配置文件详解
var path = require('path'); // NodeJS中的Path对象,用于处理目录的对象,提高开发效率. // 模块导入 module.exports = { // 入口文件地址 ...
- cout格式化输出
问题描述: 有N条绳子, 它们的长度分别为Li. 如果从它们中切割出K条相同的绳子的话,这K条绳子每条最长能有多少? (备注:答案保留两位小数) <1>精确到小数点后两位输出 #inclu ...
- add添加
s = {1,2,3,4,5,6,} 进行添加数据! s = {1,2,3,4,5,6,} s.add('s')#添加字符串's' s.add('3')#添加字符串'3' s.add(3)#添加3和字 ...
- .Net Core Linux centos7行—.net core json 配置文件
.net core 对配置系统做出了大幅度更新,不在局限于之前的*.xml配置方式.现在支持json,xml,ini,in memory,环境变量等等.毫无疑问的是,现在的json配置文件是.net ...
- sys
sys.argv 命令行参数List,第一个元素是程序本身路径 sys.exit(n) 退出程序,正常退出时exit(0) sys.version 获取Python解释程序的版本信息 sys.maxi ...
- Android 摇一摇功能的注意事项
/**开始重力传感器的检测*/ public void start() { // 获得传感器管理器 sensorManager = (SensorManager) mContext.getSystem ...
- Xshell远程管理Linux
Xshell[1]是一个强大的安全终端模拟软件,它支持SSH1, SSH2, 以及Microsoft Windows 平台的TELNET 协议. Xshell 通过互联网到远程主机的安全连接以及它创新 ...