Leetcode TOP5 题目和解答:这里只提供一种解题思路,希望引导大家持续学习,可以采用FlowUs息流记录自己的学习

LeetCode 是一个在线编程平台,它提供了大量的算法题目供用户练习。

TOP5题目通常指的是 LeetCode 网站上最受欢迎的前5道题目。

以下是 LeetCode TOP5 题目的列表以及它们常见的解题思路和代码示例。

题目1 两数之和

两数之和 - 1. Two Sum

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

You can return the answer in any order.

Example 1:

Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].

Example 2:

Input: nums = [3,2,4], target = 6
Output: [1,2]

Example 3:

Input: nums = [3,3], target = 6
Output: [0,1]

Constraints:

  • 2 <= nums.length <= 104
  • -109 <= nums[i] <= 109
  • -109 <= target <= 109
  • Only one valid answer exists.


  • 解题思路:使用哈希表存储遍历过的数字及其索引,对于每个数字,检查目标值(当前数字的和)是否在哈希表中。
def twoSum(nums, target):
    hash_map = {}
    for i, num in enumerate(nums):
        if target - num in hash_map:
            return [hash_map[target - num], i]
        hash_map[num] = i

题目14 最长公共前缀

最长公共前缀 - 14. Longest Common Prefix
 

Write a function to find the longest common prefix string amongst an array of strings.

If there is no common prefix, return an empty string "".

Example 1:

Input: strs = ["flower","flow","flight"]
Output: "fl"

Example 2:

Input: strs = ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.

Constraints:

  • 1 <= strs.length <= 200
  • 0 <= strs[i].length <= 200
  • strs[i] consists of only lowercase English letters.


  • 解题思路:使用横向扫描的方法,逐个字符比较所有字符串,直到遇到不匹配的字符。
def longestCommonPrefix(strs):
    if not strs: return ""
    shortest_str = min(strs, key=len)
    for i, char in enumerate(shortest_str):
        for other in strs:
            if other[i] != char:
                return shortest_str[:i]
    return shortest_str

题目3 无重复字符的最长子串
 3. Longest Substring Without Repeating Characters
 

Given a string s, find the length of the longest 

substring

 without repeating characters.

Example 1:

Input: s = "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.

Example 2:

Input: s = "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.

Example 3:

Input: s = "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.

Constraints:

  • 0 <= s.length <= 5 * 104
  • s consists of English letters, digits, symbols and spaces.

  • 解题思路:使用滑动窗口,记录窗口内字符的索引,如果遇到重复字符,移动窗口的左边界。

def lengthOfLongestSubstring(s):
    char_map = {}
    start = maxLength = 0
    for end in range(len(s)):
        if s[end] in char_map and char_map[s[end]] >= start:
            start = char_map[s[end]] + 1
        char_map[s[end]] = end
        maxLength = max(maxLength, end - start + 1)
    return maxLength

题目4 寻找两个正序数组的中位数

- 4. Median of Two Sorted Arrays
 

Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.

The overall run time complexity should be O(log (m+n)).

Example 1:

Input: nums1 = [1,3], nums2 = [2]
Output: 2.00000
Explanation: merged array = [1,2,3] and median is 2.

Example 2:

Input: nums1 = [1,2], nums2 = [3,4]
Output: 2.50000
Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.

Constraints:

nums1.length == m

nums2.length == n

0 <= n <= 1000

0 <= m <= 1000

1 <= m + n <= 2000

-10^6 <= nums1[i], nums2[i] <= 10^6
 

  • 解题思路:使用二分查找法来找到两个数组的分割点,使得左边的和等于右边的和,或者左边的和比右边的和多1。

def findMedianSortedArrays(nums1, nums2):
    nums1, nums2 = sorted(nums1 + nums2)
    return (nums1[len(nums1) // 2] + nums1[(len(nums1) - 1) // 2]) / 2

题目10 正则表达式匹配
 

10. 正则表达式匹配

给你一个字符串 s 和一个字符规律 p,请你来实现一个支持 '.' 和 '*' 的正则表达式匹配。

  • '.' 匹配任意单个字符
  • '*' 匹配零个或多个前面的那一个元素

所谓匹配,是要涵盖 整个 字符串 s的,而不是部分字符串。

示例 1:

输入:s = "aa", p = "a"
输出:false
解释:"a" 无法匹配 "aa" 整个字符串。

示例 2:

输入:s = "aa", p = "a*"
输出:true
解释:因为 '*' 代表可以匹配零个或多个前面的那一个元素, 在这里前面的元素就是 'a'。因此,字符串 "aa" 可被视为 'a' 重复了一次。

示例 3:

输入:s = "ab", p = ".*"
输出:true
解释:".*" 表示可匹配零个或多个('*')任意字符('.')。

提示:

  • 1 <= s.length <= 20
  • 1 <= p.length <= 20
  • s 只包含从 a-z 的小写字母。
  • p 只包含从 a-z 的小写字母,以及字符 . 和 *
  • 保证每次出现字符 * 时,前面都匹配到有效的字符
    解题思路:使用动态规划,创建一个二维数组 dp,其中 dp[i][j] 表示字符串 s 的前 i 个字符与字符串 p 的前 j 个字符是否匹配。
    def isMatch(s, p):
        dp = [[False] * (len(p) + 1) for _ in range(len(s) + 1)]
        dp[0][0] = True
        for j in range(2, len(p) + 1):
            if p[j - 1] == '*':
                dp[0][j] = dp[0][j - 2]
        for i in range(1, len(s) + 1):
            for j in range(1, len(p) + 1):
                if p[j - 1] == '*':
                    dp[i][j] = dp[i][j - 2] or (s[i - 1] == p[j - 2] or p[j - 2] == '.') and dp[i - 1][j]
                else:
                    dp[i][j] = dp[i - 1][j - 1] and (s[i - 1] == p[j - 1] or p[j - 1] == '.')
        return dp[-1][-1]

    FlowUs 是一款灵活的笔记和知识管理工具,它提供了多种功能,使得工程师和其他专业人士能够记录和组织他们的代码、学习笔记和思考。以下是一些FlowUs的主要特点,以及它们如何帮助工程师进行记录和思考:

  • 多维数据视图:FlowUs 支持多种数据视图,如列表视图、看板视图、日历视图等,这使得用户可以根据自己的需求灵活地组织信息。

  • 嵌入代码块:FlowUs 允许用户在笔记中嵌入代码块,这对于记录代码片段、代码示例或者编程思想非常有用。

  • Markdown 支持:FlowUs 支持 Markdown 语法,这意味着用户可以使用简洁的格式来编写笔记,包括格式化文本、链接、列表等。

  • 知识库构建:用户可以在 FlowUs 中创建知识库,将相关的笔记和文档组织在一起,构建一个系统化的知识体系。

  • 团队协作:FlowUs 支持团队协作功能,工程师可以与团队成员共享笔记、文档和代码,实现知识的共享和团队协作。

  • 集成第三方服务:FlowUs 可以与许多第三方服务集成,如GitHub、Google Drive等,方便用户将外部资源链接到笔记中。

  • 自定义工作流:用户可以根据自己的工作流程自定义 FlowUs 的使用方式,比如设置提醒、自动化任务等。

  • 移动和桌面应用:FlowUs 提供了移动和桌面应用,使用户可以在任何设备上访问和编辑他们的笔记。

  • 数据安全:FlowUs 提供数据加密和备份功能,确保用户数据的安全性。

  • 学习和思考记录:工程师不仅可以记录代码,还可以记录学习过程中的思考、问题和解决方案,帮助他们更好地理解和掌握新知识。

    FlowUs 的这些功能使得它成为工程师和其他专业人士进行知识管理和个人生产力提升的有力工具。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值