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程序也是 ...
随机推荐
- 错误 在应用程序级别之外使用注册为 allowDefinition='MachineToApplic
错误 在应用程序级别之外使用注册为 allowDefinition='MachineToApplication' 的节是错误的.如果在 IIS 中没有将虚拟目录配置为应用程序,则可能导致此错误. 如果 ...
- 黑马程序员_毕向东_Java基础视频教程——进制的相互转换(随笔)
进制的相互转换 二进制转十进制: 原理对十进制数进行除2运算(余数不是0 就是1) 6 的二进制: 6 / 2 = 3--0 3 / 2 = 1--1 1 / 2 = 0--1 余数倒序排列输出:11 ...
- ip变动时 wordpress 需要修改一个东西。
搞了大半天,还把mysql 重装了一遍,装的过程中还有这样那样的问题.直到今天总算把网页搞正常了. 主机从6楼搬下来以后,ip就变了.但是很奇怪,用新ip 去访问,能访问,但总觉得很卡的样子.然后会跳 ...
- 实现es6中的set和map
转载自: https://www.cnblogs.com/hui-fly/p/9459152.html https://blog.csdn.net/roamingcode/article/detail ...
- 复习webpack的常用loader
今天复习了下webpack的常用loaders,其实习惯ES6开发的话,webpack的config.js基础配置应该是比较固定: 首先是JS,我们ES6要转为ES5,需要用到babel转码: 1. ...
- vue的slot
1.明确一点:分发内容是在父作用域内编译: 2.slot作为备用内容的条件:宿主元素为空且父元素没有要分发的内容. 3.具名slot:<slot name="XXX"> ...
- Template模式C++实现
#include <iostream> using namespace std; class AbstractClass { public: void TemplateMethod() { ...
- Jquery动画,排队与并发
一.事件绑定 1.鼠标事件:模拟触发 什么是模拟触发? 虽然没有点在按钮上,也可以触发按钮的事件处理函数. 如何:$元素.trigger("事件名") 即使没有点在指定的元素上,也 ...
- 爬虫之requests的请求与响应
requests是基于urllib3的一个用于发起http请求的库(中文文档)数据采集流程: 指定url>> 基于 requests模块发起请求>> 获取响应中的数据>& ...
- Postgres基础操作
显示数据库\l \l+ dw=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges ...