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. CSS的继承性与优先级

    一.CSS的继承性 在CSS中不可继承的属性:display.margin.padding.border.background.width.min-width.max-width.height.min ...

  2. PHP 数组模糊查询

    function search() { $a=array( '0' => array('id'=>1,'pid'=>0,'name'=>'水果'), '1' => arr ...

  3. Bzoj4916: 神犇和蒟蒻

    题面 传送门 Sol 第一问puts("1") 第二问,\(\varphi(i^2)=i\varphi(i)\) 设\(\phi(n)=\sum_{i=1}^{n}i\varphi ...

  4. 【2016北京集训测试赛】river

    HINT 注意是全程不能经过两个相同的景点,并且一天的开始和结束不能用同样的交通方式. [吐槽] 嗯..看到这题的想法的话..先想到了每个点的度为2,然后就有点不知所措了 隐隐约约想到了网络流,但并没 ...

  5. java微信公众号开发token验证失败的问题及解决办法

    本文引自http://m.blog.csdn.net/qq_32331997/article/details/72885424 微信公众平台服务器配置时,需要引入token,但是提交的时候总是提示to ...

  6. 文件导出也可以这么写【js+blob】

    文件导出在软件开发中是个比较常用的功能,基本原理也很简单: 浏览器向后台发送一个Get请求 后台处理程序接收到请求后,经过处理,返回二进制文件流 浏览器接收到二进制文件流后提示下载文件 调用的js方法 ...

  7. python DNS域名轮询业务监控

    应用场景: 目前DNS支持一个域名对应多个IP的解析,优势是可以起到负载均衡的作用,最大的问题是目标主机不可用时无法自动剔除,因此必须在自己的业务端写好监控与发现,怎么样来做这样的监控,以python ...

  8. char码值对应列表大全

    Char("0") 为0的字符Char("1") Char("2") Char("3") Char("4&qu ...

  9. Ajax模拟Form表单提交,含多种数据上传

    ---恢复内容开始--- Ajax提交表单.使用FormData提交表单数据和上传的文件(这里的后台使用C#获取,你可以使用Java一样获取) 有时候前台的数据提交到后台,不想使用form表单上传,希 ...

  10. jni 类初始化失败(nested exception is java.lang.NoClassDefFoundError)

    nested exception is java.lang.NoClassDefFoundError: Could not initialize class com.netease.facedetec ...