作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/#/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.

题目大意

在已经排好序的数组中,找到两个数字的和等于target,返回两个数字的基于1开始的索引。

解题方法

Java解法

已经排序好了的数组,找出目标和。可以挨个试,时间复杂度是O(n^2),复杂度太高。可以用双指针,同时向中间靠拢,肯定能达到目标的和。

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

Python解法

二刷,python解法。

class Solution:
def twoSum(self, numbers, target):
"""
:type numbers: List[int]
:type target: int
:rtype: List[int]
"""
N = len(numbers)
left, right = 0, N - 1
while left < right:
cursum = numbers[left] + numbers[right]
if cursum == target:
return [left + 1, right + 1]
elif cursum < target:
left += 1
else:
right -= 1
return [0, 0]

日期

2017 年 4 月 18 日
2018 年 11 月 14 日 —— 又到周五了

【LeetCode】167. Two Sum II - Input array is sorted 解题报告(Python)的更多相关文章

  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 - O(n) - ( C++ ) - 解题报告

    1.题目大意 Given an array of integers that is already sorted in ascending order, find two numbers such 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

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

随机推荐

  1. nginx_location

    Nginx location 配置语法 1. location [ = | ~ | ~* | ^~ ] uri { ... } 2. location @name { ... } location 配 ...

  2. Spring Cloud 2021.0.0 正式发布,第一个支持Spring Boot 2.6的版本!

    美国时间12月2日,Spring Cloud 正式发布了第一个支持 Spring Boot 2.6 的版本,版本号为:2021.0.0,codename 为 Jubilee. 在了解具体更新内容之前, ...

  3. 学习java 7.9

    学习内容: Date类 Date类常用方法 SimpleDateFormat 1.格式化(从Date到String) public final String format(Date date) 将日期 ...

  4. python生成器浅析

    A 'generator' is a function which returns a generator iterator. It looks like a normal function exce ...

  5. eclipse上安装 windowBuilder方法

    最近因为需要用java Swing做一些组件设计,但想想以前在大学时候为了布局组件和位置设计花了很多时间.所以再网上查了一些带有可视化的设计插件用来提高工作效率. 其中一个是 windowBuilde ...

  6. Android: EditText设置属性和设置输入规则

    1.EditText输入限制规则 在xml:EditText 设置属性 android:digits="ABCDE123&*" ABCDE123&*是你的限制规则 ...

  7. GCD的补充

    1-1 关于GCD中的创建和释放     在iOS6.0之前,在GCD中每当使用带creat单词的函数创建对象之后,都应该对其进行一次release操作.           在iOS6.0之后,GC ...

  8. 【Spring Framework】Spring入门教程(五)AOP思想和动态代理

    本文主要讲解内容如下: Spring的核心之一 - AOP思想 (1) 代理模式- 动态代理 ① JDK的动态代理 (Java官方) ② CGLIB 第三方代理 AOP概述 什么是AOP(面向切面编程 ...

  9. Spring实现类私有方法测试通用方案

    现实的业务场景中,可能需要对Spring的实现类的私有方法进行测试. 场景描述: 比如XXXService里有 两个函数a.函数b. 而实现类XXXServiceImpl中实现了函数a.函数b,还包含 ...

  10. 【C/C++】旋转数组的最小数字/ 剑指offer

    #include <bits/stdc++.h> using namespace std; class Solution { public: int minNumberInRotateAr ...