【代码随想录算法训练营】第一天| 【数组】:704. 二分查找,27. 移除元素

数组理论基础

【重点1】:数组是存放在连续内存空间上的相同类型数据的集合。

数组

  • 数组下标从0开始
  • 数组的内存空间是连续的

【重点2】:删除数组元素
可以理解为,数组的元素是不能删的,只能覆盖。
删除数组元素

【重点三】: 二维数组

  • 什么是二维数组:
    二维数组
  • 二维数组在内存的空间地址是连续的么?
    C++中,二维数组的排列是连续的
    Java中,二维数组的排列是不连续的,排列方式如下图
    java二维数组排列方式

数组总结

总结文章入口

在这里插入图片描述

LeetCode 704. Binary Search

很经典的题,依然是一看就会,一写就废···
视频连接:B站:二分查找

Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target 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 4

Example 2:

Input: nums = [-1,0,3,5,9,12], target = 2
Output: -1
Explanation: 2 does not exist in nums so return -1

Constraints:

  • 1 <= nums.length <= 104
  • -104 < nums[i], target < 104
  • All the integers in nums are unique.
  • nums is sorted in ascending order.

方法:使用[左闭右闭] 规则
此时,left <= right,因为left = right是有意义的;
mid = nums[right] - 1 或者 mid = nums[left] + 1

class Solution:
    def search(self, nums: List[int], target: int) -> int:
        left = 0
        right = len(nums) - 1
        mid = 0
        
        while left <= right:
            mid = left + (right - left) // 2
            if nums[mid] == target:
                return mid
            elif nums[mid] < target:
                left = mid + 1
            elif nums[mid] > target:
                right = mid - 1

        # if the nums do not have the target value, return -1
        return -1

遇到的报错

  • 在计算mid时,没有使用//,使用了/
    • /// 的区别
      // 取的是结果的最小整数,而 / 取得是实际的除法结果

LeetCode 27. Remove Element

第一次做这道题~
视频连接:B站:移除元素

Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The relative order of the elements may be changed. Return k as the length of the output nums.

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.

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.

方法:双指针(快慢指针法)
双指针法(快慢指针法)在数组和链表的操作中是非常常见的,很多考察数组、链表、字符串等操作的面试题,都使用双指针法。

class Solution:
    def removeElement(self, nums: List[int], val: int) -> int:
        slow = 0
        fast = 0

        while fast <= len(nums)-1:
            if nums[fast] != val:
                # slow pointer point to the removal, it will be coverd by the value of fast pointer
                nums[slow] = nums [fast]
                slow += 1
            # fast pointer will move forward by 1 step anyway
            fast += 1

        # finally, reuturn the slow pointer which is eqaul to the size of the new nums
        return slow

遇到的报错
浅浅的理解后,半抄半写吧····没有什么报错
这道题还需继续努力····

Summary

  1. 数组的内存地址是连续的,下标从0开始
  2. 数组是不可以被删除的,只能被覆盖
  3. 二维数组的内存地址在C++中是连续的,在Java中是不连续的
  4. //的结果是小的整数,/的结果是准确值
  5. LeetCode 27题要重新做!!!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
第二十二天的算法训练营主要涵盖了Leetcode题目中的三道题目,分别是Leetcode 28 "Find the Index of the First Occurrence in a String",Leetcode 977 "有序数组的平方",和Leetcode 209 "长度最小的子数组"。 首先是Leetcode 28题,题目要求在给定的字符串中找到第一个出现的字符的索引。思路是使用双指针来遍历字符串,一个指向字符串的开头,另一个指向字符串的结尾。通过比较两个指针所指向的字符是否相等来判断是否找到了第一个出现的字符。具体实现的代码如下: ```python def findIndex(self, s: str) -> int: left = 0 right = len(s) - 1 while left <= right: if s[left == s[right]: return left left += 1 right -= 1 return -1 ``` 接下来是Leetcode 977题,题目要求对给定的有序数组中的元素进行平方,并按照非递减的顺序返回结果。这里由于数组已经是有序的,所以可以使用双指针的方法来解决问题。一个指针指向数组的开头,另一个指针指向数组的末尾。通过比较两个指针所指向的元素的绝对值的大小来确定哪个元素的平方应该放在结果数组的末尾。具体实现的代码如下: ```python def sortedSquares(self, nums: List[int]) -> List[int]: left = 0 right = len(nums) - 1 ans = [] while left <= right: if abs(nums[left]) >= abs(nums[right]): ans.append(nums[left ** 2) left += 1 else: ans.append(nums[right ** 2) right -= 1 return ans[::-1] ``` 最后是Leetcode 209题,题目要求在给定的数组中找到长度最小的子数组

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值