ugh, finally works
This commit is contained in:
commit
5a9634eb5b
6 changed files with 114 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
*/target/
|
7
happy_number/Cargo.lock
generated
Normal file
7
happy_number/Cargo.lock
generated
Normal file
|
@ -0,0 +1,7 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "happy_number"
|
||||
version = "0.1.0"
|
8
happy_number/Cargo.toml
Normal file
8
happy_number/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "happy_number"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
23
happy_number/src/main.rs
Normal file
23
happy_number/src/main.rs
Normal file
|
@ -0,0 +1,23 @@
|
|||
struct Solution {}
|
||||
|
||||
impl Solution {
|
||||
pub fn is_happy(n: i32) -> bool {
|
||||
let mut s = std::collections::HashSet::new();
|
||||
let mut n = n;
|
||||
while s.insert(n) {
|
||||
n = Self::sum_squares(n);
|
||||
}
|
||||
n == 1
|
||||
}
|
||||
|
||||
fn sum_squares(n: i32) -> i32 {
|
||||
n.to_string()
|
||||
.chars()
|
||||
.map(|c| c.to_digit(10).unwrap_or(0).pow(2))
|
||||
.sum::<u32>() as i32
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("{}", Solution::is_happy(19));
|
||||
}
|
8
spiral_matrix/Cargo.toml
Normal file
8
spiral_matrix/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "spiral_matrix"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
67
spiral_matrix/src/main.rs
Normal file
67
spiral_matrix/src/main.rs
Normal file
|
@ -0,0 +1,67 @@
|
|||
struct Solution();
|
||||
|
||||
impl Solution {
|
||||
pub fn spiral_order(matrix: &[&[i32]]) -> Vec<i32> {
|
||||
let mut fr = 0;
|
||||
let mut lr = matrix.len() - 1;
|
||||
let mut fc = 0;
|
||||
let mut lc = matrix.get(0).unwrap().len() - 1;
|
||||
let mut spiral = Vec::with_capacity((lr + 1) * (lc + 1));
|
||||
let cols = lc + 1;
|
||||
let size = matrix.len() * cols;
|
||||
loop {
|
||||
dbg!(fr, lr, fc, lc);
|
||||
// right
|
||||
for i in fc..=lc {
|
||||
spiral.push(matrix[fr][i]);
|
||||
}
|
||||
fr += 1;
|
||||
if spiral.len() == size {}
|
||||
|
||||
// down
|
||||
for row in fr..=lr {
|
||||
spiral.push(matrix[row][lc]);
|
||||
}
|
||||
lc = lc.saturating_sub(1);
|
||||
if spiral.len() == size {
|
||||
break;
|
||||
}
|
||||
|
||||
// left
|
||||
if fr <= lr {
|
||||
for e in (fc..=lc).rev() {
|
||||
spiral.push(matrix[lr][e]);
|
||||
}
|
||||
}
|
||||
lr = lr.saturating_sub(1);
|
||||
if spiral.len() == size {
|
||||
break;
|
||||
}
|
||||
|
||||
// up
|
||||
if fc <= lc {
|
||||
for i in (fr..=lr).rev() {
|
||||
spiral.push(matrix[i][fc]);
|
||||
}
|
||||
}
|
||||
fc += 1;
|
||||
if spiral.len() == size {
|
||||
break;
|
||||
}
|
||||
|
||||
dbg!(&spiral);
|
||||
}
|
||||
|
||||
spiral
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
dbg!(Solution::spiral_order(&[
|
||||
&[1, 2, 3],
|
||||
&[4, 5, 6],
|
||||
&[7, 8, 9]
|
||||
]));
|
||||
|
||||
dbg!(Solution::spiral_order(&[&[3], &[2]]));
|
||||
}
|
Loading…
Reference in a new issue