Question

167. Two Sum II - Input array is sorted

Solution

题目大意:和Two Sum一样,这里给出的数组是有序的

思路:target - nums[i],这样就实现了降维了

Java实现:

  1. public int[] twoSum(int[] nums, int target) {
  2. int[] result = new int[2];
  3. Map<Integer, Integer> map = new HashMap<>();
  4. for (int i = 0; i < nums.length; i++) {
  5. if (map.containsKey(target - nums[i])) {
  6. result[1] = i + 1;
  7. result[0] = map.get(target - nums[i]) + 1;
  8. return result;
  9. }
  10. map.put(nums[i], i);
  11. }
  12. return result;
  13. }

别人的实现:

https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/discuss/51239/Share-my-java-AC-solution.

  1. public int[] twoSum(int[] num, int target) {
  2. int[] indice = new int[2];
  3. if (num == null || num.length < 2) return indice;
  4. int left = 0, right = num.length - 1;
  5. while (left < right) {
  6. int v = num[left] + num[right];
  7. if (v == target) {
  8. indice[0] = left + 1;
  9. indice[1] = right + 1;
  10. break;
  11. } else if (v > target) {
  12. right --;
  13. } else {
  14. left ++;
  15. }
  16. }
  17. return indice;
  18. }

167. Two Sum II - Input array is sorted - LeetCode的更多相关文章

  1. 29. leetcode 167. Two Sum II - Input array is sorted

    167. Two Sum II - Input array is sorted Given an array of integers that is already sorted in ascendi ...

  2. 167. Two Sum II - Input array is sorted【easy】

    167. Two Sum II - Input array is sorted[easy] Given an array of integers that is already sorted in a ...

  3. 167. Two Sum II - Input array is sorted@python

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  4. leetcode 1.Two Sum 、167. Two Sum II - Input array is sorted 、15. 3Sum 、16. 3Sum Closest 、 18. 4Sum 、653. Two Sum IV - Input is a BST

    1.two sum 用hash来存储数值和对应的位置索引,通过target-当前值来获得需要的值,然后再hash中寻找 错误代码1: Input:[3,2,4]6Output:[0,0]Expecte ...

  5. [LeetCode] 167. Two Sum II - Input array is sorted 两数和 II - 输入是有序的数组

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  6. 167. Two Sum II - Input array is sorted

    题目: Given an array of integers that is already sorted in ascending order, find two numbers such that ...

  7. (双指针 二分) leetcode 167. Two Sum II - Input array is sorted

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  8. 167. Two Sum II - Input array is sorted (Array)

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  9. [LeetCode&Python] Problem 167. Two Sum II - Input array is sorted

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

随机推荐

  1. JavaScript中函数节流的理解

    函数节流的原理 函数节流,就是对会频繁触发的函数事件做一些限制,让这些函数可以在每隔一定的时间或者每次满足一定的条件下再触发.一般我们会给他起一个名字throttle.也就是节流的意思.一般这样的函数 ...

  2. js和原生应用常用的数据交互方式

    场景1 在原生app中经常会使用到H5页面,比如说电商中的活动页,一些电商中的详情页,等等...这些页面都有一个特点,那就是在未来修改的可能性,和一次性的几率特别的大.所以用H5的页面是最睿智的一种选 ...

  3. leetcode1753. 移除石子的最大得分

    题目描述: 你正在玩一个单人游戏,面前放置着大小分别为 a​​​​​​.b 和 c​​​​​​ 的 三堆 石子. 每回合你都要从两个 不同的非空堆 中取出一颗石子,并在得分上加 1 分.当存在 两个或 ...

  4. c++对c的拓展_增强

    一:新增bool类型关键字:c中bool类型需要添加stdbool.h头文件,c++则可直接使用 void test(){ bool a=true; //c++可直接定义而c需添加头文件 true和f ...

  5. linux磁盘之分区类型id

    我们通过命令来查看一下linux系统定义的分区类型id及其意义(更改磁盘分区类型必须掌握)系统采样: [root@fp-web-130 ~]# cat /etc/redhat-release Cent ...

  6. switch 用法

    1.语法格式和规则 switch case 语句语法格式如下: switch(expression){ case value : //语句 break; //可选 case value : //语句 ...

  7. C. Sum of Cubes

    原题链接 https://codeforces.com/problemset/problem/1490/C 题目 题意 如果一个数 n = x3 + y3 (x, y可以相等, 且> 0) 输出 ...

  8. android软件简约记账app开发day03-自定义键盘的书写

    android软件简约记账app开发day03-自定义键盘的书写 我们在fragment界面使用了自定义的keybroad键盘,所以今天我们来书写自定义的键盘代码 新建util包,新建keyboard ...

  9. Java语言学习day04--7月1日

    ###09数据类型转换_自动转换     * A:   自动类型转换         * a:表示范围小的数据类型转换成范围大的数据类型,这种方式称为自动类型转换             自动类型转 ...

  10. golang内存对齐分析(转载)

    问题 type Part1 struct { a bool b int32 c int8 d int64 e byte } 在开始之前,希望你计算一下 Part1 共占用的大小是多少呢? func m ...