Skip to content

Commit

Permalink
'1'
Browse files Browse the repository at this point in the history
  • Loading branch information
Ainevsia committed Jul 16, 2023
1 parent 320dfb0 commit 3a68157
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 3 deletions.
27 changes: 25 additions & 2 deletions notes/src/day2/lc209.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
所以不断地右侧生长去找到一个可行解,然后左侧缩小去尝试更小的解

```rust
struct Solution {}
# struct Solution {}

impl Solution {
pub fn min_sub_array_len(target: i32, nums: Vec<i32>) -> i32 {
Expand Down Expand Up @@ -60,4 +60,27 @@ impl Solution {

所以 只用一个for循环,那么这个循环的索引,一定是表示 滑动窗口的终止位置。

原来是滑动窗口,只用一个变量来表示结束的位置
原来是滑动窗口,只用一个变量来表示结束的位置

```rust
# struct Solution {}

impl Solution {
pub fn min_sub_array_len(target: i32, nums: Vec<i32>) -> i32 {
let n = nums.len();
let mut res = i32::MAX;
let mut s = 0;
let mut a = 0;
for b in 0..n {
s += nums[b];
while s >= target {
res = res.min((b - a + 1) as i32);
s -= nums[a];
a += 1;
}
}
if res == i32::MAX {0} else {res as i32}
}
}

```
56 changes: 55 additions & 1 deletion notes/src/day2/lc59.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,58 @@


## 解题思路
## 学习感想

好像就是en做
写出来了 但是很长,就是按照题目的意思进行模拟(迭代),每次迭代填入最外层的一圈

```rust
# struct Solution {}

impl Solution {
pub fn generate_matrix(n: i32) -> Vec<Vec<i32>> {
let n = n as usize;
let mut v = vec![vec![0; n]; n];
// idx,idx 左上角的坐标, n 这一行的所有元素个数-1 右上角坐标idx,idx+n
pub fn f(v: &mut Vec<Vec<i32>>, idx: usize, n: usize, start: i32) -> i32 {
if n == 0 { v[idx][idx] = start; return start + 1 }
let mut cur = start;
for j in 0..n {
v[idx][idx+j] = cur ; cur += 1;
}
for i in 0..n {
v[idx+i][idx+n] = cur ; cur += 1;
}
for j in 0..n {
v[idx+n][idx+n-j] = cur ; cur += 1;
}
for i in 0..n {
v[idx+n-i][idx] = cur ; cur += 1;
}
cur
}
let mut start = 1;
let mut x = n as isize - 1;
let mut i = 0;
while x >= 0 {
start = f(&mut v, i, x as usize, start);
i += 1;
x -= 2;
}
v
}
}

```


## 学习感想

本题并不涉及到什么算法,就是模拟过程,但却十分考察对代码的掌控能力。

坚持循环不变量原则

确实,定义一定要非常明确,明确了定义之后就牢牢地实现这个定义

可以发现这里的边界条件非常多,在一个循环中,如此多的边界条件,如果不按照固定规则来遍历,那就是一进循环深似海,从此offer是路人。

然后好像就是我这种模拟的做法

0 comments on commit 3a68157

Please sign in to comment.