leet code: Two Sum
leet code: Two Sum
题目
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
思路
要求两个数相加等于target,对数组进行排序。排序之后的数组两边各有一个指针,即指向大元素j和小元素i。两个指针所指元素相加。
若result大于target,则说明大元素需要减小,则j–。若result小于target,则说明小元素需要增大,则i++。即可寻找出结果。
需要注意:
题目要求返回原数组两个数的索引,上面对数组进行排序之后的索引改变了, 所以需要一个另外的数组来保存排序之后的数组每一个元素所对应初始数组的索引位置。
代码
Solution类:
package twosum;public class Solution {public int[] twoSum(int[] nums, int target) {int[] flag = new int[nums.length]; //辅助记录每个数字的原始数组索引for(int i = 0; i<nums.length; i++)flag[i] = i;//冒泡排序for(int i=0; i<nums.length; i++){for(int j=1; j<nums.length-i; j++){System.out.println(nums[j] + " " + nums[j-1]);if(nums[j]<nums[j-1]){int temp;temp = nums[j-1];nums[j-1] = nums[j];nums[j] = temp;temp = flag[j-1];flag[j-1] = flag[j];flag[j] = temp;}}}for (int x: nums) {System.out.print(x + " ");}int[] final_result = new int[2];for(int i=0, j=nums.length-1; i<=j; ){int result = nums[i] + nums[j];if(result == target){final_result[0] = flag[i];final_result[1] = flag[j];break;}else{if(result > target){j--;}else{i++;}}}return final_result;}
}
main函数,只是写了一个测试例子:
import twosum.Solution;public class Main {public static void main(String[] args) {int[] nums = {3,2,4};int target = 6;Solution s = new Solution();int[] result = s.twoSum(nums, target);System.out.println(result[0] + " " + result[1]);}
}
运行结果:
Runtime: 42 ms, faster than 10.23% of Java online submissions for Two Sum.
Memory Usage: 36.6 MB, less than 99.66% of Java online submissions for Two Sum.
反思:
从运行结果反馈来看,速度太慢了,但是占用空间比较少。
之后再看一下别人的代码,比较一下差异吧。
更新
看了一下别人的解法,这个题有两种解法:
双指针解法
看了别人的才知道原来我这种解法叫双指针解法,自己不会起名字。
这个题其实可以用暴力搜索的方式解决,但是太浪费时间了。改进的方法就是用空间换时间喽,先对原数组排序,快速排序的话时间复杂度是O(nlogn),同时需要记录排序前后数字的位置变化。然后分别在排序后的数组前后设置两个指针,每个比较头尾两个数的和,根据两数之和和target的大小比较,挪动指针,直到找到解。利用hashmap解法
hashmap存储的是数组中每个元素值在数组中的位置索引,即hashmap<Integer,Integer>, 第一个Integer是元素值,第二个Integer是位置索引。遍历一遍数组,将遍历结果存到hashmap中。
遍历数组,用target减当前值寻找另一个数,直接在hashmap中检索,存在key,并在value不等于当前值的索引,则成功找到解。
这种方法的时间复杂度是O(n)。
leet code: Two Sum相关推荐
- Leet Code OJ 刷题顺序参考
出现频度为5: 1. Leet Code OJ 1. Two Sum [Difficulty: Easy] 2. Leet Code OJ 8. String to Integer (atoi) ...
- Leet Code 力扣 - - 最短最优雅python解法带解析汇总
Leet Code 刷题笔记 - - 不求最快最省,但求最短最优雅 前言 代码精炼是 Python 的核心,同时能够反应对于语言的熟练程度,本项目目的在于汇总 leet code 最短最优雅的解法,拒 ...
- Leet Code OJ 119. Pascal's Triangle II [Difficulty: Easy]
题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return ...
- Leet Code题解 - 1559. Detect Cycles in 2D Grid 检测二维无向图中的环
Leet Code题解 -- 1559. Detect Cycles in 2D Grid 检测二维无向图中的环 前言 一.题目描述 二.思路整理 1. 审题 2. 分布实现步骤 2.1 将二维数组处 ...
- Leet Code OJ 112. Path Sum [Difficulty: Easy]
题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...
- Leet Code OJ 1. Two Sum [Difficulty: Easy]
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- java遍历斐波纳契数列_详解循环、迭代、递归、分治(Leet Code 509 斐波那契数列),实际运用...
Multiple solutions of Fibonacci (Python or Java) 本章是用英文写的,作为或想成为一名优秀的攻城狮,习惯阅读英文文档将使你受益良多.例如更好的查看最新版的 ...
- Leet Code OJ 4. Median of Two Sorted Arrays [Difficulty: Hard]
题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- Leet Code OJ 15. 3Sum[Difficulty: Medium]
题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find al ...
最新文章
- 搜索专题:问题 E: 挑战ACM迷宫
- From 《visual C++ 6.0开发工具与调试》
- 尼康单反相机测试软件,尼康D4S数码单反相机专题测试
- 第三次学JAVA再学不好就吃翔(part72)--Date类
- mac定时执行python_Python脚本实现在Mac上DIY定时提醒功能
- python抓取网站URL小工具
- SpringBoot 之集成 Spring AOP
- 【带着canvas去流浪(15)】threejs fundamentals翻译系列1-scene graph
- Android 系统(90)---JIT 编译器
- 漫谈 - 从技术业务骨干走向管理
- 用HTML5为你的网页添加音效(兼容Firefox 3.5+, IE 6-9, Safari 3.0+, Chrome 3.0+, Opera 10.5+)
- 第六章 现在给我道歉还来得及~修改登录cookie进后台上传muma
- 热备份冗余技术HSRP
- 【Turtlrbot3-burger】从零开始配置Turtlrbot3小车1
- 网站被黑中毒WebShell木马的解决方案
- 云豹短视频app源码中,标签选择功能的实现
- [MySQL免安装版本] 下载、配置、启动、密码修改
- matlab中如何打出心,如何在MATLAB中再现这个心形网格?
- 运行金蝶KIS记账王V10.0对系统有什么要求
- eNSP—静态路由+NAT网络地址转换