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. [platform]新旧内核的device设备注册对比

    转自:http://blog.chinaunix.net/uid-7332782-id-3268801.html 1. Version2.6内核启动过程 start_kernel( ) //板子上电启 ...

  2. 限制页面内部链接访问源-HTML注释

        不知道大家有没有碰到过这样一个问题:我修改的是别人的网页,他的超连接是指向一个服务器的某个HTML文件,我把超连接地址修改成了我本地机器的跟首页 同一目录下的一个HTML文件,却发现超连接根本 ...

  3. SQL 中delete和truncate区别

    1.前者按行删除,后者直接删除数据页 2.前者可带where删除部分,后者只能删除全表 3.前者在事务日志中记录每一行的记录,后者只记录页的释放 4.前者删除后,标识技术值不重置,后者重置 5.由fo ...

  4. 黄聪:wordpress如何添加自定义文章快速编辑按钮

    When working with WordPress posts and you want to quickly change the status or date of one or more p ...

  5. HDU5221 Occupation 树链剖分

    题意: 给出一棵树,root=1,树有点权,有一个人叫做M 有3种操作: 1 u v 把u到v路径上的所有点的点权都给M 2 u 若u的点权在M手上,拿走 3 u 把u为根的子树的所有点权都给M 每一 ...

  6. REST架构实质(转)

    REST(Representational State Transfer) 曾经被误解为只是CRUD(增删改查),从这个层面上,好像REST只是和RPC一个层面的东西,没有什么了不起,其实这些都是对R ...

  7. 编写第一个java程序

    安装了一个编辑器,Notepad++,这个编辑器以前在写PHP的时候就喜欢用,呵呵,现在写java也先沿用这个这个编辑器吧. 代码: public class Test{ public static ...

  8. jquery ready 延迟

    $.holdReady(true);//延迟 $.holdReady(false);//解延迟 用于下载文件的时候,异步操作 $.holdReady(true); $.getScript(" ...

  9. Mac下配置JAVA_HOME

    http://blog.csdn.net/shallowgrave/article/details/39367119 闲来无事,装个Hbase玩玩,突然发现Mac下默认安装的JDK7,没有配置JAVA ...

  10. laravel下使用阿里云oss上传图片

    对小公司而言,使用阿里云oss比直接买硬盘要划算的多,不管从存储性价比上还是从网速负载上.最近因为公司的项目有比较大的图片存储访问需求,所以决定使用阿里云的oss. 在研究了一下以后,摆着不自己造轮子 ...