当前位置:网站首页>1.两数之和
1.两数之和
2022-07-26 14:37:00 【superkcl2022】


1. 暴力解法
/** * 描述 暴力解法 O(n^2) */
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++){
if(nums[i] + nums[j] == target){
return new int[]{
i,j};
}
}
}
return new int[]{
};
}
public static void main(String[] args) {
Solution solution = new Solution();
int[] ints = solution.twoSum(new int[]{
2, 7, 11, 15}, 9);
System.out.println(ints[0] + " " + ints[1]);
}
}
2. 哈希表
/** * 项目名: leetcode * 包名: com.kcl.L1 * 文件名 Solution * 创建者 * 创建时间: 2022/7/10 10:49 AM * 描述 hash O(n) */
class Solution2 {
public int[] twoSum(int[] nums, int target) {
Map<Integer,Integer> map = new HashMap<>();
int len = nums.length;
for(int i = 0; i < len; i++){
//往前找
if(map.get(target-nums[i]) != null){
return new int[]{
i,map.get(target-nums[i])};
}else{
map.put(nums[i],i);
}
}
return new int[]{
};
}
public static void main(String[] args) {
Solution2 solution = new Solution2();
int[] ints = solution.twoSum(new int[]{
2, 7, 11, 15}, 9);
System.out.println(ints[0] + " " + ints[1]);
}
}
边栏推荐
- Seata的部署与微服务集成
- 全校软硬件基础设施一站式监控 ,苏州大学以时序数据库替换 PostgreSQL
- PyTorch中 nn.Conv2d与nn.ConvTranspose2d函数的用法
- Use of LINGO software
- Siamrpn++: evolution of deep network connected visual tracking
- How to evaluate the test quality?
- Tdengine helps Siemens' lightweight digital solution simicas simplify data processing process
- 当AI邂逅生命健康,华为云为他们搭建三座桥
- Introduction to C language must brush the daily question of the collection of 100 questions (1-20)
- 填问卷,领奖品 | 诚邀您填写 Google Play Academy 活动调研问卷
猜你喜欢

Keyboard shortcut to operate the computer (I won't encounter it myself)

《MySQL高级篇》五、InnoDB数据存储结构

Whaledi message queue stability improvement practice

C nanui related function integration

【使用工具条绘图】

PyTorch中 torch.nn与torch.nn.functional的区别

CAS based SSO single point client configuration

Win11运行虚拟机死机了?Win11运行VMware虚拟机崩溃的解决方法

Leetcode1170- compare the occurrence frequency of the minimum letter of the string (the corresponding occurrence frequency of each string minimum element in the map set storage array)

Tips for unity transparent channel
随机推荐
Realize the full link grayscale based on Apache APIs IX through MSE
Is the MySQL index tree built before each search or when the index is built?
[nuxt 3] (XII) project directory structure 1
Leetcode1170- compare the occurrence frequency of the minimum letter of the string (the corresponding occurrence frequency of each string minimum element in the map set storage array)
[1.2. return and risk of investment]
【使用工具条绘图】
[ostep] 02 virtualized CPU - process
"Baget" takes you one minute to build your own private nuget server
Whaledi message queue stability improvement practice
请问数据库规范的文档吗 参考一下?
我的创作纪念日-从心出发
CAS单点登录
Error reported by Nacos enabled client
保证接口数据安全的10种方案
过滤器和拦截器的区别
JS creative range select drag and drop plug-ins
Unity learning notes – infinite map
Mysql-03 database operation
【1.2.投资的收益和风险】
RPN: region proposal networks