题目:

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

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution.

Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2

思路:两层for循环

package sum;public class TwoSum {public int[] twoSum(int[] nums, int target) {int len = nums.length;int[] res = new int[2];for (int i = 0; i < len - 1; ++i) {res[0] = i + 1;int rem = target - nums[i];for (int j = i + 1; j < len; ++j) {if (nums[j] == rem) {res[1] = j + 1;return res;}}}return res;}public static void main(String[] args) {// TODO Auto-generated method stubTwoSum t = new TwoSum();int[] nums = {2, 7, 11, 15};int target = 9;int[] res = t.twoSum(nums, target);System.out.println(res[0]);System.out.println(res[1]);}}

LeetCode - Two Sum相关推荐

  1. 【同113】LeetCode 129. Sum Root to Leaf Numbers

    LeetCode 129. Sum Root to Leaf Numbers Solution1:我的答案 二叉树路径和问题,类似113 /*** Definition for a binary tr ...

  2. LeetCode Path Sum III(前缀和)

    问题: 给定一个二叉树,它的每个结点都存放着一个整数值. 找出路径和等于给定数值的路径总数. 路径不需要从根节点开始,也不需要在叶子节点结束,但是路径方向必须是向下的(只能从父节点到子节点). 二叉树 ...

  3. LeetCode Combination Sum IV(动态规划)

    问题:给出一个数组nums和目标数target,问有多少组合形式 思路:用dp(i)表示目标数target的组合数.则有状态转移关系为dp(i)=sum(dp(i-nums[j])),其中i>= ...

  4. LeetCode Range Sum Query - Mutable(树状数组、线段树)

    问题:给出一个整数数组,求出数组从索引i到j范围内元素的总和.update(i,val)将下标i的数值更新为val 思路:第一种方式是直接根据定义,计算总和时直接计算从i到j的和 第二种方式是使用树状 ...

  5. LeetCode Two Sum III - Data structure design

    原题链接在这里:https://leetcode.com/problems/two-sum-iii-data-structure-design/ 题目: Design and implement a ...

  6. LeetCode Subarray Sum Equals K

    原题链接在这里:https://leetcode.com/problems/subarray-sum-equals-k/description/ 题目: Given an array of integ ...

  7. leetcode 907. Sum of Subarray Minimums | 907. 子数组的最小值之和(单调栈)

    题目 https://leetcode.com/problems/sum-of-subarray-minimums/ 题解 单调栈问题.参考左神算法课:https://ke.qq.com/webcou ...

  8. leetcode 371. Sum of Two Integers | 371. 两整数之和(补码运算)

    题目 https://leetcode.com/problems/sum-of-two-integers/ 题解 根据 related topics 可知,本题考察二进制运算. 第一次提交的时候,没想 ...

  9. [Leetcode]Two sum(两数之和)系列总结

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

  10. leetcode -- 3 sum

    3-sum 题目描写叙述: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = ...

最新文章

  1. asp.net关于kindeditor 上传图片出现服务器故障的解决办法
  2. PAT-乙级-1020. 月饼 (25)
  3. 重构手册阅读笔记:重构的含义
  4. js中的Array数组清空
  5. Eclipse中Mybatis的自动提示的配置
  6. 苹果付费app共享公众号_娄底共享云店铺公众号
  7. 【投资策略】2022 年大类资产配置展望:稳中求进-中金公司
  8. mybatis实战教程(mybatis in action)之三:实现数据的增删改查
  9. mysql安装排错-解决1
  10. Oracle Tablespace Transportation
  11. C语言关键字能用大写字母,C语言关键字及其解释
  12. pytorch拟合函数
  13. java emf 转jpg_JAVA读取EMF文件并转化为PNG,JPG,GIF格式
  14. 地址总线、数据总线、控制总线
  15. kettle简单的更新与插入
  16. 【软路由安装(PVE+ikuai)】
  17. Linux工具篇 | Ubuntu安装string命令
  18. 黑白照片修复彩色软件免费有哪些?分享这三个实用的软件给你
  19. 深入浅出单点登录---4、基于OAuth实现的统一认证
  20. 干支纪年java_天干地支纪年法 - osc_xcg0s5cw的个人空间 - OSCHINA - 中文开源技术交流社区...

热门文章

  1. GridViewObjectDataSource新特性小记 懒人篇(一) 分页上路
  2. 设计模式,你知道什么是Observer模式吗?
  3. Hibernate 中 set 里的属性及定义
  4. python 多个装饰器的调用顺序
  5. Zsh和oh my zsh的安装和使用
  6. jsp页面数据与action数据交互 使用导航图语言和set注入
  7. 通过bindservice方式调用服务方法里面的过程
  8. result之global-results全局结果集
  9. [转载]Yahoo!的分布式数据平台PNUTS简介及感悟
  10. 工欲善其事,必先利其器——图文并茂详解VisualStudio使用技巧一