MCPcopy Create free account
hub / github.com/Ainevsia/Leetcode-Rust / remove_element

Method remove_element

27. Remove Element/src/main.rs:8–25  ·  view source on GitHub ↗
(nums: &mut Vec<i32>, val: i32)

Source from the content-addressed store, hash-verified

6
7impl Solution {
8 pub fn remove_element(nums: &mut Vec<i32>, val: i32) -> i32 {
9 if nums.len() == 0 { return 0 }
10 let (mut i, mut j) = (0, nums.len() - 1);
11 while i < j {
12 // println!("i = {:#?}, j = {:#?}", i, j);
13 // println!("nums = {:#?}", nums);
14 if nums[i] == val {
15 let tmp = nums[i];
16 nums[i] = nums[j];
17 nums[j] = tmp;
18 j -= 1;
19 } else {
20 i += 1;
21 }
22 }
23 (if nums[i] == val { i } else { i + 1 })
24 as i32
25 }
26}
27
28#[cfg(test)]

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected