-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2ab1d3d
commit 0c6a64a
Showing
14 changed files
with
293 additions
and
154 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<i32>, m: i32, n: i32) -> Vec<Vec<i32>> { | ||
|
||
} | ||
} | ||
// @lc code=end | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Rc<RefCell<TreeNode>>>, | ||
pub right: Option<Rc<RefCell<TreeNode>>>, | ||
} | ||
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<Rc<RefCell<TreeNode>>>, | ||
// pub right: Option<Rc<RefCell<TreeNode>>>, | ||
// } | ||
// | ||
// 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<Rc<RefCell<TreeNode>>>) -> i32 { | ||
let mut res = 0; | ||
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, 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<Rc<RefCell<TreeNode>>>) -> 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<i32>, 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)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
name = "leetcodebyrust" | ||
version = "0.1.0" | ||
authors = ["zhangyuang <yuuang>"] | ||
edition = "2018" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<i32>, m: i32, n: i32) -> Vec<Vec<i32>> { | ||
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
name = "leetcodebyrust" | ||
version = "0.1.0" | ||
authors = ["zhangyuang <yuuang>"] | ||
edition = "2018" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] |
Oops, something went wrong.