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程序也是 ...
随机推荐
- Python --表达式和运算符
表达式 由一个或者几个数字或者变量和运算符组合成的一行代码 通常会返回一个结果 运算符 由一个以上的值经过变化得到新值的过程就叫做运算 用于运算的符号称为运算符 运算符的分类: 算数运算符 比较或者关 ...
- jquery-----ajax的几种方法
$.get(url,function(data,status){ if(status == 'success'){ layer.msg(data,{shift:1,time:2000},functio ...
- storm-redis 详解
多的不说,先来代码分析,再贴我自己写的代码.如果代码有错误,求更正.. 导入两个关键包,其他项目需要的包,大家自己导入了,我pom下的包太多,不好一下扔上来. <dependency> & ...
- vue绑定数据之前 会看到源代码
http://blog.csdn.net/fengjingyu168/article/details/72915468 VUE绑定数据闪现问题 问题描述如下: 1.在HTML中使用Vue为div绑定数 ...
- element-ui的el-table和el-form嵌套使用表单校验
表格中嵌套使用表单验证 表格是el-table自动获取的后台数据,每行都有el-input的验证,这样一个rules的规则就不能匹配到每一行,所以要是用动态的prop和rules规则 需求如下,要对告 ...
- Spring 中基于 AOP 的 @AspectJ
Spring 中基于 AOP 的 @AspectJ @AspectJ 作为通过 Java 5 注释注释的普通的 Java 类,它指的是声明 aspects 的一种风格. 通过在你的基于架构的 XML ...
- 【Java_SSM】(四)Eclipse中通过maven引入jar包
这篇博文我们介绍一下如何通过eclipse配置setting并引入jar包 (1)eclipse:Window--Preferences--Maven--User Setting 全部完成后点Appl ...
- Linux 下三种提高工作效率的文件处理技巧
Linux 下三种提高工作效率的文件处理技巧 在 Linux 下工作,打交道最多的就是文件了,毕竟 Linux 下工作一切皆文件嘛.Linux 也为大家提供了多种用于处理文件的命令,合理使用这些命令可 ...
- 使用flask实现简单的文件上传
from flask import Flask, redirect, render_template, request, url_forfrom werkzeug.utils import secur ...
- windows环境下Kubernetes及Docker安装(那些坑)
k8s 和 Docker容器技术,当前非常流行的技术. 让人日狗的是, 这套技术栈对CN的donet 程序员不怎么友好.娓娓道来,1. 好多镜像都是需要梯子才能访问: 2. window程序员天生 ...