Two Sum

  • Total Accepted: 262258
  • Total Submissions: 1048169
  • Difficulty: Easy

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]. 暴力解法,没有优化思路。
 public class Num1 {
public int[] twoSum(int[] nums, int target) {
int [] res = new int [2] ;
for(int i = 0 ; i < nums.length ; i++){
if(nums[i] > target){
continue ;
}else{
res[0] = i ;
}
for(int j = i+1 ; j < nums.length ; j++){
if((nums[i]+nums[j]) == target){
res[1] = j ;
return res ;
}else{
continue ;
}
}
} return res ;
}
}

No.001 Two Sum的更多相关文章

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

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

  2. 【JAVA、C++】LeetCode 001 Two Sum

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

  3. LeetCode #001# Two Sum(js描述)

    索引 思路1:暴力搜索 思路2:聪明一点的搜索 思路3:利用HashMap巧解 问题描述:https://leetcode.com/problems/two-sum/ 思路1:暴力搜索 一个很自然的想 ...

  4. LeetCode--No.001 Two Sum

    Two Sum Total Accepted: 262258 Total Submissions: 1048169 Difficulty: Easy Given an array of integer ...

  5. [Leetcode][001] Two Sum (Java)

    题目在这里: https://leetcode.com/problems/two-sum/ [标签]Array; Hash Table [个人分析] 这个题目,我感觉也可以算是空间换时间的例子.如果是 ...

  6. 001 Two Sum 两个数的和为目标数字

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

  7. 【LeetCode】001. Two Sum

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  8. LeetCode 【1】 Two Sum --001

    5月箴言 住进布达拉宫,我是雪域最大的王.流浪在拉萨街头,我是世间最美的情郎.—— 仓央嘉措 从本周起每周研究一个算法,并以swift实现之 001 -- Two Sum (两数之和) 题干英文版: ...

  9. 《LeetBook》LeetCode题解(1) : Two Sum[E]——哈希Map的应用

    001.Two Sum[E] Two SumE 题目 思路 1双重循环 2 排序 3 Hashmap 1.题目 Given an array of integers, return indices o ...

随机推荐

  1. SVN分支的合并和同步

    使用svn几年了,一直对分支和合并敬而远之,一来是因为分支的管理不该我操心,二来即使涉及到分支的管理,也不敢贸然使用合并功能,生怕合并出了问题对团队造成不良影响,最主要的原因是,自己对分支的目的和合并 ...

  2. 拖拽碰撞--原声js(自身理解上新的方法)

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  3. 黄聪:PHP字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、切割成数组等)

    一.字符串替换 str_replace("iwind", "kiki", "i love iwind, iwind said"); 将输出 ...

  4. 在C#中子线程如何操作主线程中窗体上控件

    在C#中,直接在子线程中对窗体上的控件操作是会出现异常,这是由于子线程和运行窗体的线程是不同的空间,因此想要在子线程来操作窗体上的控件,是不可能 简单的通过控件对象名来操作,但不是说不能进行操作,微软 ...

  5. windows下脚本检测tomcat是否启动,没有启动则启动

    最近有个服务需要部署到windows server2003上面,机房没有windows ser的机器,没办法搞了个阿里云服务,购买的配置比较低, 不知道什么原因,tomcat启动后总是容易自动退出,搞 ...

  6. 加入ScrollView后OnGestureListener无效的解决办法

    android中,ViewFlipper+OnGestureListener可以实现左右滑动效果. 但是在ViewFlipper加上了ScrollView就悲剧了,左右滑动事件无效了…… 这里其实只需 ...

  7. C语言中,数组名作为参数传递给函数时,退化为指针

    C语言中,数组名作为参数传递给函数时,退化为指针   C语言中,数组名作为参数传递给函数时,退化为指针:需要数组大小时, 需要一个参数传数组名,另一个传数组大小. 数组名做函数参数时,就相当于指针了. ...

  8. 自己动手写ORM

    http://blog.csdn.net/sundacheng1989/article/category/1350100

  9. centos下安装MySQL5.7

    1.查找mysqlwhereis mysql 2.删除mysqlyum remove mysql mysql-server mysql-libs mysql-server;rm –rf /usr/li ...

  10. tiny210裸机第1课(启动原理)

    软硬件环境 宿主机系统:ubuntu 板子芯片:S5PV210(Contex-A8),512M DDR2,512M SLC Nand 交叉编译器:arm-linux-gcc-4.5.1 手册:S5PV ...