【Description】

Given an array of integers that is already sorted in ascending order, 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.

Note:

  • Your returned answers (both index1 and index2) are not zero-based.
  • You may assume that each input would have exactly one solution and you may not use the same element twice.

Example:

Input: numbers = [2,7,11,15], target = 9
Output: [1,2]
Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.

【AC code】

一、暴力法  时间复杂度:O(n^2)

 class Solution {
public int[] twoSum(int[] numbers, int target) {
int arrlen = numbers.length;
for (int i = 0; i < arrlen - 1; i++) {
for (int j = i + 1; j < arrlen; j++) {
if (numbers[i] + numbers[j] == target) return new int[]{i + 1, j + 1};
}
}
return new int[]{};
}
}

二、二分查找法  时间复杂度:O(nlogn)

 class Solution {
public int[] twoSum(int[] numbers, int target) {
int arrlen = numbers.length;
for (int i = 0; i < arrlen; i++) {
int left = i + 1, right = arrlen - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
int tmp = numbers[i] + numbers[mid];
if (tmp > target) right = mid - 1;
else if (tmp < target) left = mid + 1;
else return new int[]{i + 1, mid + 1};
}
}
return new int[]{};
}
}

三、双索引法  时间复杂度:O(n)

 class Solution {
public int[] twoSum(int[] numbers, int target) {
int left = 0, right = numbers.length - 1;
while (left < right) {
int tmp = numbers[left] + numbers[right];
if (tmp == target) return new int[]{left + 1, right + 1};
else if (tmp > target) right--;
else left++;
}
return new int[]{};
}
}

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

  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. [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 ...

  3. 【Leetcode 167】Two Sum II - Input array is sorted

    问题描述:给出一个升序排列好的整数数组,找出2个数,它们的和等于目标数.返回这两个数的下标(从1开始),其中第1个下标比第2个下标小. Input: numbers={2, 7, 11, 15}, t ...

  4. 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 ...

  5. (双指针 二分) 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 ...

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

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

  7. ✡ leetcode 167. Two Sum II - Input array is sorted 求两数相加等于一个数的位置 --------- java

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

  8. Java [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 th ...

  9. LeetCode - 167. Two Sum II - Input array is sorted - O(n) - ( C++ ) - 解题报告

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

随机推荐

  1. idea中的springboot项目如何不用重新编译,自动热部署

    两步走:引入依赖,配置idea 在pom.xml中引入如下依赖,关键字:devtools 第二步,修改idea两处配置 2.1 windows下,ctl+alt+s打开idea配置菜单 左上角输入框搜 ...

  2. Python递归函数,二分查找算法

    目录 一.初始递归 二.递归示例讲解 二分查找算法 一.初始递归 递归函数:在一个函数里在调用这个函数本身. 递归的最大深度:998 正如你们刚刚看到的,递归函数如果不受到外力的阻止会一直执行下去.但 ...

  3. 通过注解实现通用导出Excel

    Javaweb开发中数据的导入导出很常见,每次我们都需要写很多代码,所以我就在想能不能写一些通用的方法,之前已经在网上 整理过一些通用的方法,最近在网上看到一位牛人封装的更加简介,自己拿过来整理了一下 ...

  4. vue中的虚拟DOM树

    什么是虚拟DOM树?(Virtual DOM)   虚拟DOM树其实就是一个普通的js对象,它是用来描述一段HTML片段的    01    当页面渲染的时候Vue会创建一颗虚拟DOM树 02    ...

  5. CentOS7.x 搭建 GitLab 教程

    今天闲来无事,想起之前买了一个阿里云 ECS,一直闲置着没用,一时兴起就想搭个自己的 GitLab 玩玩,GitLab 官网也提供了安装教程,很简单,照着步骤一步步基本没什么问题,可能安装的过程中有一 ...

  6. 深入理解JVM虚拟机开篇:JVM介绍与知识脉络梳理

    微信公众号[Java技术江湖]一位阿里 Java 工程师的技术小站.作者黄小斜,专注 Java 相关技术:SSM.SpringBoot.MySQL.分布式.中间件.集群.Linux.网络.多线程,偶尔 ...

  7. 终于,我感受到了IDEA的强大

    Java开发者千千万,开发者用的开发工具目前主流却只有2种:eclipse和IDEA,我入行以来一直用的eclipse,听过IDEA很好很强大,但是也只是处于听说的阶段,基本没用过,自然没怎么体会过. ...

  8. 【畅通工程 HDU - 1232 】【并查集模板】

    并查集讲解和模板 有一个博文对此分析的很透彻,附链接 为避免原链接失效,现摘录如下: 为了解释并查集的原理,我将举一个更有爱的例子. 话说江湖上散落着各式各样的大侠,有上千个之多.他们没有什么正当职业 ...

  9. Day 07--最终修改(三)

    2.明天着重学一下逻辑层的语法,以及界面层的数据绑定,与队友交流进度 3.今天修改也终于完成,除了搞c++以外的全部身心都放在这个东西身上也觉得它有点难搞,说明计算机不是吃素的.甚至在使用xml语法的 ...

  10. spring中的事件 applicationevent 讲的确实不错(转)

    event,listener是observer模式一种体现,在spring 3.0.5中,已经可以使用annotation实现event和eventListner里. 我们以spring-webflo ...