当前位置:网站首页>Leetcode 1: Two Sum
Leetcode 1: Two Sum
2022-07-17 00:13:00 【想做程序媛的小太阳】
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.
Example 1:
Input: nums = [2,7,11,15], target = 9 Output: [0,1] Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
Solution:
1. Brute Force
a. 这是我第一反应的解法,常规的循环遍历相加
class Solution {
public int[] twoSum(int[] nums, int target) {
int[] ans = new int[2];
for (int i = 0; i <= nums.length - 2; ++i) {
for (int j = i + 1; j <= nums.length - 1; ++j) {
if (nums[i] + nums[j] == target) {
ans[0] = i;
ans[1] = j;
return ans;
}
}
}
throw new IllegalArgumentException("no match found");
}
}b. 这种暴力搜索方法是做减法,更容易引申下面HashMap的解法
class Solution {
public int[] twoSum(int[] nums, int target) {
for (int i = 0; i < nums.length; ++i) {
for (int j = i + 1; j < nums.length; ++j) {
int complement = target - nums[i];
if (nums[j] == complement) {
return new int[] {i, j};
}
}
}
throw new IllegalArgumentException("no match found");
}
}2. HashMap
HashMap存储nums数组的值和角标的键值对,找到则返回,找不到则存入HashMap
此种解法时间复杂度为O(n)
注意,HashMap containsKey方法的时间复杂度为O(1),containsValue方法的时间复杂度为O(n)
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; ++i) {
int complement = target - nums[i];
if (map.containsKey(complement)) {
return new int[]{map.get(complement), i};
}
map.put(nums[i], i);
}
throw new IllegalArgumentException("no match found");
}
}边栏推荐
- ENVI_IDL:讀取所有OMI產品的NO2柱含量並計算月均值、季均值、年均值+解析
- 成信大ENVI_IDL第三周课堂内容1:读取OMI数据(HDF5文件)以及输出+解析
- 简述特征工程及其sklearn的实现
- Analysis of IGBT direct short circuit process
- LeetCode:动态规划中的子序列问题
- [vernacular analog 1] PN junction and diode
- Dueling DQN的理论基础及其代码实现【Pytorch + Pendulum-v0】
- Nacos configuration management
- SAE J1708/J1587 协议详解
- Oozie 集成 Ssh
猜你喜欢

06 design of smart electronic medicine box based on stm32

ENVI_IDL: 读取文本文件并输出为Geotiff格式+简单均值插值

windows安装mysql和jdbc

04基于ZigBee的室内无线定位系统设计

ENVI_IDL:讀取所有OMI產品的NO2柱含量並計算月均值、季均值、年均值+解析

05 design of street lamp control fault detection system based on ZigBee

Can protocol communication

02基于ZigBee的智能家居系统设计

On the properties and methods of list < t >

gdb+vscode进行调试3——vscode以及gdb远程调试
随机推荐
CMake常用命令
Factory method mode notes
ENVI_IDL:批量处理Modis Swath数据的重投影并输出为Geotiff格式+详细解析
芝诺悖论2 阿基里斯与乌龟
Remote sensing submission process
Nacos configuration management
Oozie integrated shell
DGC best practice: how to ensure that confidential data is not leaked when entering the lake?
ENVI_IDL:读取所有OMI产品的NO2柱含量并计算月均值、季均值、年均值+解析
Neutralizing Self-Selection Bias in Sampling for Sortition
在Oozie中配置 map-reduce workflow
指针常量与常量指针爱恨情仇
CAN协议通信
gdb+vscode进行调试1——使用CMakelist文件进行编译和调试+附加进程调试
Fairness in Deep Learning: A Computational Perspective
ENVI_ Idl: read the NO2 column content of all OMI products and calculate the monthly average, quarterly average, annual average + analysis
Clion 安装以及中开发ROS实现自动提示补全
指針常量與常量指針愛恨情仇
Startup mode of activity
Set up sqoop environment