数组理论基础
文章链接:代码随想录
704. 二分查找
文章讲解:代码随想录
视频讲解:手把手带你撕出正确的二分法 | 二分查找法 | 二分搜索法 | LeetCode:704. 二分查找_哔哩哔哩_bilibili
题目:
Given an array of integers
nums
which is sorted in ascending order, and an integertarget
, write a function to searchtarget
innums
. Iftarget
exists, then return its index. Otherwise, return-1
.You must write an algorithm with
O(log n)
runtime complexity.Example 1:
Input: nums = [-1,0,3,5,9,12], target = 9 Output: 4 Explanation: 9 exists in nums and its index is 4Example 2:
Input: nums = [-1,0,3,5,9,12], target = 2 Output: -1 Explanation: 2 does not exist in nums so return -1
解题思路:
左闭右闭
1. 设置左右指针,left = 0, right = len(nums) - 1
2. 进入循环, 条件为 left <= right , 如果left > right 则跳出循环
when nums[mid] < target: 调整左指针left = mid +1(因为mid已经验证)
when nums[mid] > target: 调整右指针right = mid-1
when nums[mid] == target:return
4. 如果没有答案 return -1
class Solution(object):
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
if not nums:
return -1
left = 0
right = len(nums)-1
while left <= right:
mid = (left+right)//2
if nums[mid] < target:
left = mid + 1
elif nums[mid] > target:
right = mid - 1
else:
return mid
return -1
左闭右开
思路类似
class Solution(object):
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
if not nums:
return -1
left = 0
right = len(nums)
while left < right:
mid = (left + right)//2
if nums[mid] > target:
right = mid
elif nums[mid] < target:
left = mid +1
else:
return mid
return -1
复杂度:
时间复杂度:O(logn)
空间复杂度:O(1)
27. 移除元素
题目链接:Remove Element - LeetCode
文章讲解:代码随想录
Given an integer array
nums
and an integerval
, remove all occurrences ofval
innums
in-place. The relative order of the elements may be changed.Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array
nums
. More formally, if there arek
elements after removing the duplicates, then the firstk
elements ofnums
should hold the final result. It does not matter what you leave beyond the firstk
elements.Return
k
after placing the final result in the firstk
slots ofnums
.Do not allocate extra space for another array. You must do this by modifying the input array in-placewith O(1) extra memory.
Example 1:
Input: nums = [3,2,2,3], val = 3 Output: 2, nums = [2,2,_,_] Explanation: Your function should return k = 2, with the first two elements of nums being 2. It does not matter what you leave beyond the returned k (hence they are underscores).Example 2:
Input: nums = [0,1,2,2,3,0,4,2], val = 2 Output: 5, nums = [0,1,4,0,3,_,_,_] Explanation: Your function should return k = 5, with the first five elements of nums containing 0, 0, 1, 3, and 4. Note that the five elements can be returned in any order. It does not matter what you leave beyond the returned k (hence they are underscores).
解题思路:
暴力解法
1. 设置新list
2. 进入nums的循环:
如果nums[i]不等于target则放入新list
3. return len(新list)
class Solution(object):
def removeElement(self, nums, val):
"""
:type nums: List[int]
:type val: int
:rtype: int
"""
result = list()
for i in nums:
if i != val:
result.append(i)
count=len(result)
return count
复杂度:
时间复杂度:O(n)
空间复杂度:O(n)
双指针但其中一个用for loop代替(具体看第三种)
class Solution(object):
def removeElement(self, nums, val):
"""
:type nums: List[int]
:type val: int
:rtype: int
"""
first=0
for second in range(len(nums)):
if nums[second] != val:
nums[first]=nums[second]
first += 1
return first
复杂度:
时间复杂度:O(n)
空间复杂度:O(1)
快慢指针
1. 设置快慢指针, 快指针遍历,满指针记录数字
2. 当快指针不等于target,nums记录数字,同时快慢同时移动, 反之则只移动快指针。
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
fast = 0 # 快指针
slow = 0 # 慢指针
size = len(nums)
while fast < size: # 不加等于是因为,a = size 时,nums[a] 会越界
# slow 用来收集不等于 val 的值,如果 fast 对应值不等于 val,则把它与 slow 替换
if nums[fast] != val:
nums[slow] = nums[fast]
slow += 1
fast += 1
return slow
复杂度:
时间复杂度:O(n)
空间复杂度:O(1)
总结
1. 看题看题看题,没注意到题目要求必须空间复杂度为O(1)直接写了移动元素的暴力解法, which is WRONG!!!
第一天正式刷题,也是第一次写bolg,花了两个小时左右,希望越来越熟练。