当前位置:网站首页>Leetcode 1: Two Sum
Leetcode 1: Two Sum
2022-07-19 02:43:00 【Little sun who wants to be a program yuan】
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. This is the solution of my first reaction , Conventional loop ergodic addition
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. This violent search method is subtraction , It is easier to extend the following HashMap Solution method
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 Storage nums The value of array and the key value pair of subscript , Return if found , Deposit if not found HashMap
The time complexity of this solution is O(n)
Be careful ,HashMap containsKey The time complexity of the method is O(1),containsValue The time complexity of the method is 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");
}
}边栏推荐
猜你喜欢

Zabbix6.0通过iDRAC,IMM2监控DELL,IBM服务器硬件

安装软件提示无法定位程序输入点AddDllDirectory于动态链接库Kernel32.dll上(文末有下载地址)

Reprint: SQL injection common bypass

YUM仓库服务与PXE自动部署系统

Use JMeter to test services based on websocket protocol

Subnet division (see details)

安装.NET提示“无法建立到信任根颁发机构的证书链”(方法简单有下载地址)

Zabbix6.0监控vCenter7.0

单片机之数码管秒表的动态显示

The difference between cookies and sessions
随机推荐
Server knowledge (details)
Decentralized edge rendering meta universe protocol cadeus was invited to attend the cbaia 2022 summit to enable more Web3 application scenarios with technology
Interpretation of concurrent virtual users, RPS and TPS
Network layer transmission protocol (detailed)
MySQL初探
流量回放工具gor使用经验
UE4 notes
VLAN and trunk port configuration
Static routing (detailed)
使用Grafana8.5.2显示zabbix6.0的信息
NFS服务
The solution to the bounce and offset of unity3d game characters when jumping to the ground
String Full Permutation Problem
shell脚本接收和返回参数
innodb、Mysql结构、三种删除的区别
rsync远程同步(增量备份)
VLAN和TRUNK口配置
ARM 交叉编译器命名规则
LAMP平台部署及应用
全链路压测