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. 【原】Java学习笔记029 - 映射

    package cn.temptation; import java.util.HashMap; import java.util.Map; public class Sample01 { publi ...

  2. Dynamics 365 Online-60000 API Limits

    最近,Dynamics 365官方文档有最新的发布,就是从2018.3.19开始,会对API请求的数目有个限制:在连续的5分钟里,一个连接用户,针对一个CRM Organization,API请求数不 ...

  3. (python)剑指Offer(第二版)面试题14:剪绳子

    题目 给你一根长度为n的绳子,请把绳子剪成m段 (m和n都是整数,n>1并且m>1)每段绳子的长度记为k[0],k[1],…,k[m].请问k[0]k[1]…*k[m]可能的最大乘积是多少 ...

  4. 初识SSO与JWT

    以前在学校做项目的时候,登录注销,权限验证这些事情,都是交给框架来做的,每次都是把这个架子拿到项目中去,也没有真正思考过它的过程,总觉的这些都是十分简单的逻辑. 然而来公司工作之后,慢慢觉得登录和权限 ...

  5. [BZOJ2049] [SDOI2008] Cave 洞穴勘测 (LCT)

    Description 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好 ...

  6. 2017angular、vue、react热度

    中国,过去一年: 中国,过去五年: 全球,过去一年: 全球,过去五年: 其他相关: 数据来源自:Google Trends

  7. spring整合mybatis(hibernate)配置

    一.Spring整合配置Mybatis spring整合mybatis可以不需要mybatis-config.xml配置文件,直接通过spring配置文件一步到位.一般需要具备如下几个基本配置. 1. ...

  8. fitnesse - 用例创建编辑、管理、执行和日志

    fitnesse - 用例创建编辑.管理.执行和日志 2017-10-09 目录 1 用例创建编辑  1.1 用例创建  1.2 用例编辑2 用例管理3 用例测试执行和日志  3.1 用例测试执行  ...

  9. Matcher类的简单使用

    今天工作时遇到一个问题, 用正则处理html标签时不知该如何下手.还好有Matcher帮助解决了问题. 需求如下: 例如有如下html文章内容: <p><a href="w ...

  10. 如何在原生微信小程序中实现数据双向绑定

    官网:https://qiu8310.github.io/minapp/ 作者:Mora 在原生小程序开发中,数据流是单向的,无法双向绑定,但是要实现双向绑定的功能还是蛮简单的! 下文要讲的是小程序框 ...