public class TwoSum1 {
public static void main(String[] args) {
int[] nums = new int[]{2, 7, 11, 15};
int target = 9;
int a[] = new int[2];
a = twoSum(nums, target);
System.out.println(a[0]+"----------"+a[1]);
} public static int[] twoSum(int[] nums,int target){
int[] a = new int[2];
for (int i = 0; i < nums.length; i++) {
for (int j = i+1; j < nums.length; j++) {
if(nums[i]+nums[j] == target){
a[0]=i;
a[1]=j; return a;
}
}
}
return a;
}
}

Leetcode:1. Two Sum的更多相关文章

  1. LeetCode:40. Combination Sum II(Medium)

    1. 原题链接 https://leetcode.com/problems/combination-sum-ii/description/ 2. 题目要求 给定一个整型数组candidates[ ]和 ...

  2. leetcode:Minimum Path Sum(路线上元素和的最小值)【面试算法题】

    题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right w ...

  3. LeetCode:39. Combination Sum(Medium)

    1. 原题链接 https://leetcode.com/problems/combination-sum/description/ 2. 题目要求 给定一个整型数组candidates[ ]和目标值 ...

  4. Python解Leetcode: 1. Two Sum

    题目描述:求出数组中等于目标值的两个数的索引,假定肯定存在两个数并且同一个索引上的数不能用两次. 思路: 用空间换时间,使用一个字典存储已经遍历的数字的索引,如果新遍历的数字和target的差值在字典 ...

  5. 乘风破浪:LeetCode真题_040_Combination Sum II

    乘风破浪:LeetCode真题_040_Combination Sum II 一.前言 这次和上次的区别是元素不能重复使用了,这也简单,每一次去掉使用过的元素即可. 二.Combination Sum ...

  6. 乘风破浪:LeetCode真题_039_Combination Sum

    乘风破浪:LeetCode真题_039_Combination Sum 一.前言     这一道题又是集合上面的问题,可以重复使用数字,来求得几个数之和等于目标. 二.Combination Sum ...

  7. LeetCode 2 Add Two Sum 解题报告

    LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...

  8. leetcode:Roman to Integer(罗马数字转化为罗马数字)

    Question: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the rang ...

  9. leetcode 练习1 two sum

    leetcode 练习1  two sum whowhoha@outlook.com 问题描述 Given an array of integers, return indices of the tw ...

随机推荐

  1. centos web+mysql服务器的安全

    今天闲来无事,拿来X-Scan-v3.3 来扫描自己的服务器,开放端口有22,80,443,3306:3306端口被扫出来,呵呵,那可不得了: 一,屏蔽扫描器扫出3306端口,因为web和数据库是在同 ...

  2. 处理 javax.el.ELException: Failed to parse the expression 报错

    在JSP的表达式语言中,使用了  <h3>是否新Session:${pageContext.session.new}</h3>  输出Session是否是新的,此时遇到了  j ...

  3. 大话设计模式--策略模式 strategy -- C++实现实例

    1. 策略模式: 它定义了算法家族, 分别封装起来,使他们之间可以相互替换,此模式让算法变化, 不会影响到使用算法的客户. 用相同的方法调用不同的算法,减少各种算法类与使用算法类之间的耦合. 实例中策 ...

  4. 在解析XML时要注意解析元素和解析标签属性的区别

    解析元素时: Sting str = ele.elementText("name"); 而解析标签属性时: String key = ele.attributeValue(&quo ...

  5. Linux课程---7、shell技巧(获取帮助命令)

    Linux课程---7.shell技巧(获取帮助命令) 一.总结 一句话总结: ls --help:简单手册 man ls:内容手册 1.tab补全? 命令+tab:加快敲命令敲文件目录的速度,多敲几 ...

  6. 分享知识-快乐自己:大数据(hadoop)环境搭建

    大数据 hadoop 环境搭建: 一):大数据(hadoop)初始化环境搭建 二):大数据(hadoop)环境搭建 三):运行wordcount案例 四):揭秘HDFS 五):揭秘MapReduce ...

  7. C中malloc的使用(转)

    malloc函数  原型:extern void *malloc(unsigned int num_bytes); 用法:#include <malloc.h> 功能:分配长度为num_b ...

  8. python-杂烩

    如何从其它目录导入py文件 import sys sys.path.append("文件目录地址") from xxx import xxx

  9. java--Hibernate实现分页查询

    首先在Action类定义当前页的值 private Integer currPage = 1 setter,getter 在spring IOC容器进行注入该变量 在Action类写一个findAll ...

  10. python map函数 reduce函数

    Python中map()函数浅析 函数式编程: 更好的描述问题 map函数 怎么理解当传入多个参数list时,map如何运作:    abc函数第一次传入的数据时 (11,44,77),然后(22,5 ...