首页 > 其他分享 >day1-array-part01-7.3

day1-array-part01-7.3

时间:2024-07-09 15:28:53浏览次数:12  
标签:right nums how part01 pay attention 7.3 array left

task for today:

1.数组理论基础,

2. 704. 二分查找,

3. 27. 移除元素

-------------------------------

1. 数组理论基础

- 数组是存放在连续内存空间上的相同类型数据的集合。

- 数组内存不能删除只能覆盖

- 注意与容器的区别

2. 704 二分查找

二分法的两个条件:(1)有序数组;(2)无重复元素;

写二分法,区间的定义一般为两种,左闭右闭即[left, right],或者左闭右开即[left, right)。

- 左闭右闭:left = 0, right = len(nums) - 1

in [left, right], left == right means something, so the determine condition should be while left <= right

- 左闭右开:left = 0, right = len(nums)

in [left, right), left == right means nothing, so the determin condition should be while left < right

if the side is close, the update of that side should be +/- 1

- Aiming to learn golang, so, today's challenge teach how to initial a variable and how to compose a code block with condition and executing code.

how to define function: def; func; pub fn;

in GoLang: 

pay attention to how to initial a variable "left := 0"

pay attention to use len(nums)

in Rust: 

pay attention to when use a ";", after "use", "let", the if/while block does not require

pay attention to how to define a vraiable "let (mut left, mut right) = (0_i32, nums.len() as i32 - 1)"

pay attention to how to index an element in vector in Rust: nums[mid as usize]

pay attention to use cmp, ordering

pay attention to use nums.len() 

3. 27. 移除元素

关键点:要知道数组的元素在内存地址中是连续的,不能单独删除数组中的某个元素,只能覆盖

- 暴力解法:两轮搜索

go: "for i := 0; i < l; i++"

rust: "for j in (i+1)..l"

- 快慢指针

fast goes through the array

slow records those elements which are not equal to given value.

标签:right,nums,how,part01,pay,attention,7.3,array,left
From: https://blog.csdn.net/bbrruunnoo/article/details/140145277

相关文章

  • day2-array-part02-7.4
    taskfortoday1.977.有序数组的平方 2,209.长度最小的子数组 3.59.螺旋矩阵II---------------------------------------------1.977.有序数组的平方 -暴力法raiseeachelementinthearraytothepowerof2,andthensorttheminascendingorder.it......
  • day3-linked list-part01-7.5
    tasksfortoday1.链表理论基础2.203.移除链表元素3.707.设计链表4.206.反转链表------------------------------------------------1.链表理论基础单链表:(singly-linkedlist)链表是一种通过指针串联在一起的线性结构,每一个节点由两部分组成,一个是数据......
  • 07-7.3.2 平衡二叉树(AVL)
    ......
  • OC-NSArray的基本介绍
    NSArray是不可变的;存储不同类型的对象。这意味着一个NSArray可以同时包含NSString、NSNumber、NSDictionary等不同类型的对象。同时只能存储对象,不能直接存储基本数据类型(如int、float等)。如果需要存储基本数据类型,应该先将它们封装为相应的对象类型(如NSNumber或NSValue)。......
  • codeforces1849 D. Array Painting
    题目链接https://codeforces.com/problemset/problem/1849/D题意输入\(n(1\leqn\leq2e5)\)和长为\(n\)的数组\(a(0\leqa[i]\leq2)\)。最初,数组的每个元素都是蓝色的。有两种类型的操作:支付一枚硬币,选择一个蓝色元素,将其涂成红色。选择一个不等于\(0\)的红......
  • [LeetCode] 238. Product of Array Except Self
    坑真的很多,首先要处理全零reduce没有值typeerror的问题。classSolution:defproductExceptSelf(self,nums:List[int])->List[int]:total=reduce(mul,nums)ret=[]iftotal==0:try:total=reduce(mul,[......
  • return isPlainObject(res) || Array.isArray(res) ? observer(res, cb) : res; 这个
    这段代码主要是在实现一个深度观察者模式的部分逻辑,用于递归地处理对象和数组,以便在数据结构变化时触发回调。这里的关键是理解条件运算符和函数调用的执行顺序。让我们逐步分析:条件表达式的左侧:isPlainObject(res):这个函数检查res是否是一个纯对象(即普通的JavaScript对象......
  • HashTable,ArrayList,queue等常用方法
    HashTable,ArrayList,queue等常用方法HashMap是Java中非常常用的集合类,它存储键值对,并提供了一系列方便的方法来操作这些数据。以下是一些HashMap的常用方法:1.添加和获取元素:put(key,value):将指定的键值对添加到HashMap中。如果键已存在,则更新对应的值。get(ke......
  • Arrays,Object,String,StringBuffer和常用工具类汇总
    Arrays[数组操作方法:排序,查找,替换,转换,复制]排序将数组升序排序:Arrays.sort(数组);查找数组中想查找的数字的索引:Arrays.binarysearch(数组,想查找的数字(例如3));替换将数组中的元素全部用x替换:Arrays.fill(数组,x);Arrays.fill(数组,下标y,下标z,x);//y-z下标的元素替换为x......
  • 「杂题乱刷2」CF402D Upgrading Array
    题目链接CF402DUpgradingArray(luogu)CF402DUpgradingArray解题思路首先有一个很显然的结论,就是你一旦在第\(i\)个位置上做了一次操作后,那么你之后所有在第\(j(i\lej)\)个位置做的操作都无效了,因为此时前缀的公因数为\(1\)了。因此有个很显然的结论就是操作需要......