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].

Solution:

public class Solution {
public int[] twoSum(int[] nums, int target) {
int[] result=new int[2];
for(int i=0;i<nums.length-1;i++){
for(int j=i+1;j<nums.length;j++)
{
if(target==nums[i]+nums[j]){
result[0]=i;
result[1]=j;
}
}
}
return result;
} }

一个简单的java程序,两次for循环,时间复杂度是O(n^2),空间复杂度为O(1)。

Leetcode 1——twosum的更多相关文章

  1. LeetCode #1 TwoSum

    Description Given an array of integers, return indices of the two numbers such that they add up to a ...

  2. LeetCode 之 TwoSum

    题目: Given an array of integers, find two numbers such that they add up to a specific target number. ...

  3. leetcode之twosum

    class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { vector& ...

  4. leetcode ----ARRAY TWOSUM

    代码的(判断nums[i]或者是target-nums[i]都可以):

  5. [LeetCode_1] twoSum

    LeetCode: 1. twoSum 题目描述 Given an array of integers, return indices of the two numbers such that the ...

  6. LeetCode 算法题解 js 版 (001 Two Sum)

    LeetCode 算法题解 js 版 (001 Two Sum) 两数之和 https://leetcode.com/problems/two-sum/submissions/ https://lee ...

  7. ANDROID学习书单

    Skip to content PersonalOpen sourceBusinessExplore Sign upSign in PricingBlogSupport   This reposito ...

  8. Android技能树

    第一部分:Android(安卓)Android基础知识Android内存泄漏总结Handler内存泄漏分析及解决Android性能优化ListView详解RecyclerView和ListView的异 ...

  9. LeetCode初体验—twoSum

    今天注册了大名鼎鼎的LeetCode,做了一道最简单的算法题目: Given an array of integers, return indices of the two numbers such ...

随机推荐

  1. CentOS中配置lvm存储

    磁盘添加 vmware workstation 虚拟机为例 1.关闭虚拟机,在虚拟机设置中添加3块硬盘. 2.首先创建物理卷  pvcreate /dev/sdb /dev/sdc Physical ...

  2. PyCharm链接服务器同步代码

    准备工作 1:服务器(本地虚拟安装或是云服务器)这里我使用的是腾讯云服务器 2:PyCharm开发软件 3:XShell 软件 第一步:打开PyCharm如下图新建一个项目 Location:选择代码 ...

  3. 搭建基于eclipse的纯的JavaWeb项目

    -----------2016.9.9--------------------- 步骤: 1.搭建一个Java项目 2,字该项目下新建一个文件夹,表示根,名字为webapp(name随意) 3,在we ...

  4. 【NOIP2009】【CJOJ1687】【洛谷1074】靶形数独

    题面 Description 小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们向 Z博士请教,Z 博士拿出 ...

  5. 清橙A1212:剪枝

    题面 清橙 Sol 一种新的树上\(DP\)姿势 从左往右按链\(DP\) 做法: 维护两个栈\(S1\),\(S2\) \(S1\)存当前的链 \(S2\)存分叉点以下要改的链 \(Dfs\),弄一 ...

  6. Developer Survey Results 2017

    概观 今年,超过64,000名开发人员告诉我们他们学习和升级的方式,他们使用的工具和他们想要的东西. 自2011年以来,Stack Overflow每年都会向开发者询问他们最喜爱的技术,编码习惯,工作 ...

  7. 【Learning】带花树——一般图最大匹配

    一般图最大匹配--带花树 问题 ​ 给定一个图,求该图的最大匹配.即找到最多的边,使得每个点至多属于一条边. ​ 这个问题的退化版本就是二分图最大匹配. ​ 由于二分图中不存在奇环,偶环对最大匹配并无 ...

  8. oracle 12c 安装指南(各种问题总结)

    下一学期要学习数据库的知识,趁着敲代码之余,安装著名的oracle来放松一下 1.首先从官网下载安装包http://www.oracle.com/technetwork/database/enterp ...

  9. java 多态 ---父类调用子类方法

    package test1;//多态的体现import javax.print.attribute.standard.RequestingUserName;import java.util.Scann ...

  10. 洛谷 P1017 进制转换

    推荐洛谷 题目描述 我们可以用这样的方式来表示一个十进制数: 将每个阿拉伯数字乘以一个以该数字所处位置的(值减1)为指数,以10为底数的幂之和的形式.例如:123可表示为 1*10^2+2*10^1+ ...