当前位置:网站首页>【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;
边栏推荐
- Spark 离线开发框架设计与实现
- Fibonacci number of dynamic programming
- Summary of SQL single table query 2020.7.27
- LM12丨Rolling Heikin Ashi二重K线滤波器
- 【7.4】25. Turn over the linked list in groups of K
- Progress broadcast | all 29 shield machines of Guangzhou Metro Line 7 have been launched
- The for loop realizes 1-100 addition and eliminates the 4-digit tail number
- LDO voltage stabilizing chip - internal block diagram and selection parameters
- PCB wiring rules of PCI Express interface
- 平衡二叉树【AVL树】——插入、删除
猜你喜欢

SAP HR 社会工作经历 0023

Unity3d learning notes 5 - create sub mesh

C simple question one

2022 Season 6 perfect children's model Shaanxi finals came to a successful conclusion

Digital procurement management system for fresh food industry: help fresh food enterprises solve procurement problems and implement online procurement throughout the process

Design and implementation of spark offline development framework

SAP memory parameter tuning process

LM12丨Rolling Heikin Ashi二重K线滤波器

2022第六季完美童模陕西总决赛圆满落幕

B_ QuRT_ User_ Guide(36)
随机推荐
[stm32+esp8266 connects to Tencent cloud IOT development platform 3] stm32+esp8266-01s dynamically registers devices on Tencent cloud (at instruction mode) -- with source code
How to change the formula picture in the paper directly into the formula in word
SAP HR labor contract information 0016
B_ QuRT_ User_ Guide(36)
生鲜行业数字化采购管理系统:助力生鲜企业解决采购难题,全程线上化采购执行
Ros2 topic (03): the difference between ros1 and ros2 [02]
城联优品作为新力量初注入,相关上市公司股价应声上涨150%
C # exchange number, judge to pass the exam
Coreseek: the second step is index building and testing
[STM32 + esp-12s connect Tencent cloud IOT development platform 1] creation of cloud platform and burning of at firmware
Three questions TDM
2022第六季完美童模陕西总决赛圆满落幕
【7.4】25. Turn over the linked list in groups of K
【汇总】看过的一些Panel与视频
HDU 4747 Mex「建议收藏」
SAP HR social work experience 0023
Map operation execution process
Tree background data storage (using webmethod) [easy to understand]
MySQL Index Optimization Practice I
JNI uses asan to check memory leaks