import java.util.Arrays;
import java.util.Scanner; public class TwoSum_1 {
public static void main(String[] args) {
@SuppressWarnings({ "resource"})
Scanner input = new Scanner(System.in);
System.out.println("Please input the array's length:");
int length=input.nextInt();
int []nums=new int[length];
System.out.println("Please input the array elements:");
for(int i=0;i<nums.length;i++) {
nums[i]=input.nextInt();
}//end for
System.out.println("Please input the target:");
int target=input.nextInt();
System.out.println(Arrays.toString(twoSum(nums,target)));
}// end main() /**
*
*the function of judge
*@param
*
* */
public static int[] twoSum(int[] nums, int target) {
int i, j; // index
for (i = 0; i < nums.length; i++) {
for (j = i+1; j < nums.length; j++) {
if (target - nums[j] == nums[i]) {
return new int[] {i,j};
} // end if
} // end for
} // end for
throw new IllegalArgumentException("No two sum solution");
}// end twoSum() }// end TwoSum_1

Java实现LeetCode_0001_Two Sum的更多相关文章

  1. LeetCode第[1]题(Java):Two Sum 标签:Array

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  2. LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  3. C#与Java对比学习:数据类型、集合类、栈与队列、迭达、可变参数、枚举

    数据类型: C#:String与StringBuilder Java:String与StringBuffer 第一个不习惯是string的第一个字母必须大写了. 第二个不习惯是int得写成Intege ...

  4. Java中的java.math.BigInteger

    Java中的java.math.BigInteger /** * */ package com.you.model; /** * @author YouHaidong * */ public clas ...

  5. Atitit s2018 s3 doc list alldvc.docx .docx s2018 s3f doc compc s2018 s3f doc homepc sum doc dvcCompc dtS312 s2018 s3f doc compc\Atitit PathUtil 工具新特性新版本 v8 s312.docx s2018 s3f doc compc\Atitit 操作日

    Atitit s2018 s3 doc list alldvc.docx .docx s2018 s3f doc compc s2018 s3f doc homepc sum doc dvcCompc ...

  6. 实现java随机数Random的几招

    一,在java.util这个包里面提供了一个Random的类,我们可以新建一个Random的对象来产生随机数,可以产生随机整数.随机float.随机double,随机long,这个也是我们经常用的一个 ...

  7. 20155303 2016-2017-2 《Java程序设计》第一周学习总结

    20155303 2016-2017-2 <Java程序设计>第一周学习总结 教材学习内容总结 浏览教材,根据自己的理解每章提出一个问题 Chapter1 Java平台概论:MyProgr ...

  8. Java学习---InetAddress类的学习

    基础知识 1.InetAddress类 在网络API套接字,InetAddress类和它的子类型对象使用域名DNS系统,处理主机名到主机IPv4或IPv6地址的转换.如图1-1所示. 由于InetAd ...

  9. 用GDB 调试Java程序

      陈皓 http://blog.csdn.net/haoel 背景 想要使用GDB调试程序,就需要用GNU的编译器编译程序.如:用GCC编译的C/C++的程序,才能用GDB调试.对于Java程序也是 ...

随机推荐

  1. [csu/coj 1079]树上路径查询 LCA

    题意:询问树上从u到v的路径是否经过k 思路:把树dfs转化为有根树后,对于u,v的路径而言,设p为u,v的最近公共祖先,u到v的路径必定是可以看成两条路径的组合,u->p,v->p,这样 ...

  2. 基于R语言的航空公司客户价值分析

    分析航空公司现状 1.行业内竞争 民航的竞争除了三大航空公司之间的竞争之外,还将加入新崛起的各类小型航空公司.民营航空公司,甚至国外航空巨头.航空产品生产过剩,产品同质化特征愈加明显,于是航空公司从价 ...

  3. HDU 2016 (水)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2016 题目大意:给你 n 个数,把最小的数和第一个数字互换,然后输出 解题思路: 很水,开数组,遍历并 ...

  4. 两种方式实现sticky footer绝对底部

    一.什么是sticky footer 如果页面内容不够长的时候,页脚块粘贴在视窗底部:如果内容足够长时,页脚块会被内容向下推送,我们看到的效果就如下面两张图这样.这种效果基本是无处不在的,很受欢迎. ...

  5. 深入理解JS中的对象(三):class 的工作原理

    目录 序言 class 是一个特殊的函数 class 的工作原理 class 继承的原型链关系 参考 1.序言 ECMAScript 2015(ES6) 中引入的 JavaScript 类实质上是 J ...

  6. es 报错cannot allocate because allocation is not permitted to any of the nodes

    0.现象 es 集群报red ,有unassigned shared , 用命令 curl localhost:9200/_cat/shards |grep UNASSIGNED 可以查看. 即使你马 ...

  7. matlab第六章数据分析与多项式计算

    MATLAB练习 第六章数据分析与多项式计算 1.max和min 1.分别求矩阵A中各列和各行元素中的最大值.max和min的用法一样 % [例6.1]分别求矩阵中各列和各行元素中的最大值. A=[5 ...

  8. UVa 11529

    题目大意:见刘汝佳<算法竞赛入门经典——训练指南>P173 解题思路: 先求出对于每一个点,有多少个三角形包含它,把各个点得到的数值加起来的总和除以 C[n][3] 即可得出答案.对于每一 ...

  9. Angular SPA基于Ocelot API网关与IdentityServer4的身份认证与授权(一)

    好吧,这个题目我也想了很久,不知道如何用最简单的几个字来概括这篇文章,原本打算取名<Angular单页面应用基于Ocelot API网关与IdentityServer4+ASP.NET Iden ...

  10. poi——读取excel数据

    单元格类型 读取Excel数据 package com.java.test.poi; import java.io.File; import java.io.FileInputStream; impo ...