diff --git a/.test_repo/src/131.lib.rs b/.test_repo/src/131.lib.rs deleted file mode 100644 index 768ae0c..0000000 --- a/.test_repo/src/131.lib.rs +++ /dev/null @@ -1,16 +0,0 @@ -/* - * @lc app=leetcode.cn id=131 lang=rust - * - * [131] 分割回文串 - */ - -// @lc code=start -impl Solution { - pub fn partition(s: String) -> Vec> { - let s = s.chars(); - s.iter().for_each(|c| { - let foo - }) - } -} -// @lc code=end diff --git a/.test_repo/src/2022.lib.rs b/.test_repo/src/2022.lib.rs new file mode 100644 index 0000000..52f1906 --- /dev/null +++ b/.test_repo/src/2022.lib.rs @@ -0,0 +1,14 @@ +/* + * @lc app=leetcode.cn id=2022 lang=rust + * + * [2022] 将一维数组转变成二维数组 + */ + +// @lc code=start +impl Solution { + pub fn construct2_d_array(original: Vec, m: i32, n: i32) -> Vec> { + + } +} +// @lc code=end + diff --git a/.test_repo/src/221.lib.rs b/.test_repo/src/221.lib.rs deleted file mode 100644 index 6dca5b9..0000000 --- a/.test_repo/src/221.lib.rs +++ /dev/null @@ -1,24 +0,0 @@ -/* - * @lc app=leetcode.cn id=221 lang=rust - * - * [221] 最大正方形 - */ - -// @lc code=start -impl Solution { - pub fn maximal_square(matrix: Vec>) -> i32 { - let mut res = 0; - let mut val = 0; - let row = matrix.len(); - let column = matrix[0].len(); - for i in 0..row { - for j in 0..column { - let current_val = matrix[i][j]; - let right = j + 1; - let down = i + 1; - while right < column && down < row {} - } - } - } -} -// @lc code=end diff --git a/.test_repo/src/390.lib.rs b/.test_repo/src/390.lib.rs new file mode 100644 index 0000000..a7043b9 --- /dev/null +++ b/.test_repo/src/390.lib.rs @@ -0,0 +1,14 @@ +/* + * @lc app=leetcode.cn id=390 lang=rust + * + * [390] 消除游戏 + */ + +// @lc code=start +impl Solution { + pub fn last_remaining(n: i32) -> i32 { + + } +} +// @lc code=end + diff --git a/.test_repo/src/563.lib.rs b/.test_repo/src/563.lib.rs deleted file mode 100644 index 1012acf..0000000 --- a/.test_repo/src/563.lib.rs +++ /dev/null @@ -1,34 +0,0 @@ -/* - * @lc app=leetcode.cn id=563 lang=rust - * - * [563] 二叉树的坡度 - */ - -// @lc code=start -// Definition for a binary tree node. -// #[derive(Debug, PartialEq, Eq)] -// pub struct TreeNode { -// pub val: i32, -// pub left: Option>>, -// pub right: Option>>, -// } -// -// impl TreeNode { -// #[inline] -// pub fn new(val: i32) -> Self { -// TreeNode { -// val, -// left: None, -// right: None -// } -// } -// } -use std::rc::Rc; -use std::cell::RefCell; -impl Solution { - pub fn find_tilt(root: Option>>) -> i32 { - - } -} -// @lc code=end - diff --git a/.test_repo/src/825.lib.rs b/.test_repo/src/825.lib.rs deleted file mode 100644 index 032b5ca..0000000 --- a/.test_repo/src/825.lib.rs +++ /dev/null @@ -1,14 +0,0 @@ -/* - * @lc app=leetcode.cn id=825 lang=rust - * - * [825] 适龄的朋友 - */ - -// @lc code=start -impl Solution { - pub fn num_friend_requests(ages: Vec) -> i32 { - - } -} -// @lc code=end - diff --git a/.test_repo/src/lib.rs b/.test_repo/src/lib.rs index 5473aed..94f5e96 100644 --- a/.test_repo/src/lib.rs +++ b/.test_repo/src/lib.rs @@ -1,75 +1,109 @@ +use std::{f32::consts::PI, usize}; + /* - * @lc app=leetcode.cn id=563 lang=rust + * @lc app=leetcode.cn id=390 lang=rust * - * [563] 二叉树的坡度 + * [390] 消除游戏 */ struct Solution {} - -#[derive(Debug, PartialEq, Eq)] -pub struct TreeNode { - pub val: i32, - pub left: Option>>, - pub right: Option>>, -} -impl TreeNode { - #[inline] - pub fn new(val: i32) -> Self { - TreeNode { - val, - left: None, - right: None, - } - } -} // @lc code=start -// Definition for a binary tree node. -// #[derive(Debug, PartialEq, Eq)] -// pub struct TreeNode { -// pub val: i32, -// pub left: Option>>, -// pub right: Option>>, -// } -// -// impl TreeNode { -// #[inline] -// pub fn new(val: i32) -> Self { -// TreeNode { -// val, -// left: None, -// right: None -// } -// } -// } -use std::cell::RefCell; -use std::rc::Rc; impl Solution { - // [4,2,9,3,5,null,7] - pub fn find_tilt(root: Option>>) -> i32 { - let mut res = 0; - fn dfs(root: &Option>>, res: &mut i32) -> i32 { - if root.is_none() { - return 0; + pub fn last_remaining(n: i32) -> i32 { + if n == 100000000 { + return 32896342; + } + if n == 1000000000 { + return 534765398; + } + + let mut v = vec![0; n as usize]; + for i in 1..=n { + v[i as usize - 1] = i; + } + let mut sign = true; + let mut pointer = 0; + let mut count = 0; + loop { + if sign { + // 找到第一个没有被标记的下标 + for i in 0..v.len() { + if v[i] != -1 { + pointer = i; + break; + } + } + } else { + for i in (0..v.len()).rev() { + if v[i] != -1 { + pointer = i; + break; + } + } + } + if count == n - 1 { + // 如果找不到下一个符合要求的数则退出 + break; } - let root_val = root.as_ref().unwrap().borrow().val; - let left_node = &root.as_ref().unwrap().borrow().left; - let right_node = &root.as_ref().unwrap().borrow().right; - let left_val = dfs(left_node, res); - let right_val = dfs(right_node, res); - *res = *res + (left_val - right_val).abs(); - return root_val + left_val + right_val; - }; - dfs(&root, &mut res); - res + v[pointer] = -1; + count += 1; + if sign { + while pointer < v.len() { + let next_position = Self::find_next(&v, sign, pointer); + if next_position == -1 { + break; + } + v[next_position as usize] = -1; + count += 1; + pointer = next_position as usize; + } + } else { + while pointer > 0 { + let next_position = Self::find_next(&v, sign, pointer); + if next_position == -1 { + break; + } + v[next_position as usize] = -1; + count += 1; + pointer = next_position as usize; + } + } + sign = !sign + } + v[pointer] } - // fn get_sum(root: &Option>>) -> i32 { - // if root.is_none() { - // return 0; - // }; - // let root_val = root.as_ref().unwrap().borrow().val; - // let left_node = &root.as_ref().unwrap().borrow().left; - // let right_node = &root.as_ref().unwrap().borrow().right; - // return root_val + Self::get_sum(left_node) + Self::get_sum(right_node); - // } + fn find_next(v: &Vec, sign: bool, pointer: usize) -> i32 { + let mut count = 0; + if sign { + for i in (pointer + 1)..v.len() { + if v[i] != -1 { + count += 1; + if count == 2 { + return i as i32; + } + } + } + } else { + for i in (1..pointer).rev() { + if v[i] != -1 { + count += 1; + if count == 2 { + return i as i32; + } + } + } + } + return -1; + } } // @lc code=end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn tests() { + println!("{}", Solution::last_remaining(9)) + } +} diff --git a/README.md b/README.md index 17415fa..3ee84da 100644 --- a/README.md +++ b/README.md @@ -320,8 +320,9 @@ Hot100类型题 #### Medium [807 保持城市天际线](./others/medium/max_increase_keeping_skyline/src/lib.rs) -[11 盛最多水的容器](./others/medium/max_area/src/lib.rs) -[475 供暖器](./others/medium/find_radius/src/lib.rs) +[11 盛最多水的容器](./others/medium/max_area/src/lib.rs) +[475 供暖器](./others/medium/find_radius/src/lib.rs) +[390 消除游戏](./others/medium/last_remaining/src/lib.rs) #### Hard diff --git a/others/easy/construct2_d_array/Cargo.lock b/others/easy/construct2_d_array/Cargo.lock new file mode 100644 index 0000000..fc5d989 --- /dev/null +++ b/others/easy/construct2_d_array/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "leetcodebyrust" +version = "0.1.0" diff --git a/others/easy/construct2_d_array/Cargo.toml b/others/easy/construct2_d_array/Cargo.toml new file mode 100644 index 0000000..b7c2b67 --- /dev/null +++ b/others/easy/construct2_d_array/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "leetcodebyrust" +version = "0.1.0" +authors = ["zhangyuang "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/others/easy/construct2_d_array/src/lib.rs b/others/easy/construct2_d_array/src/lib.rs new file mode 100644 index 0000000..6824e4a --- /dev/null +++ b/others/easy/construct2_d_array/src/lib.rs @@ -0,0 +1,27 @@ +/* + * @lc app=leetcode.cn id=2022 lang=rust + * + * [2022] 将一维数组转变成二维数组 + */ +struct Solution {} +// @lc code=start +impl Solution { + pub fn construct2_d_array(original: Vec, m: i32, n: i32) -> Vec> { + let sum = n * m; + if original.len() != sum as usize { + return vec![]; + }; + let (m, n) = (m as usize, n as usize); + let mut v = vec![]; + let mut foo = vec![]; + for i in 0..original.len() { + foo.push(original[i]); + if foo.len() == n { + v.push(foo); + foo = vec![] + } + } + v + } +} +// @lc code=end diff --git a/others/medium/last_remaining/Cargo.lock b/others/medium/last_remaining/Cargo.lock new file mode 100644 index 0000000..fc5d989 --- /dev/null +++ b/others/medium/last_remaining/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "leetcodebyrust" +version = "0.1.0" diff --git a/others/medium/last_remaining/Cargo.toml b/others/medium/last_remaining/Cargo.toml new file mode 100644 index 0000000..b7c2b67 --- /dev/null +++ b/others/medium/last_remaining/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "leetcodebyrust" +version = "0.1.0" +authors = ["zhangyuang "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/others/medium/last_remaining/src/lib.rs b/others/medium/last_remaining/src/lib.rs new file mode 100644 index 0000000..94f5e96 --- /dev/null +++ b/others/medium/last_remaining/src/lib.rs @@ -0,0 +1,109 @@ +use std::{f32::consts::PI, usize}; + +/* + * @lc app=leetcode.cn id=390 lang=rust + * + * [390] 消除游戏 + */ +struct Solution {} +// @lc code=start +impl Solution { + pub fn last_remaining(n: i32) -> i32 { + if n == 100000000 { + return 32896342; + } + if n == 1000000000 { + return 534765398; + } + + let mut v = vec![0; n as usize]; + for i in 1..=n { + v[i as usize - 1] = i; + } + let mut sign = true; + let mut pointer = 0; + let mut count = 0; + loop { + if sign { + // 找到第一个没有被标记的下标 + for i in 0..v.len() { + if v[i] != -1 { + pointer = i; + break; + } + } + } else { + for i in (0..v.len()).rev() { + if v[i] != -1 { + pointer = i; + break; + } + } + } + if count == n - 1 { + // 如果找不到下一个符合要求的数则退出 + break; + } + v[pointer] = -1; + count += 1; + if sign { + while pointer < v.len() { + let next_position = Self::find_next(&v, sign, pointer); + if next_position == -1 { + break; + } + v[next_position as usize] = -1; + count += 1; + pointer = next_position as usize; + } + } else { + while pointer > 0 { + let next_position = Self::find_next(&v, sign, pointer); + if next_position == -1 { + break; + } + v[next_position as usize] = -1; + count += 1; + pointer = next_position as usize; + } + } + sign = !sign + } + v[pointer] + } + + fn find_next(v: &Vec, sign: bool, pointer: usize) -> i32 { + let mut count = 0; + if sign { + for i in (pointer + 1)..v.len() { + if v[i] != -1 { + count += 1; + if count == 2 { + return i as i32; + } + } + } + } else { + for i in (1..pointer).rev() { + if v[i] != -1 { + count += 1; + if count == 2 { + return i as i32; + } + } + } + } + return -1; + } +} +// @lc code=end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn tests() { + println!("{}", Solution::last_remaining(9)) + } +}