Java实现LeetCode_0001_Two Sum
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的更多相关文章
- 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 ...
- 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 ...
- C#与Java对比学习:数据类型、集合类、栈与队列、迭达、可变参数、枚举
数据类型: C#:String与StringBuilder Java:String与StringBuffer 第一个不习惯是string的第一个字母必须大写了. 第二个不习惯是int得写成Intege ...
- Java中的java.math.BigInteger
Java中的java.math.BigInteger /** * */ package com.you.model; /** * @author YouHaidong * */ public clas ...
- 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 ...
- 实现java随机数Random的几招
一,在java.util这个包里面提供了一个Random的类,我们可以新建一个Random的对象来产生随机数,可以产生随机整数.随机float.随机double,随机long,这个也是我们经常用的一个 ...
- 20155303 2016-2017-2 《Java程序设计》第一周学习总结
20155303 2016-2017-2 <Java程序设计>第一周学习总结 教材学习内容总结 浏览教材,根据自己的理解每章提出一个问题 Chapter1 Java平台概论:MyProgr ...
- Java学习---InetAddress类的学习
基础知识 1.InetAddress类 在网络API套接字,InetAddress类和它的子类型对象使用域名DNS系统,处理主机名到主机IPv4或IPv6地址的转换.如图1-1所示. 由于InetAd ...
- 用GDB 调试Java程序
陈皓 http://blog.csdn.net/haoel 背景 想要使用GDB调试程序,就需要用GNU的编译器编译程序.如:用GCC编译的C/C++的程序,才能用GDB调试.对于Java程序也是 ...
随机推荐
- [c++ IO加速]快速输入输出
自己封装的FastIO类,效率虽有所损失,不过实用性提高很多. 测试,写10000000个整数(86M): printf 2.7s cout 27s FastIO 1s 测试,读10000000个整数 ...
- Druid 0.17入门(4)—— 数据查询方式大全
本文介绍Druid查询数据的方式,首先我们保证数据已经成功载入. Druid查询基于HTTP,Druid提供了查询视图,并对结果进行了格式化. Druid提供了三种查询方式,SQL,原生JSON,CU ...
- Intellij Idea2018破解教程(激活到2099年)
1.下载IntelliJ IDEA 2018.1.6 链接:https://pan.baidu.com/s/18ZcKiPp3LU5S-la10r5icw 提取码:ghn0 2.安装IntelliJ ...
- CentOS 7.1 图形化安装
1.在命令行下输入下面的命令来安装 Gnome 包 sudo yum groupinstall "GNOME Desktop" "Graphical Administr ...
- Pytorch数据集读取
Pytorch中数据集读取 在机器学习中,有很多形式的数据,我们就以最常用的几种来看: 在Pytorch中,他自带了很多数据集,比如MNIST.CIFAR10等,这些自带的数据集获得和读取十分简便: ...
- 判断iptables是否运行的一些探索
现在有这么一个需求,要判断iptables是否开启.咋看比较难以入手,那么可以想,怎么启动iptables,redhat下很容易联想到/etc/init.d/iptabes start . 我们可以来 ...
- 解决yum 问题
Dependencies Resolved Traceback (most recent call last): File "/usr/bin/yum", line 29, in ...
- POJ2516
题目链接:http://poj.org/problem?id=2516 解题思路: 最小费用最大流,这个没什么疑问.但此题小难点在于读题,大难点在于建图. 首先,供应量小于需求量的时候直接输出“-1” ...
- tomcat关于配置servlet的url-pattern的问题详解
目录 1 servlet url-pattern的匹配问题 1.1 精确匹配 1.2 路径匹配 1.3 后缀匹配 注意:路径和后缀匹配无法同时设置 2 url-pattern中/和/*的区别 3 ur ...
- NO.4 CCS运行第一个demo(本地)
前面介绍了基本的SDK内容,这次主要是本地实际应用CCS实现程序的运行. 首先我们进入CCS,我简单介绍下界面: 界面很简洁,通俗易懂(怎么跟STM32IDE这么像) 由于我们还不会写程序,我们先导入 ...