LeetCode【167. 两数之和 II - 输入有序数组】

这道题最开始想到的就是,两个for,target是两数之和,就可以target - numbers[i],比较是否有与后面数相等。

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

这样写的问题就在于,数多了后,有超时的可能。

原文地址:https://www.cnblogs.com/wzwi/p/10860892.html

时间: 2024-08-29 07:47:06

LeetCode【167. 两数之和 II - 输入有序数组】的相关文章

Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)

给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明: 返回的下标值(index1 和 index2)不是从零开始的. 你可以假设每个输入只对应唯一的答案,而且你不可以重复使用相同的元素. 示例: 输入: numbers = [2, 7, 11, 15], target = 9 输出: [1,2] 解释: 2 与 7 之和等于目标数 9 .因此 index1 = 1

167. 两数之和 II - 输入有序数组

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 m

LeetCode167 两数之和 II - 输入有序数组

给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明: 返回的下标值(index1 和 index2)不是从零开始的. 你可以假设每个输入只对应唯一的答案,而且你不可以重复使用相同的元素. 示例: 输入: numbers = [2, 7, 11, 15], target = 9 输出: [1,2] 解释: 2 与 7 之和等于目标数 9 .因此 index1 = 1

167 Two Sum II - Input array is sorted 两数之和 II - 输入有序数组

给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数.函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2.请注意,返回的下标值(index1 和 index2)不是从零开始的.你可以假设每个输入都只有一个解决方案,而且你不会重复使用相同的元素.输入:数组 = {2, 7, 11, 15}, 目标数 = 9输出:index1 = 1, index2 = 2详见:https://leetcode.com/problems/two-

两数之和 II - 输入有序数组 --二分查找

题目 给定一个已按照升序排列的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明: 返回的下标值(index1 和 index2)不是从零开始的. 你可以假设每个输入只对应唯一的答案,而且你不可以重复使用相同的元素. 示例: 输入: numbers = [2, 7, 11, 15], target = 9 输出: [1,2] 解释: 2 与 7 之和等于目标数 9 .因此 index1 =

leetcode167. 两数之和 II - 输入有序数组(双指针)

https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted/ 求数组中加起来恰好等于target的两个数的位置. 双指针问题,分别从头和尾遍历数组,如果加起来和大于target,则尾指针左移,如果加起来和小于target,则头指针右移,如果恰好则返回数组即可. 注意:1.数组的长度为****.length,没有括号! 2.定义数组new int[]{...} class Solution { public int[] two

leetCode:twoSum 两数之和 【JAVA实现】

LeetCode 两数之和 给定一个整数数组,返回两个数字的索引,使它们相加到特定目标. 您可以假设每个输入只有一个解决方案,并且您可能不会两次使用相同的元素. 更多文章查看个人博客 个人博客地址:twoSum 两数之和 [JAVA实现] 方法一 使用双重循环两两相加判断是否等于目标值 public List<String> twoSum2(int[] arr, int sum) { if (arr == null || arr.length == 0) { return new ArrayL

前端与算法 leetcode 1. 两数之和

目录 # 前端与算法 leetcode 1. 两数之和 题目描述 概要 提示 解析 解法一:暴力法 解法二:HashMap法 算法 传入[2,7,11,1,12,34,4,15],19的运行结果 执行结果 GitHub仓库 # 前端与算法 leetcode 1. 两数之和 题目描述 给定一个整数数组 nums?和一个目标值 target,请你在该数组中找出和为目标值的那?两个?整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 示例: 给

[算法] LeetCode 1.两数之和

LeetCode 1.两数之和(python) 1.朴素解法 最朴素的两个for循环大法: class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: for i in range(len(nums)): for j in range(i+1,len(nums)): if nums[i] + nums[j] == target: return [i, j] 但注意,不要用enumerate函数写,会超时