Skip to content

Commit 2a1afab

Browse files
committed
Reorganized solutions by topic; sqrt, set-bits, reverse-bits, add-binary
1 parent e81e8f8 commit 2a1afab

File tree

13 files changed

+265
-8
lines changed

13 files changed

+265
-8
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "leetcode_rust"
3-
version = "0.1.0"
3+
version = "0.2.4"
44
edition = "2021"
55
authors = ["Ryo Light <68205373+CoffeelessProgrammer@users.noreply.github.com>"]
66

Readme.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,26 @@
1-
# Leetcode Solutions in Rust
1+
# LeetCode Solutions in Rust
2+
3+
- Leetcode Profile - [OmnicWolf | Leetcode](https://leetcode.com/OmnicWolf/)
4+
- Solutions Visualized – [@CoffeelessProgrammer | Instagram](https://www.instagram.com/coffeelessprogrammer/)
5+
6+
## Environment
7+
- <span>Rust v1.72.1</span>
8+
9+
## Problems Completed
10+
11+
### By Topic
12+
13+
- **Math**
14+
- <span>69. Sqrt(x)</span>
15+
- **Bit Manipulation**
16+
- <span>190. Reverse Bits</span>
17+
- <span>191. Number of 1 Bits</span>
18+
- **Strings**
19+
- <span>67. Add Binary</span>
20+
21+
## Resources
22+
23+
- Solutions in Java – [CoffeelessProgrammer/leetcode-java](https://github.com/CoffeelessProgrammer/leetcode-java)
24+
- Solutions in C# – [OmnicWolf/leetcode-csharp](https://github.com/OmnicWolf/leetcode-csharp)
25+
- Solutions in TypeScript – [Ryo112358/leetcode-typescript](https://github.com/Ryo112358/leetcode-typescript)
26+
- Interview Prep Resources Curated by Koi Creek – [koicreek/interview-prep](https://github.com/koicreek/interview-prep)

src/array/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub fn run() {
2+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Problem: 191. Number of 1 Bits (Easy)
3+
* URL: https://leetcode.com/problems/number-of-1-bits/
4+
*
5+
* Runtime: 0 ms (100%)
6+
* Memory Usage: 1.93 MB (91.44%)
7+
*/
8+
9+
pub fn solve(mut n: u32) -> i32 {
10+
let mut set_bits = 0;
11+
12+
while n > 0 {
13+
n &= n-1;
14+
set_bits += 1;
15+
}
16+
17+
return set_bits;
18+
}
19+
20+
21+
#[cfg(test)]
22+
mod hamming_weight_tests {
23+
use super::*;
24+
25+
#[test]
26+
fn happy_path() {
27+
assert_eq!(solve(0b000110010), 3);
28+
}
29+
30+
#[test]
31+
fn none_set() {
32+
assert_eq!(solve(0b000000000), 0);
33+
}
34+
35+
#[test]
36+
fn all_set() {
37+
assert_eq!(solve(0b11111111_11111111_11111111_11111111), 32);
38+
}
39+
}

src/bit_manipulation/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pub mod count_set_bits;
2+
pub mod reverse_bits;
3+
4+
pub fn run() {
5+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Problem: 190. Reverse Bits (Easy)
3+
* URL: https://leetcode.com/problems/reverse-bits/
4+
*
5+
* Runtime: 2 ms (55.42%)
6+
* Memory Usage: 2.02 MB (54.22%)
7+
*/
8+
9+
pub fn solve(mut x: u32) -> u32 {
10+
let mut result: u32 = 0;
11+
let mask = 1;
12+
13+
for i in 0..32 {
14+
// println!("\n({i}) x={:#32b}\nresult={:#32b}", x, result);
15+
result <<= 1;
16+
17+
if (x & mask == 1) { result += 1 }
18+
x >>= 1;
19+
}
20+
21+
return result;
22+
}
23+
24+
#[cfg(test)]
25+
mod reverse_bits_tests {
26+
use super::*;
27+
28+
#[test]
29+
fn leading_zeroes() {
30+
assert_eq!(solve(0b00000010_10010100_00011110_10011100), 0b00111001_01111000_00101001_01000000);
31+
}
32+
33+
#[test]
34+
fn leftmost_bit_set() {
35+
assert_eq!(solve(0b11111111_11111111_11111111_11111101), 0b10111111_11111111_11111111_11111111);
36+
}
37+
}

src/main.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
// #![allow(dead_code)]
1+
#![allow(unused)]
22

3-
pub mod easy;
3+
pub mod math;
4+
pub mod bit_manipulation;
5+
pub mod array;
6+
pub mod string;
47

58
fn main() {
6-
easy::run();
9+
// array::run();
10+
// string::run();
11+
// bit_manipulation::run();
712
}

src/math/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pub mod sqrt;
2+
3+
pub fn run() {
4+
}

src/math/sqrt.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Problem: 69. Sqrt(x) (Easy)
3+
* URL: https://leetcode.com/problems/sqrtx/
4+
*
5+
* Runtime: 0 ms (100%)
6+
* Memory Usage: 1.92 MB (88.50%)
7+
*/
8+
9+
pub fn solve(x: i32) -> i32 {
10+
let mut lb=0;
11+
let mut rb= if x < 46_340 { x } else { 46_340 }; // sqrt(2_147_483_647) = 46_340.9
12+
let mut mid=0;
13+
14+
while lb < rb {
15+
mid = (lb+rb)/2;
16+
// println!("range={}...{}, mid={}", lb, rb, mid);
17+
18+
if (mid*mid < x) { lb = mid+1; }
19+
else { rb = mid; }
20+
}
21+
22+
return if lb*lb > x { lb-1 } else { lb };
23+
}
24+
25+
#[cfg(test)]
26+
mod sqrt_tests {
27+
use super::*;
28+
29+
#[test]
30+
fn happy_path() {
31+
assert_eq!(solve(16), 4);
32+
}
33+
34+
#[test]
35+
fn round_down() {
36+
assert_eq!(solve(27), 5);
37+
}
38+
39+
#[test]
40+
fn i32_upper_limit() {
41+
assert_eq!(solve(i32::MAX), 46_340);
42+
}
43+
}

0 commit comments

Comments
 (0)