算法练习题

1、括号

class Solution:
    def isValid(self, s: str) -> bool:
        dic = {')': '(', ']': '[', '}': '{'}
        stack = []
        for i in s:
            if stack and i in dic:
                print(stack)
                if stack[-1] == dic[i]:
                    stack.pop()
                else:
                    return False
            else:
                stack.append(i)
        return not stack
class Solution:
    def isValid(self, s: str) -> bool:
        dic = {')': '(', ']': '[', '}': '{'}
        stack = []
        for i in s:
            if stack and i in dic:
                print(stack)
                if stack[-1] == dic[i]:
                    stack.pop()
                else:
                    return False
            else:
                stack.append(i)
        return not stack

2、罗马转数字:

class Solution:
   def romanToInt(self, s: str) -> int:
       Roman2Int = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000}
       Int = 0
       n = len(s)

       for index in range(n - 1):
           if Roman2Int[s[index]] < Roman2Int[s[index + 1]]:
               Int -= Roman2Int[s[index]]
           else:
               Int += Roman2Int[s[index]]

       return Int + Roman2Int[s[-1]]

3、两数之和

给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。

你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。

你可以按任意顺序返回答案。

def two_sum( nums, target):
   n = len(nums)
   for i in range(n):
       for j in range(i + 1, n):
           if nums[i] + nums[j] == target:
               return i, j

字典解法:

def two_sum(nums, target):
   dct = {}
   for i, n in enumerate(nums):
       if target - n in dct:
           return [dct[target - n], i]
       dct[n] = i

4、最长公共前缀

编写一个函数来查找字符串数组中的最长公共前缀。

如果不存在公共前缀,返回空字符串 “”。

示例 1:

输入:strs = [“flower”,“flow”,“flight”]
输出:“fl”
示例 2:

输入:strs = [“dog”,“racecar”,“car”]
输出:“”
解释:输入不存在公共前缀。

class Solution:
    def longestCommonPrefix(self, strs: list[str]) -> str:
        if len(strs) == 0:
            return ''
        s = strs[0]
        for i in range(1, len(strs)):
            print(strs[i],s,s[:-1])
            #比较两个字符串是否存在公共字符串s,不存在则将s去掉一个尾部
            while strs[i].find(s) != 0:
                print(1111,s)
                s = s[:-1]
        return s

class Solution:
   def longestCommonPrefix(self, strs: list[str]) -> str:
       if not strs: return ""
       str0 = min(strs)
       str1 = max(strs)
       print(str0,str1)
       for i in range(len(str0)):
           if str0[i] != str1[i]:
               print(i)
               return str0[:i]
       return str0

5、找出字符串中第一个匹配项的下标

给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 needle 字符串的第一个匹配项的下标(下标从 0 开始)。如果 needle 不是 haystack 的一部分,则返回 -1
示例 1:

输入:haystack = “sadbutsad”, needle = “sad”
输出:0
解释:“sad” 在下标 0 和 6 处匹配。
第一个匹配项的下标是 0 ,所以返回 0 。
示例 2:

输入:haystack = “leetcode”, needle = “leeto”
输出:-1
解释:“leeto” 没有在 “leetcode” 中出现,所以返回 -1

class Solution:
   def strStr(self, haystack: str, needle: str) -> int:
       if needle not in haystack:
           return -1
       if needle == '':
           return 0
       m, n = len(haystack), len(needle)
       #在haystack中截取找 needle
       for i in range(m):
           print(i,m,n)
           print(haystack[i:n + i])

           if haystack[i:n+i] == needle:
               return i
     ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值