Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 75c124f

Browse files
author
guangsheng.li01
committed
.
1 parent e749c2f commit 75c124f

File tree

4 files changed

+155
-0
lines changed

4 files changed

+155
-0
lines changed

‎src/a0726_number_of_atoms.rs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* [0726] number-of-atoms
3+
*/
4+
5+
struct Solution;
6+
7+
use std::collections::HashMap;
8+
impl Solution {
9+
pub fn count_of_atoms(formula: String) -> String {
10+
let bytes = formula.as_bytes();
11+
let mut stack: Vec<HashMap<String, i32>> = Vec::new();
12+
let mut i = 0;
13+
let n = formula.len();
14+
stack.push(HashMap::new());
15+
while i < n {
16+
let c = bytes[i] as char;
17+
if c == '(' {
18+
stack.push(HashMap::new());
19+
i += 1;
20+
} else if c == ')' {
21+
let top = stack.pop().unwrap();
22+
i += 1;
23+
let start = i;
24+
while i < n && bytes[i].is_ascii_digit() {
25+
i += 1;
26+
}
27+
let number = Self::toi32(&bytes[start..i]);
28+
let m: &mut HashMap<String, i32> = stack.last_mut().unwrap();
29+
for (name, count) in top {
30+
*m.entry(name).or_insert(0) += count * number;
31+
}
32+
} else {
33+
// parse atom name
34+
let start = i + 1;
35+
i += 1;
36+
while i < n && bytes[i].is_ascii_lowercase() {
37+
i += 1;
38+
}
39+
let name = Self::to_string(&bytes[start - 1..i]);
40+
41+
// parse atom count
42+
let start = i;
43+
while i < n && bytes[i].is_ascii_digit() {
44+
i += 1;
45+
}
46+
let count = Self::toi32(&bytes[start..i]);
47+
let m: &mut HashMap<String, i32> = stack.last_mut().unwrap();
48+
*m.entry(name).or_insert(0) += count;
49+
}
50+
}
51+
52+
let map = stack.last().unwrap();
53+
let mut v: Vec<_> = map.into_iter().collect();
54+
v.sort_by(|x, y| x.0.cmp(&y.0));
55+
56+
let mut res = "".to_string();
57+
for (name, count) in v {
58+
res.push_str(name);
59+
if *count > 1 {
60+
res.push_str(&count.to_string());
61+
}
62+
}
63+
res.to_owned()
64+
}
65+
66+
fn toi32(v: &[u8]) -> i32 {
67+
if v.len() == 0 {
68+
return 1;
69+
}
70+
std::str::FromStr::from_str(std::str::from_utf8(v).unwrap()).unwrap()
71+
}
72+
73+
fn to_string(v: &[u8]) -> String {
74+
std::str::from_utf8(v).unwrap().to_string()
75+
}
76+
}
77+
78+
#[cfg(test)]
79+
mod tests {
80+
use super::*;
81+
82+
#[test]
83+
fn test_case0() {
84+
assert_eq!(Solution::count_of_atoms("H2O".to_owned()), "H2O");
85+
assert_eq!(Solution::count_of_atoms("Mg(OH)2".to_owned()), "H2MgO2");
86+
assert_eq!(
87+
Solution::count_of_atoms("K4(ON(SO3)2)2".to_owned()),
88+
"K4N2O14S4"
89+
);
90+
}
91+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* [0783] minimum-distance-between-bst-nodes
3+
*/
4+
5+
use super::utils::tree::*;
6+
struct Solution;
7+
8+
use std::cell::RefCell;
9+
use std::i32::{MAX, MIN};
10+
use std::rc::Rc;
11+
impl Solution {
12+
pub fn min_diff_in_bst(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
13+
let mut diff = MAX;
14+
let mut pval = MIN;
15+
Self::dfs(root, &mut pval, &mut diff);
16+
diff
17+
}
18+
19+
fn dfs(root: Option<Rc<RefCell<TreeNode>>>, pval: &mut i32, diff: &mut i32) {
20+
if let Some(root) = root {
21+
let root = root.borrow();
22+
Self::dfs(root.left.clone(), pval, diff);
23+
if *pval > MIN {
24+
*diff = std::cmp::min(*diff, root.val - *pval);
25+
}
26+
*pval = root.val;
27+
Self::dfs(root.right.clone(), pval, diff);
28+
}
29+
}
30+
}
31+
32+
#[cfg(test)]
33+
mod tests {
34+
use super::*;
35+
36+
#[test]
37+
fn test_case0() {
38+
assert_eq!(
39+
Solution::min_diff_in_bst(tree![4, 2, 6, 1, 3, null, null]),
40+
1
41+
);
42+
}
43+
}

‎src/casting_between_types.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
fn u8_slice_to_i32(v: &[u8]) -> i32 {
2+
std::str::FromStr::from_str(std::str::from_utf8(v).unwrap()).unwrap()
3+
}
4+
5+
fn u8_slice_to_string() {}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::*;
10+
11+
#[test]
12+
fn test_u8_slice_to_i32() {
13+
let s = String::from("123456");
14+
let bytes = s.as_bytes();
15+
let u8_slice = &bytes[1..5];
16+
assert_eq!(u8_slice_to_i32(u8_slice), 2345i32);
17+
}
18+
}

‎src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ mod a0643_maximum_average_subarray_i;
3434
mod a0646_maximum_length_of_pair_chain;
3535
mod a0654_maximum_binary_tree;
3636
mod a0701_insert_into_a_binary_search_tree;
37+
mod a0726_number_of_atoms;
3738
mod a0761_special_binary_string;
39+
mod a0783_minimum_distance_between_bst_nodes;
3840
mod a0867_transpose_matrix;
3941
mod a0883_projection_area_of_3d_shapes;
4042
mod a0894_all_possible_full_binary_trees;
@@ -54,3 +56,4 @@ mod a1287_element_appearing_more_than_25_in_sorted_array;
5456
mod a1302_deepest_leaves_sum;
5557
mod a1315_sum_of_nodes_with_even_valued_grandparent;
5658
mod a1317_convert_integer_to_the_sum_of_two_no_zero_integers;
59+
mod casting_between_types;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /