leetcode刷题(7.30总结)

1、两数之和

题目描述:https://leetcode.cn/problems/two-sum/

class Solution:
    def twoSum(self, nums: list[int], target: int) -> list[int]:
        for index1, num1 in enumerate(nums):
            index2 = index1 + 1
            if (target - num1) in nums[index2:]:
                return [index1, (nums[index2:].index(target - num1) + index2)]


2、回文数

题目描述:https://leetcode.cn/problems/palindrome-number/

    public boolean isPalindrome(int x) {
        if (x < 0) {
            return false;
        }
        int length = 0, tmp = x;
        while (tmp > 0) {
            length++;
            tmp /= 10;
        }
        
        int left = 0, count = 0;
        while (count < length / 2) {
            left = left * 10 + x % 10;
            x /= 10;
            count++;
        }
        return left == x || (length % 2 != 0 && left == x / 10);
    }

3、正则表达式匹配

题目描述:https://leetcode.cn/problems/regular-expression-matching/

class Solution {
    public boolean isMatch(String s, String p) {
        int m = s.length();
        int n = p.length();
        boolean[][] dp = new boolean[m + 1][n + 1];
        
        #初始化,字符串 p 是否与 空字符串 匹配
        dp[0][0] = true;
        for(int i = 1; i <= n; ++i) {
            if(p.charAt(i - 1) == '*') {
                dp[0][i] = dp[0][i - 2];
            }
        }
        #状态转移
        for(int i = 1; i <= m; ++i) {
            for(int j = 1; j <= n; ++j) {
                # p[j]='.' 表示与任意字符匹配
                if(p.charAt(j - 1) == '.') { 
                    dp[i][j] = dp[i - 1][j - 1];

                # p[j]='*',可以根据情况选择 消除 (匹配 0 个),或者 复制 多个 字符
                } else if(p.charAt(j - 1) == '*') { 

                    # 当不匹配的时候,必须消除; 如果匹配,同样可以选择消除
                    dp[i][j] = dp[i][j - 2]; 

                    # 如果 s[i]==p[j - 1] 或者 p[j - 1]=='.' 万能匹配,可以选择复制多个 p[j-1]
                    if(p.charAt(j - 2) == '.' || p.charAt(j - 2) == s.charAt(i - 1)) {
                        dp[i][j] |= dp[i - 1][j];
                    }

                } else { # 普通字符,直接判断是否匹配即可
                    dp[i][j] = dp[i - 1][j - 1] && (s.charAt(i - 1) == p.charAt(j - 1));
                }
            }
        }
        return dp[m][n];
    }
}

4、盛最多水的容器

题目描述:https://leetcode.cn/problems/container-with-most-water/

class Solution:
    def maxArea(self, height: List[int]) -> int:
        max_vol = 0
        left = 0
        right = len(height) - 1

        while left < right:
            # 水的高度只与板子中较小者相关,较长者不起作用
            if height[right] > height[left]:
                vol = (right - left)*height[left]
                left += 1
            else:
                vol = (right - left)*height[right]
                right -= 1

            max_vol = vol if vol > max_vol else max_vol

        return max_vol

5、整数转罗马数字

题目描述:https://leetcode.cn/problems/integer-to-roman/

class Solution:
    def intToRoman(self, num: int) -> str:
        k = num
        def Int2R(n, l):
            dic = {1:['I','V'], 10:['X','L'], 100:['C','D'], 1000:['M', '']}
            r = ''
            if 0 <= n <= 3: r = dic[l][0]*n
            elif n == 4: r = dic[l][0] + dic[l][1]
            elif 5 <= n <= 8: r = dic[l][1] + dic[l][0]*(n-5)
            else: r = dic[l][0] + dic[l*10][0]
            return r
        res = ''
        level = 1
        while k != 0:
            tmp = k%10
            s = Int2R(tmp, level)
            res = s + res
            level *= 10
            k //= 10
        return res

6、最长公共前缀

题目描述:https://leetcode.cn/problems/longest-common-prefix/

class Solution:
    def longestCommonPrefix(self, strs: List[str]) -> str:
        pref = ""
        lengthVector = []
        for a in range(len(strs)):
            lengthVector.append(len(strs[a]))
        minLength = min(lengthVector)
        if minLength:
            for i in range(minLength):
                mark = True
                for j in range(len(strs)):
                    if not strs[j].startswith(strs[0][0:i+1]) :
                        mark = False
                        break
                if mark:
                    pref += strs[0][i]
        else:
            pref = ""
        return pref

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值