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. 芝麻HTTP:Appium的安装

    Appium是移动端的自动化测试工具,类似于前面所说的Selenium,利用它可以驱动Android.iOS等设备完成自动化测试,比如模拟点击.滑动.输入等操作,其官方网站为:http://appiu ...

  2. android界面设计之布局管理

    谈到android界面设计,各种布局样式不得不提!传统的布局方式有6种,我们会一一介绍. 在android studio2.2版本之后出现了一款超棒的布局方式,真正意义上的所见即所得,后面我们也会讲到 ...

  3. arttemplate与webpack的应用

    模板渲染使用arttemplate,使用方法如下: 普通使用 普通使用把渲染模板写在<script>标签里面,引入arttemplate.js,从服务端接收数据(数组与对象的形式),然后调 ...

  4. Vasya and Basketball CodeForces - 493C

    Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows ...

  5. Drying POJ - 3104

    It is very hard to wash and especially to dry clothes in winter. But Jane is a very smart girl. She ...

  6. 标注-隐马尔可夫模型HMM的探究

    1 HMM基本概念1.1 定义1.2 观测序列生成过程1.3 HMM的三个问题2 概率计算算法2.1 直接计算算法2.2 前向算法forward algorithm2.3 后向算法2.4 一些概率与期 ...

  7. Redis入门必读,The Little Redis Book中文版

    csdn的博客都要搬到这里了 The Little Redis Book中文版 入门 The Little Redis Book中文版 第一章 - 基础知识 The Little Redis Book ...

  8. 由会话信息保存认识ThreadLocal

    这次想总结ThreadLocal这个东西,也是由于项目中使用到了它去帮助保存会话信息.传统的(或者说我在学校的时候)方法,大多是用服务端的session保存会话,与浏览器端的cookie协作去追踪这个 ...

  9. C#抽象方法与抽象实例--C#基础

    1.抽象方法与抽象类的声明 1)抽象类和抽象方法声明必须包含abstract 2)抽象方法的声明没有方法体:public abstract void fly(); 3)抽象类和抽象法前加上public ...

  10. 在MyEclipse 10中配置tomcat田服务器时出现的问题以及解觉办法

    今天刚刚重装电脑,为了实训的一个项目要使用到MyEclipse开发工具但是在配置服务器之后运行时出现了问题 错误:java.lang.UnsupportedClassVersionError: org ...