当前位置:网站首页>【leetcode】day1
【leetcode】day1
2022-07-07 21:52:00 【橘の月半喵】
开始刷题!
文章目录
001 :两数之和
给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。你可以按任意顺序返回答案。
具体问题见网址
001.暴力解法 (双for循环)
001.暴力解法 (双for循环) java版
解法时间复杂度约为 O ( n 2 ) O(n^2) O(n2)
class Solution {
public int[] twoSum(int[] nums, int target) {
int[] result=new int[2];
for (int i=0;i<(nums.length-1);i++){
//java中的length为 nums.length
for (int j=i+1;j<nums.length;j++){
if ((nums[i]+nums[j])==target)
{
result[0]=i;
result[1]=j;
return result;
}
}
}
return result; // 如果只有上面的return 会导致部分循环没有返回值,导致报错
}
}
001.暴力解法 (双for循环) pyhton版
class Solution(object):
def twoSum(self, nums, target):
result=[];
for i in range(0,len(nums)): # 注意 python for循环 if 后面加“:”
for j in range(i+1,len(nums)):
if ((nums[i]+nums[j])==target):
result.append(i);
result.append(j);
return result
001.hash解法
利用hash表的方式求解,关键在于hash表的查找速度特别块。此外 python的字典与hash表类似
001.hashmap java版
import java.util.HashMap;
import java.util.Map;
public int[] twoSum(int[] nums, int target) {
int[] result = new int[2];
if(nums == null || nums.length == 0){
return result ;
}
Map<Integer, Integer> map = new HashMap<>(); // 建立k-v ,一一对应的哈希表
// 注意 hash表,数组的值作为key值,数组的下标作为value值
//(为什么?在查找数组的时候我们关注的是数组的值,而不是下标,我们需要以值为“导向” 来查找所需要的数组).
// 不用担心数组的值作为key值会重复,因为如果重复的话,覆盖即可
for(int i = 0; i < nums.length; i++){
int temp = target - nums[i];
if(map.containsKey(temp)){
// 判断 hash表中是否存在target - nums[i] 这个值,如果存在直接返回
result [1] = i;
result [0] = map.get(temp);
return result;
}
// 如果不存在就将 键值对存入,以备查找(这些键值对是已经确定的、任意两个数字之间没有所需要和等于target的数组)
map.put(nums[i], i);
}
return result ;
}
001.dict pyhton
class Solution(object):
def twoSum(self, nums, target):
dict1={
};
for index,num in enumerate(nums):
temp=target-num;
if temp in dict1:
return [dict1[temp],index]; # 注意 以 key 访问 字典时 要用方括号!!!
dict1[num]=index;
return None;
边栏推荐
- HDU 4747 Mex「建议收藏」
- 2021icpc Shanghai h.life is a game Kruskal reconstruction tree
- 2022注册测绘师备考开始 还在不知所措?手把手教你怎么考?
- MySQL架构
- Progress broadcast | all 29 shield machines of Guangzhou Metro Line 7 have been launched
- USB (XIV) 2022-04-12
- Mysql索引优化实战一
- 【7.5】15. 三数之和
- How to login and enable synchronization function in Google browser
- Anxin vb01 offline voice module access intelligent curtain guidance
猜你喜欢

Installing gradle

ESP at installation esp8266 and esp32 versions

Vulnerability recurrence ----- 49. Apache airflow authentication bypass (cve-2020-17526)

KeePass realizes automatic input of web pages

Spark 离线开发框架设计与实现

Progress broadcast | all 29 shield machines of Guangzhou Metro Line 7 have been launched

产业共融新势能,城链科技数字峰会厦门站成功举办

Anxinco EC series modules are connected to the multi protocol access products of onenet Internet of things open platform

C number of words, plus ¥, longest word, average value

Explain
随机推荐
产业共融新势能,城链科技数字峰会厦门站成功举办
JNI uses asan to check memory leaks
Three questions TDM
HDU 4747 Mex「建议收藏」
Happy gathering time
给出一个数组,如 [7864, 284, 347, 7732, 8498],现在需要将数组中的数字拼接起来,返回「最大的可能拼出的数字」
8.31 Tencent interview
FPGA basics catalog
Ros2 topic (03): the difference between ros1 and ros2 [01]
The efficient s2b2c e-commerce system helps electronic material enterprises improve their adaptability in this way
B_QuRT_User_Guide(38)
Mysql索引优化实战二
USB (XVI) 2022-04-28
Open source hardware small project: anxinco esp-c3f control ws2812
Right click the idea file to create new. There is no solution to create new servlet
USB (XVII) 2022-04-15
Summary of common methods of object class (September 14, 2020)
SAP HR 劳动合同信息 0016
How to login and enable synchronization function in Google browser
【汇总】看过的一些Panel与视频