leetcode100记录

  1. 两数之和
python 复制代码
class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        result = []
        for i in range(len(nums)):
            for j in range(i+1, len(nums)):
                if target == (nums[i] + nums[j]) and i!=j:
                    return [i,j]
  1. 49. 字母异位词分组
python 复制代码
class Solution:
    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
        map1 = {}
        for s in strs:
            s1 = sorted(list(s))
            s1 = ''.join(s1)
            map1[s1] = map1.get(s1, []) + [s]

        return [item for item in map1.values()]
    1. 最长连续序列
python 复制代码
class Solution:
    def longestConsecutive(self, nums: List[int]) -> int:
        nums1 = sorted(list(set(nums)))
        count = 0
        result = 0
        if nums == []:
            return 0
        for i in range(len(nums1)-1):
            
            if nums1[i] +1 == nums1[i+1]:
                count+=1
            else:
                count = 0
            result = max(count, result)
        return result + 1
  1. 283. 移动零
python 复制代码
class Solution:
    def moveZeroes(self, nums: List[int]) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        for i in range(len(nums)):
            if nums[i]==0:
                nums.remove(nums[i])
                nums.append(0)
        return nums
  1. 11. 盛最多水的容器
python 复制代码
class Solution:
    def maxArea(self, height: List[int]) -> int:
        left, right, res = 0, len(height)-1, 0
        while(left<right):
            if height[left] < height[right]:
                res = max(res, height[left]*(right-left))
                left += 1
            else:
                res = max(res, height[right]*(right-left))
                right -= 1
        return res
        
  1. 15. 三数之和
python 复制代码
class Solution:
    def threeSum(self, nums: List[int]) -> List[List[int]]:
        nums.sort()
        res, k =[], 0
        for k in range(len(nums)-2):
            i, j = k+1, len(nums)-1
            if nums[k]>0: break
            if k> 0 and nums[k]==nums[k-1]:continue
            while i< j:
                s = nums[i] + nums[j] + nums[k]
                if s < 0:
                    i+=1
                    while i < j and nums[i]==nums[i-1]:i+=1
                elif s > 0:
                    j-=1
                    while i<j and nums[j]==nums[j+1]:j-=1
                else:
                    res.append([nums[i],nums[j],nums[k]])
                    i+=1
                    j-=1
                    while i < j and nums[i]==nums[i-1]:i+=1  
                    while i < j and nums[j]==nums[j+1]:j-=1

        return res      

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

思路:滑动窗口,用window存遍历过的元素,一旦碰到相同的元素,就进入while会不断删除重复的那个元素之前的以及这个重复的元素。

python 复制代码
class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        left, result = 0, 0
        window = []
        for right, c in enumerate(s):
            while c in window:
                window.remove(window[0])
                left+=1
            window.append(c)
            result = max(result, right-left+1)
        return result

438. 找到字符串中所有字母异位词

python 复制代码
class Solution:
    def findAnagrams(self, s: str, p: str) -> List[int]:
        slow = 0
        res = []
        list2 = list(p)
        list2.sort()
        for fast in range(len(p)-1,len(s)):
            list1 = (list(s[slow:fast+1]))
            list1.sort()
            if list1==list2:
                res.append(slow)
            slow+=1

        return res
python 复制代码
class Solution:
    def findAnagrams(self, s: str, p: str) -> List[int]:
        s_len, p_len = len(s), len(p)
        if s_len < p_len:
            return []
        s_count = [0]*26
        p_count = [0]*26
        ans = []

        for i in range(p_len):
            s_count[ord(s[i])-97] +=1
            p_count[ord(p[i])-97] +=1
        if p_count == s_count:
            ans.append(0)
        for i in range(s_len-p_len):
            s_count[ord(s[i])-97]-=1
            s_count[ord(s[i+p_len])-97]+=1
        
            if s_count == p_count:
                ans.append(i+1)
        
        return ans
相关推荐
碧海蓝天20228 分钟前
C++法则21:避免将#include放在命名空间内部。
开发语言·c++
兮动人15 分钟前
Java应用全链路故障排查实战指南:从系统资源到JVM深度诊断
java·开发语言·jvm
R-sz26 分钟前
导出word并且插入图片
开发语言·c#·word
CodeWithMe26 分钟前
【读书笔记】《C++ Software Design》第一章《The Art of Software Design》
开发语言·c++
脑袋大大的44 分钟前
判断当前是否为钉钉环境
开发语言·前端·javascript·钉钉·企业应用开发
运器1231 小时前
【一起来学AI大模型】PyTorch DataLoader 实战指南
大数据·人工智能·pytorch·python·深度学习·ai·ai编程
音元系统1 小时前
Copilot 在 VS Code 中的免费替代方案
python·github·copilot
超龄超能程序猿1 小时前
(5)机器学习小白入门 YOLOv:数据需求与图像不足应对策略
人工智能·python·机器学习·numpy·pandas·scipy
Wy. Lsy1 小时前
Kotlin基础学习记录
开发语言·学习·kotlin
Tanecious.2 小时前
C++--红黑树
开发语言·c++