【leecode刷题】初级算法-排序和搜索

tech2024-12-19  3

1、合并两个有序数组

class Solution(object): def merge(self, nums1, m, nums2, n): """ :type nums1: List[int] :type m: int :type nums2: List[int] :type n: int :rtype: None Do not return anything, modify nums1 in-place instead. """ # nums1 = nums1[:m] # nums2 = nums2[:n] # # 保存结果 # nums = [0]*(m+n) # i = 0 # j = 0 # k = 0 # while i<len(nums1) and j<len(nums2): # if nums1[i]<nums2[j]: # nums[k] = nums1[i] # i = i+1 # else: # nums[k] = nums2[j] # j = j+1 # k = k+1 # while i<len(nums1): # nums[k] = nums1[i] # i = i+1 # k = k+1 # while j<len(nums2): # nums[k] = nums2[j] # j = j+1 # k = k+1 # nums1 = [0]*(m+n) # for i in range(len(nums)): # nums1[i] = nums[i] # nums1 = sorted(nums1[:m]+nums2) nums1[:] = sorted(nums1[:m] + nums2)

2、第一个错误的版本

class Solution(object): def firstBadVersion(self, n): """ :type n: int :rtype: int """ left = 1 right = n while left <= right: midpoint = (left+right) // 2 if not isBadVersion(midpoint): left = midpoint + 1 else: right = midpoint - 1 return left
最新回复(0)