当前位置:网站首页>【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;
边栏推荐
- POJ2392 SpaceElevator [DP]
- Design and implementation of spark offline development framework
- IDEA 2021.3. X cracking
- Fibonacci number of dynamic programming
- SAP HR labor contract information 0016
- MATLAB signal processing [Q & A essays · 2]
- How to change the formula picture in the paper directly into the formula in word
- 建筑建材行业SRM供应商云协同管理平台解决方案,实现业务应用可扩展可配置
- PCB wiring rules of PCI Express interface
- Coreseek: the second step is index building and testing
猜你喜欢

C method question 1

电子设备行业智能供应链协同平台解决方案:解决低效, 赋能产业数字化升级

Anxinco esp32-a1s development board is adapted to Baidu dueros routine to realize online voice function

SAP HR 社会工作经历 0023

Lm12 rolling heikin Ashi double K-line filter

ASP. Net core middleware request processing pipeline

2022注册测绘师备考开始 还在不知所措?手把手教你怎么考?

List. How to achieve ascending and descending sort() 2020.8.6

Anxin vb01 offline voice module access intelligent curtain guidance

Design and implementation of spark offline development framework
随机推荐
0-1 knapsack problem
IDEA 2021.3. X cracking
Display the server hard disk image to the browser through Servlet
The 19th Zhejiang Provincial College Programming Contest VP record + supplementary questions
[STM32 + esp-12s connect Tencent cloud IOT development platform 1] creation of cloud platform and burning of at firmware
Turbo introder common scripts
ASP. Net query implementation
How to login and enable synchronization function in Google browser
包装行业智能供应链S2B2B商城解决方案:开辟电商消费新生态
Digital procurement management system for fresh food industry: help fresh food enterprises solve procurement problems and implement online procurement throughout the process
Summary of common methods of object class (September 14, 2020)
2022 Season 6 perfect children's model Shaanxi finals came to a successful conclusion
PCB wiring rules of PCI Express interface
ASP. Net open web page
Entity层、DAO层、Service层、Controller层 先后顺序
Open source hardware small project: anxinco esp-c3f control ws2812
【7.4】25. K 个一组翻转链表
C # exchange number, judge to pass the exam
ASP. Net core middleware request processing pipeline
Oracle database backup and recovery