Leetcode Rust 27 移除元素

huangapple go评论51阅读模式
英文:

Leetcode Rust 27 Remove Element

问题

以下是翻译好的内容:

这是我的解决方案。也许看起来有点复杂和不必要。我通过了105/111个测试,所以我想找出我的代码中的错误在哪里。
你可以在这里访问Leetcode页面。移除元素

我的想法是创建一个名为temp的向量,其中存储与val相等的元素的索引,这些元素将被删除。

然后我使用函数式编程。简要思想是将nums的末尾设置为与temp.len()相等的交换区域的长度。然后,我将所有与value不相等的值交换到前面,索引存储在temp中(毫无疑问,我们需要忽略已在交换区域中的索引)。

以下是错误的测试用例:

# 测试用例:
[4,2,0,2,2,1,4,4,1,4,3,2]

# 答案:
[3,2,0,2,2,1,4,1]

# 期望结果:
[2,2,0,2,2,1,3,1]

也许你应该自己在Leetcode中测试这段代码。非常感谢!

英文:

Here's my solution. Maybe it looks a little complicated and unnecessary. I passed 105/111 tests, so I wanted to find where the bug is in my code.
Here you can direct to the Leetcode page. Remove Element

    pub fn remove_element(nums: &mut Vec<i32>, val: i32) -> i32 {
        let mut temp = Vec::new();
        let len = nums.len();
        for (i, v) in nums.iter().enumerate() {
            if *v == val {
                temp.push(i)
            }
        }
        let new_len = temp.len();
        temp.iter()
            .skip_while(|x| (len - new_len..len).contains(x))
            .zip((len - new_len..len).skip_while(|x| temp.contains(x)))
            .map(|(a, b)| {
                nums[*a] = nums[b];
            })
            .collect::<()>();
        (len - new_len) as i32
    }

My idea is to create a vec temp which stores the indexs of the elements that equal to the val, which is to be deleted.

Then I use functional programming. The brief idea is to set the end of the nums as swap area with length of the temp.len(). Then I swap all the values that don't equal to the value to the front with the indexs stored in temp(undoubtedly we need to omit the indexs that is already in the swap area)

Here is the error testcase:

# Testcase:
[4,2,0,2,2,1,4,4,1,4,3,2]

# Answer:
[3,2,0,2,2,1,4,1]

# Expected:
[2,2,0,2,2,1,3,1]

Maybe you should test this code in the leetcode yourself. Thanks a lot!

答案1

得分: 0

I found that I should use filter instead of skip_while, that's where the problem is.

    pub fn remove_element(nums: &mut Vec<i32>, val: i32) -> i32 {
        let mut temp = Vec::new();
        let len = nums.len();
        for (i, v) in nums.iter().enumerate() {
            if *v == val {
                temp.push(i)
            }
        }
        let new_len = temp.len();
        temp.sort_by(|a, b| b.cmp(a));
        temp.iter()
            .filter(|x| (len - new_len..len).contains(x))
            .zip((len - new_len..len).filter(|x| !temp.contains(x)))
            .for_each(|(a, b)| {
                println!("{a} : {b}");
                nums[*a] = nums[b]
            });
        (len - new_len) as i32
    }
英文:

I found that I should use filter instead of skip_while, that's where the problem is.

    pub fn remove_element(nums: &amp;mut Vec&lt;i32&gt;, val: i32) -&gt; i32 {
        let mut temp = Vec::new();
        let len = nums.len();
        for (i, v) in nums.iter().enumerate() {
            if *v == val {
                temp.push(i)
            }
        }
        let new_len = temp.len();
        temp.sort_by(|a, b| b.cmp(a));
        temp.iter()
            .skip_while(|x| (len - new_len..len).contains(x))
            .zip((len - new_len..len).filter(|x| !temp.contains(x)))
            .for_each(|(a, b)| {
                println!(&quot;{a} : {b}&quot;);
                nums[*a] = nums[b]
            });
        (len - new_len) as i32
    }

huangapple
  • 本文由 发表于 2023年4月1日 00:49:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75900915.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定