LeetCode_001.两数之和
LeetCode_001
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那两个整数,并返回他们的数组下标。
你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
示例:
给定 nums = [2, 7, 11, 15], target = 9
因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]。
示例代码:
class Solution {
public int[] twoSum(int[] nums, int target) {
}
}
方法一:两层 for 循环暴力破解
- 测试用例:29个,{(2, 7, 11, 15), target=9, [0, 1]}、{(2, 5, 5, 11), target=10, [1, 2]} 等。
- 执行用时:69ms
- 内存消耗:37.4MB
- 时间复杂度:O(n^2)
- 空间复杂度:O(1)
class Solution {
public int[] twoSum(int[] nums, int target) {
for (int i = 0; i < nums.length - 1; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (target - nums[i] == nums[j]) {
return new int[] {i, j};
}
}
}
return null;
}
}
方法二:哈希映射题解
- 测试用例:29个,{(2, 7, 11, 15), target=9, [0, 1]}、{(2, 5, 5, 11), target=10, [1, 2]} 等。
- 执行用时:7ms
- 内存消耗:38.7MB
- 时间复杂度:O(n)
- 空间复杂度:O(n)
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(target - nums[i])) {
return new int[] { map.get(target - nums[i]), i };
}
map.put(nums[i], i);
}
throw new IllegalArgumentException("No two sum solution");
}
}
LeetCode_001.两数之和的更多相关文章
- 给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X
题目:给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X 思路一: 1,先采用归并排序对这个数组排序, 2,然后寻找相邻<k,i>的两数之和sum,找到恰好sum>x的 ...
- LeetCode 170. Two Sum III - Data structure design (两数之和之三 - 数据结构设计)$
Design and implement a TwoSum class. It should support the following operations: add and find. add - ...
- LeetCode 371. Sum of Two Integers (两数之和)
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...
- 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 ...
- [LeetCode] Two Sum IV - Input is a BST 两数之和之四 - 输入是二叉搜索树
Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...
- [LeetCode] 1. Two Sum 两数之和
Part 1. 题目描述 (easy) Given an array of integers, return indices of the two numbers such that they add ...
- Leetcode(一)两数之和
1.两数之和 题目要求: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重 ...
- 南大算法设计与分析课程OJ答案代码(1)中位数附近2k+1个数、任意两数之和是否等于给定数
问题1 用来测试的,就不说了 问题2:中位数附近2k+1个数 给出一串整型数 a1,a2,...,an 以及一个较小的常数 k,找出这串数的中位数 m 和最接近 m 的小于等于 m 的 k 个数,以及 ...
- 两数之和,两数相加(leetcode)
我们都知道算法是程序员成长重要的一环,怎么才能提高算法呢, 出来在网上看视频之外,动手练习是非常重要的.leetcode 就是一个非常好的锻炼平台. 1. 两数之和,在 leetcode 里面是属于 ...
随机推荐
- ELK的搭建以及使用
一.架构如图: 二.工作机制: 在需要收集日志的应用上安装filebeat(需要修改配置文件,配置文件稍后介绍),启动filebeat后,会收集该应用的日志推送给redis,然后logstash从re ...
- 1.基础CRUD
在ef中,CUD都使用Datacontext.SaveChange()进行保存. SavaChange方法在保存之前会自动调用DetectChanges方法检查DataContext中做了什么更改,以 ...
- sql修改表名字段名
修改字段: ALTER TABLE user_info CHANGE NAME name VARCHAR(10); 修改表名alter TABLE user_role RENAME user_info ...
- jsp-TagLib标签库
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ t ...
- 阿里服务器+Centos7.4+Tomcat+JDK部署
适用对象 本文档介绍如何使用一台基本配置的云服务器 ECS 实例部署 Java web 项目.适用于刚开始使用阿里云进行建站的个人用户. 配置要求 这里列出的软件版本仅代表写作本文档使用的版本.操作时 ...
- 初探html-9 链接
HTML 链接 HTML 使用超级链接与网络上的另一个文档相连.几乎可以在所有的网页中找到链接.点击链接可以从一张页面跳转到另一张页面. 尝试一下 - 实例 <!DOCTYPE html> ...
- 第98:svd原理
SVD分解:任何矩阵都可以分解成第一行的形式,3个相乘.UV都是正交矩阵,中间的是奇异值. 3个相乘的形式可以拆分.即奇异值*第一行*第一列.在相加. 奇异值有时很小,在这种情况下,丢掉,可以减少计算 ...
- iOS崩溃分析
崩溃的分析 最近修复了一些iOS项目的崩溃,想分析总结一下这些崩溃的原因,以及预防.崩溃的原因一般有下面几种: 内存访问错误(这个出现的比较多,原因多种多样) 非法指令的执行(超出权限范围内的指令) ...
- Laravel配置全局公共函数的方法步骤
前言 在laravel项目开发中,经常使用到公共函数,那如何在laravel配置全局公共函数呢??下面话不多说了,来一起看看详细的介绍吧 方法如下 在Laravel项目中我们常常需要定义一些全局的公共 ...
- tensorflow 屏蔽 Log
pip install alfred-py 在代码中加入 from alfred.dl.tf.common import mute_tf mute_tf()