首页 > 其他分享 >Leetcode刷题记录本

Leetcode刷题记录本

时间:2023-08-09 16:45:23浏览次数:29  
标签:记录本 target nums int List records type Leetcode 刷题

Leetcode刷题记录本

ID: 1

点击查看代码
  1. 暴力破解法
class Solution(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
		# 暴力破解法
        for i in range(len(nums)):
            for j in range(len(nums)):
                if nums[i]+nums[j]==target:
                    return [i,j]
  1. 使用字典,即key-map
class Solution(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        records = dict()
        for index,value in enumerate(nums):
            if target-value in records:
                return [records[target-value],index]
            records[value] = index

标签:记录本,target,nums,int,List,records,type,Leetcode,刷题
From: https://www.cnblogs.com/lwp-nicol/p/17617214.html

相关文章

  • leetcode:下一个排列
     classSolution{public:voidnextPermutation(vector<int>&nums){intn=nums.size();inti=n-2;while(i>=0&&nums[i]>=nums[i+1]){//从后向前,找到第一个降序的,一直升序说明最大i--;}if(i<0......
  • LeetCode -- 127. 单词接龙
     方法一:双向广搜classSolution{public:intladderLength(stringbeginWord,stringendWord,vector<string>&wordList){set<string>se;for(autoit:wordList){se.insert(it);}if(!se.count(en......
  • LeetCode 热题 100 之 240. 搜索二维矩阵 II
    题目编写一个高效的算法来搜索mxn矩阵matrix中的一个目标值target。该矩阵具有以下特性:每行的元素从左到右升序排列。每列的元素从上到下升序排列。示例一输入:matrix=[[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]],target=5......
  • 递归反转链表局部[labuladong-刷题打卡 day8]
    写在前面前两天刷题打卡,感觉东哥的代码模板没有题解中的简洁,或者那些极限优化的代码中有很多优化技巧,但今天去感受递归的含义的时候,觉得毕竟我现在是在学习算法,理解算法含义才是学习的意义。至于优化,那是之后的事,所以刷题的时候不必过于追求简洁,就像追求简洁而降低可读性一样属......
  • LeetCode 16. 3Sum Closest 双指针+排序
    Givenanintegerarraynumsoflengthnandanintegertarget,findthreeintegersinnumssuchthatthesumisclosesttotarget.Returnthesumofthethreeintegers.Youmayassumethateachinputwouldhaveexactlyonesolution.Solution先将原数组排序,然......
  • 【刷题笔记】9. Palindrome Number
    题目Determinewhetheranintegerisapalindrome.Aninteger is a palindromewhenit readsthesamebackwardasforward.Example1:Input:121Output:trueExample2:Input:-121Output:falseExplanation:Fromlefttoright,itreads-121.Fromrightto......
  • 刷题记录(二)
    catcat-new点击主页的一个链接http://61.147.171.105:55571/info?file=Persiancat.txt可能存在任意文件读取漏洞,读取/etc/passwd文件读取当前进程的命令行参数?file=../../proc/self/cmdline,发现有一个通过python启动app.py的命令,得知网站使用的是python的flask框架。读取a......
  • #yyds干货盘点# LeetCode程序员面试金典:水壶问题
    1.简述:有两个水壶,容量分别为 jug1Capacity 和jug2Capacity升。水的供应是无限的。确定是否有可能使用这两个壶准确得到 targetCapacity升。如果可以得到 targetCapacity 升水,最后请用以上水壶中的一或两个来盛放取得的 targetCapacity 升水。你可以:装满任意一个水壶清空......
  • 【刷题笔记】8. String to Integer (atoi)
    题目Implementthe myAtoi(strings) function,whichconvertsastringtoa32-bitsignedinteger(similartoC/C++'s atoi function).Thealgorithmfor myAtoi(strings) isasfollows:Readinandignoreanyleadingwhitespace.Checkifthenextcharact......
  • LeetCode 周赛上分之旅 #38 结合排序不等式的动态规划
    ⭐️本文已收录到AndroidFamily,技术和职场问题,请关注公众号[彭旭锐]和BaguTreePro知识星球提问。学习数据结构与算法的关键在于掌握问题背后的算法思维框架,你的思考越抽象,它能覆盖的问题域就越广,理解难度也更复杂。在这个专栏里,小彭与你分享每场LeetCode周赛的解题报告,一......