双指针---有序数组的TWO SUM
双指针思想,主要用于遍历数组,两个指针指向不同的元素,从而协同完成任务。
有序数组的 Two Sum
Leetcode :167. Two Sum II - Input array is sorted (Easy)
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
题目描述:
在有序数组中找出两个数,使他们的和为target。
思路分析:
使用双指针,一个指针指向较小的元素,一个指向较大的元素,指向较小的元素的指针从左到右进行遍历,指向较大的元素的指针从右向左进行遍历。
如果两个指针指向的元素和sum==target,那么得到要求的结果。
如果sum>target,移动较大的元素,使sum变小一些;
如果sum<target,移动较大的元素,使sum变大一些;
代码:
public int[]twoSum(int []nums,int target){
if(nums==null||nums.length<2)
return null;
int []res=new int [2];
int small=0;
int big=nums.length-1;
while(small<big){
if(nums[small]+nums[big]==target){
res[0]=small+1;
res[1]=big+1;
return res;
}else if(nums[small]+nums[big]<target){
small++;
}else{
big--;
}
}
return null
}
双指针---有序数组的TWO SUM的更多相关文章
- Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)
Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted) 给定一个已按照升序排列 的有序数组,找到两个数使得它们 ...
- C#LeetCode刷题之#167-两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3903 访问. 给定一个已按照升序排列 的有序数组,找到两个数使得 ...
- 167. Two Sum II - Input array is sorted【Easy】【双指针-有序数组求两数之和为目标值的下标】
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- [Swift]LeetCode167. 两数之和 II - 输入有序数组 | 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 ...
- 167 Two Sum II - Input array is sorted 两数之和 II - 输入有序数组
给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数.函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2.请注意,返回的下标值(i ...
- LeetCode 167:两数之和 II - 输入有序数组 Two Sum II - Input array is sorted
公众号: 爱写bug(ID:icodebugs) 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index ...
- 167. 两数之和 II - 输入有序数组 + 哈希表 + 双指针
167. 两数之和 II - 输入有序数组 LeetCode_167 题目描述 方法一:暴力法(使用哈希表) class Solution { public int[] twoSum(int[] nu ...
- 4.19——数组双指针——26. 删除有序数组中的重复项 & 27. 删除有序数组中的重复项II & 80. 删除有序数组中的重复项 II
第一次做到数组双指针的题目是80: 因为python的List是可以用以下代码来删除元素的: del List[index] 所以当时的我直接用了暴力删除第三个重复元素的做法,大概代码如下: n = ...
- leetcode.双指针.88合并两个有序数组-Java
1. 具体题目 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量分别 ...
随机推荐
- 在虚拟机Linux中安装VMTools遇到的问题-小结
总结: 遇到的问题:No support for locale: zh_CN.utf8 可能的解决方法:1.sudo dpkg-reconfigure locale (重新配置?) 2.上一步失败,提 ...
- 【LeetCode】位运算 bit manipulation(共32题)
[78]Subsets 给了一个 distinct 的数组,返回它所有的子集. Example: Input: nums = [,,] Output: [ [], [], [], [,,], [,], ...
- sys模块 json pickle模块
# sys模块# import sys# sys.path# sys.argv# sys.exit() # 脚本退出# print('[%s]'%('#'*1))# print('[%s]'%('#' ...
- sonar-runner命令模式运行sonar
适用环境:该种配置的模式适用于本地调试模式 前提条件:在工程路径下创建sonar-project.properties文件 该客户端的路径在系统配置文件中进行了定义 alias sonar-runne ...
- 卷积神经网络(Text--cnn)(知识点整理)
参考:http://www.wildml.com/2015/12/implementing-a-cnn-for-text-classification-in-tensorflow/ 独热编码(one- ...
- Graph Neural Networks for Computer Vision
Graph Neural Networks for Computer Vision I was attracted by this image: This is an inspiring image ...
- NIO之Buffer操作示例
1. buffer常规操作 略 2. 只读buffer /** * 只读buffer */ public class BufferTest01 { public static void main(St ...
- OC + RAC (四) combineLatest和merg
-(void)_test4{ ///RAC combineLatest和merge // combineLatest只有当两个信号都发送了 订阅者才能收到信息 结果一次收到 结果是数组 // merg ...
- nginx proxy大文件上传失败问题总结
问题描述: http://www.syhuo.net ota.apk包上传正常 http://www.syhuo.net:8080 ota.apk包上传不正常 查看nginx error日志 [roo ...
- 20180803-Java 流(Stream)、文件(File)和IO
Java 流(Stream).文件(File)和IO 下面的程序示范了用read()方法从控制台不断读取字符直到用户输入"q". // 使用BufferedReader 在控制台读 ...