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