leetcode - 刷题记录-探索中级算法-数组和字符串

  1. 三数之和
    1. 给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组。

      注意:答案中不可以包含重复的三元组。

    2. class Solution:
          def threeSum(self, nums: List[int]) -> List[List[int]]:
              nums.sort()
              res = {}
              r = 0
              for a in range(len(nums)-2):
                  b = a+1
                  c = len(nums)-1
                  if a>0 and nums[a] == nums[a-1]:
                      continue
                  if nums[a]>0:
                      break
                  while b<c:
                      if nums[a] + nums[b] + nums[c] == 0:
                          res[r] = [nums[a],nums[b],nums[c]]
                          r += 1
                          b += 1
                          while a<b<c and nums[b] == nums[b-1]:
                              b += 1
                          c -= 1
                          while a<b<c and nums[c] == nums[c+1]:
                              c -= 1
                      elif nums[a] + nums[b] + nums[c] > 0:
                          c -= 1
                          while a<b<c and nums[c] == nums[c+1]:
                              c -= 1
                      else:
                          b += 1
                          while a<b<c and nums[b] == nums[b-1]:
                              b += 1
                      if nums[c] < 0:
                          break
              x = [res[i] for i in res]
              return x

      通过。指针!!!

  2. 矩阵置零

    1. 给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0。请使用原地算法

    2. class Solution:
          def setZeroes(self, matrix: List[List[int]]) -> None:
              """
              Do not return anything, modify matrix in-place instead.
              """
              # mn额外空间的,应该是类似于flag_matrix的感觉
              # n+m的,应该是行列分开记录。
              
              row = []
              col = []
              r_m = len(matrix)
              c_m = len(matrix[0])
              for r in range(r_m):
                  for c in range(c_m):
                      if matrix[r][c] == 0:
                          row.append(r)
                          col.append(c)      
              for r in range(r_m):
                  for c in range(c_m):
                      if r in row or c in col:
                          matrix[r][c] = 0

      通过

  3. 字谜分组

    1. 给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串.

    2. class Solution:
          def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
              dic = {}
              for i in strs:
                  tmp = [ord(ii) for ii in i]
                  tmp.sort()
                  #tmp = sum(tmp)
                  tmp = ''.join([chr(i) for i in tmp])
                  if tmp in dic:
                      dic[tmp].append(i)
                  else:
                      dic[tmp] = [i]
              res = []
              for i in dic:
                  res.append(dic[i])
              return res            

      通过,做了转换。用ascii码排序。

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

    1. 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。

    2. class Solution:
          def lengthOfLongestSubstring(self, s: str) -> int:
              res = 0
              tmp = ''
              for i in s:
                  if i not in tmp:
                      tmp += i  
                  else:
                      res = max(res,len(tmp))
                      tmp = tmp.rsplit(i)[-1] + i
              res = max(res,len(tmp))    
              return res

      通过

  5. 最长回文子串

    1. 给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。

    2. class Solution:
          def longestPalindrome(self, s: str) -> str:
              if len(s) <= 1:
                  return s
              elif len(s) == 2:
                  if s[0] == s[-1]:
                      return s
                  else:
                      return s[0]
                  
              res = s[0]
              for i in range(1,len(s)):
                  tmp1 = s[:i]
                  tmp2 = s[i:]
                  
                  if tmp1[-1] == tmp2[0]:
                      tmp = ''
                      for j in range(min(len(tmp1),len(tmp2))):
                          if tmp1[-(j+1)] == tmp2[j]:
                              tmp = tmp1[-(j+1)] + tmp + tmp2[j]
                          else:
                              break
                      res = res if len(res) > len(tmp) else tmp
                      
                  tmp1 = s[:i]
                  tmp2 = s[i:]
                  
                  if len(tmp2)>1 and tmp1[-1] == tmp2[1]:
                      tmp = tmp2[0]
                      tmp2 = tmp2[1:]
                      for j in range(min(len(tmp1),len(tmp2))):
                          if tmp1[-(j+1)] == tmp2[j]:
                              tmp = tmp1[-(j+1)] + tmp + tmp2[j]
                          else:
                              break
                      res = res if len(res) > len(tmp) else tmp
              
              return res

      通过。还有一种动态规划的解法,不会。。。。

  6. 递增的三元子序列

    1. 给定一个未排序的数组,判断这个数组中是否存在长度为 3 的递增子序列。

      数学表达式如下:

      1. 如果存在这样的 i, j, k,  且满足 0 ≤ i < j < k ≤ n-1,使得 arr[i] < arr[j] < arr[k] ,返回 true ; 否则返回 false

    2. class Solution:
          def increasingTriplet(self, nums: List[int]) -> bool:
              # 要求算法的时间复杂度为 O(n),空间复杂度为 O(1) 。
              minx = float('inf')
              maxx = float('inf')
              for i in nums:
                  if i<minx:
                      minx = i
                  elif minx<i<maxx:
                      maxx = i
                  elif i>maxx:
                      return True
              return False

      通过。遍历数组,找到当前的最小值和最大值。

 

发布了45 篇原创文章 · 获赞 1 · 访问量 8556

猜你喜欢

转载自blog.csdn.net/qq_32110859/article/details/105062685