LeetCode刷题(9、11、12、14、15)

目录

一、回文数

1、题目描述

2、题解

3、源码

 二、盛最多水的容器

1、题目描述

2、题解

3、源码

三、整数转罗马数字

1、题目描述

2、题解

3、源码 

四、三数之和

1、题目描述

2、题解

3、源码

五、最长公共前缀

1、题目描述

2、题解

3、源码


一、回文数

1、题目描述

2、题解

3、源码

class Solution:
    def isPalindrome(self, x: int) -> bool:
        s = str(x)
        if (s[::] == s[::-1]):
           return True
        else:
            return False

 二、盛最多水的容器

1、题目描述

2、题解

3、源码

# class Solution:
#     def maxArea(self, height: List[int]) -> int:
#         max = 0
#         for i in range(len(height)):
#             for j in range(i + 1,len(height)):
#                 min = height[i]
#                 if (min > height[j]):
#                     min = height[j]
#                 s = min * (j - i)
#                 if (s > max):
#                     max = s
#         return max

class Solution:
    def maxArea(self, height: List[int]) -> int:
        l, r = 0, len(height) - 1
        ans = 0
        while l < r:
            area = min(height[l], height[r]) * (r - l)
            ans = max(ans, area)
            if height[l] <= height[r]:
                l += 1
            else:
                r -= 1
        return ans

三、整数转罗马数字

1、题目描述

2、题解

3、源码 

class Solution:
    def intToRoman(self, num: int) -> str:
        Roman = [
            (1000, "M"),
            (900, "CM"),
            (500, "D"),
            (400, "CD"),
            (100, "C"),
            (90, "XC"),
            (50, "L"),
            (40, "XL"),
            (10, "X"),
            (9, "IX"),
            (5, "V"),
            (4, "IV"),
            (1, "I"),]
        ans = ''
        for n,roman_num in Roman:
            while num >= n:
                num -= n
                ans += roman_num
            if num == 0:
                break
        return ans

四、三数之和

1、题目描述

2、题解

3、源码

class Solution:
    def threeSum(self, nums: List[int]) -> List[List[int]]:
        n = len(nums)
        if len(nums) <= 2:
            return []
        nums.sort()#排序,双指针
        ans = []
        for i in range(len(nums)):
            if(nums[i] > 0):
               return ans
            if (i > 0 and nums[i] == nums[i - 1]):
                continue
            L = i + 1
            R = n - 1
            while (L < R):
                if (nums[i] + nums[L] + nums[R] == 0):
                    ans.append([nums[i],nums[L],nums[R]])
                    while(L < R and nums[L] == nums[L+1]):
                        L += 1
                    while(L < R and nums[R] == nums[R-1]):
                        R -=1
                    L +=1
                    R -=1
                elif (nums[i] + nums[L] +nums[R] > 0):
                    R -=1
                else:
                    L +=1
           
        return ans



五、最长公共前缀

1、题目描述

2、题解

3、源码

class Solution:
    def longestCommonPrefix(self, strs: List[str]) -> str:
        ans = ''
        for i in zip(*strs):
            if len(set(i)) == 1:
                ans += i[0]
            else:
                break
        return ans


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值