day2-array-part02-7.4

task for today

1. 977.有序数组的平方 

2, 209.长度最小的子数组 

3. 59.螺旋矩阵II

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

1. 977.有序数组的平方 

- 暴力法

raise each element in the array to the power of 2, and then sort them in ascending order.

it's worth noting that in python the nums.sort() function is allowed to be used.

As for Rust, because the nums given is not mutable, a new mutable vector should be defined "let mut ans = vec![0;l];" to store the outcome of rasing each element in nums to the power of 2. Besides, the ans.sort() is allowed to use in Rust.

Meanwhile, in GoLang, the nums' elements can be changed, just like in python, what needs to be paid attention to is, the corresponding function for sorting in GoLang is sort.Ints(nums)

- 双指针

Based on the ascending order of nums, so the largest number should show up in either left side or right side. Then, two pointer can be set respectively start from two sides of nums, then compare whose value to the power of 2 is larger.

in python to set a new array, ans[float('inf')] * len(nums) can be used, the corresponding expression in GoLang and Rust is:

in Go: ans:=make([]int, len(nums))

in Rust: let mut ans = vec![0; nums.len()];

another thing worth noting is "nums[i]**2" which is usually used in python is not valid in Go and Rust

In Rust, there is a small issue when holding a negative number with usize type, i32 csn hold but this will need "as usize" when indexing an element in a vector in Rust.

And in rust, "and" is &&

2, 209.长度最小的子数组 

the essence for this practice is slide window, the right pointer go through the array to accumulate the sum, when the sum is greater or equal to the target, the left window will start to move forward. Two while loops are gonna be used to realize this function.

in python, a "min(a,b)" is used to find the less length, pay attention to the corresponding usage in Go and Rust.

In Go, there is no built-in min function, while in Rust, the min(a,b) is also valid

in Rust, "let mut result = i32::MAX" is used to facilitate the similar function with result = float('inf')

in Rust, normally let mut left = 0, left will be usize type, which can be directlty used to index within a vector, but other function, it should be converted to i32 with "as i32" 

3. 59.螺旋矩阵II

注意循环不变量,左闭右开这里选择

class Solution:
    def generateMatrix(self, n: int) -> List[List[int]]:
        if n == 1:
            return [[1]]
        # this is safer
        result = [[0] * n for _ in range(n)]
        startx, starty = 0, 0
        loop, mid = n // 2, n // 2
        count = 1
        for offset in range(1, loop + 1):
            for i in range(starty, n - offset):
                result[startx][i] = count
                count += 1
            for i in range(startx, n - offset):
                result[i][n - offset] = count
                count += 1
            for i in range(n - offset, starty, -1):
                result[n - offset][i] = count
                count += 1
            for i in range(n - offset, startx, -1):
                result[i][starty] = count
                count += 1
            
            startx += 1
            starty += 1
        
        # only odd number has a middle one
        if n % 2 != 0:
            result[mid][mid] = n**2

        return result

in Go, pay attention to "

res := make([][]int, n)

" this is used to initial the result matrix

while in Rust:"

let mut res = vec![vec![0; n as usize]; n as usize];
let (mut startX, mut startY, mut offset): (usize, usize, usize) = (0, 0, 1);

"

  • 28
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值