欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

leetcode每日

程序员文章站 2023-10-16 09:23:03
目录1. 两数之和2. 两数相加1. 两数之和题目:给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。示例:给定 nums = [2, 7, 11, 15], target = 9因为 nums[0] + nums[1] = 2 + 7 = 9所以返回 [0, 1]https://leetcode-cn.com/problems/two-s...

https://leetcode-cn.com/problemset/hot-100/

1. 两数之和

  • 题目:
    给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
    你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。
  • 示例:
    给定 nums = [2, 7, 11, 15], target = 9
    因为 nums[0] + nums[1] = 2 + 7 = 9
    所以返回 [0, 1]

https://leetcode-cn.com/problems/two-sum/

  • 题解:
  1. 方法一:用 Python 中 list 的相关函数求解
  • 思路:
    计算num2=target - num1,如果num2 in nums,继续计算如果num2=num1,并且在nums中只出现了一次,则说明找到是num1本身;如果不是,继续查找num2的索引,得出返回值
  • 代码:
class Solution(object):
    def twoSum(self, nums, target):
            lens = len(nums)
            for i in range(lens):
                num1 = nums[i]
                num2 = target - num1
                if num2 in nums:
                    if num1 == num2 and nums.count(num1)==1:
                        continue
                    return [i, nums.index(num2, i+1)]

#count() 方法用于统计字符串里某个字符出现的次数
#index() 方法检测字符串中是否包含子字符串str
  1. 方法二:用字典模拟哈希求解
  • 思路:
    定义一个字典,enumerate() 同时列出nums的数据下标和数据,计算num2,如果num2 in hashmap,返回hashmap[num2]即value,也就是num2在nums中对应的下标,得到返回值。如果num2不在hashmap中,则将num和index放入hashmap,即num:index。
  • 代码:
class Solution:
    def twoSum(self, nums, target):
        hashmap={}
        for index, num in enumerate(nums):
            num2 = target - num
            if num2 in hashmap:
                return [hashmap[num2],index]
            hashmap[num] = index
#enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,

https://leetcode-cn.com/problems/two-sum/solution/xiao-bai-pythonji-chong-jie-fa-by-lao-la-rou-yue-j/
https://blog.csdn.net/starmoth/article/details/86444259

2. 两数相加

  • 题目:
    给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照逆序的方式存储的,并且它们的每个节点只能存储一位数字。如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。
    您可以假设除了数字 0 之外,这两个数都不会以 0 开头。
  • 示例:
    输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
    输出:7 -> 0 -> 8
    原因:342 + 465 = 807

https://leetcode-cn.com/problems/add-two-numbers/

3.无重复字符的最长子串

  • 题目:
    给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。
  • 示例:
    输入: “abcabcbb”
    输出: 3
    解释: 因为无重复字符的最长子串是 “abc”,所以其长度为 3。
  • 题解:
  • 思路:
    滑动窗口
    定义一个dic,将s中元素和其下标放入dic中,如果s中第一个元素不在dic中,则以元素值为key,下标为value放入dic,并且更新无重复字符的最长字串长度res。如果s中某元素在dic中有值,则将无重复字符的最长字串的起始点更新为目前该元素的下标。
class Solution(object):
    def lengthOfLongestSubstring(self, s):
        start,res,newdic=-1,0,{}
        for i,c in enumerate(s):#按循环顺序,把s中的元素挨个放到新的newdic里去
            if c in newdic and newdic[c]>start:
                start=newdic[c]#重新定位start指针
                newdic[c]=i#给这个重复字母赋上顺序值
            else:#newdic里还没有这个重复字母,且不需要重新定位指针(重复字母在start之前,说明已经算过了,略掉)
                    newdic[c]=i
                    res=max(res,i-start)   #更新最大子串长度   
        return res

https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/

本文地址:https://blog.csdn.net/xingqi4543/article/details/107333643