Compare commits

...

30 Commits

Author SHA1 Message Date
20730d34b7 feat: 771_jewels_and_stones 2025-08-20 01:56:24 +08:00
1aa56ff3c9 feat: 3467_transform_array_by_parity 2025-08-20 01:56:21 +08:00
1e2a86cf52 feat: 2181_merge_nodes_in_between_zeros 2025-08-20 01:56:12 +08:00
ef53a58935 feat: 2044_count_number_of_maximum_bitwise_or_subsets 2025-08-20 01:55:14 +08:00
d5635e7618 feat: 29_divide_two_integer 2025-08-20 01:55:07 +08:00
f06e82f965 feat: 2011_final_value_of_variable_after_performing_operations 2025-08-20 01:55:01 +08:00
66d8753334 feat: 1512_number_of_good_pairs 2025-08-20 01:54:55 +08:00
846eaa9521 feat: 1108_defanging_an_ip_address 2025-08-20 01:54:45 +08:00
d6c8702c7a feat: 2348_number_of_zero_filled_subarrays 2025-08-20 01:54:35 +08:00
eb4903c54f feat: 1863_sum_of_all_subset_xor_totals 2025-08-20 01:54:06 +08:00
d4b56a88fd feat: add initial implementation of Cargo.toml, Cargo.lock, and lib.rs with convert_temperature function 2025-08-18 20:00:50 +08:00
7848468524 feat: add initial implementation of Cargo.toml, Cargo.lock, and lib.rs with min_operations function 2025-08-18 20:00:37 +08:00
610d05ddb2 feat: add initial implementation of Cargo.toml, Cargo.lock, and lib.rs with get_concatenation function 2025-08-18 18:45:57 +08:00
21fffd8882 feat: add initial implementation of Cargo.toml, Cargo.lock, and lib.rs with find_words_containing function 2025-08-18 18:38:22 +08:00
50d62c34be refactor: rename main.rs as lib.rs 2025-08-18 18:30:10 +08:00
75a12ace37 feat: add initial implementation of Cargo.toml, Cargo.lock, and Solution struct with the_maximum_achievable_x function 2025-08-18 18:23:47 +08:00
ae4d5b5527 feat: add initial implementation of Cargo.toml, Cargo.lock, and main function with build_array method 2025-08-18 18:15:39 +08:00
a442d1cd09 feat: add initial implementation of Cargo.toml and Cargo.lock files along with main function and Solution struct 2025-08-18 18:10:42 +08:00
2a5ebdad4c feat: add initial implementation of insert_greatest_common_divisors function with ListNode struct 2025-08-18 17:51:02 +08:00
c41b628fab feat: add initial implementation of score_of_a_string with main function 2025-08-18 17:22:00 +08:00
2026979f5e feat: simplify min_path_sum implementation by removing unused code and optimizing grid updates 2025-08-18 16:06:42 +08:00
c6a939c5e0 feat: add initial implementation of minimum path sum solution 2025-08-18 15:55:06 +08:00
0aa11d31a2 feat: add script to transpose a file using awk 2025-08-18 12:18:53 +08:00
1d7f72e3e5 feat: add script for word frequency analysis from a text file 2025-08-18 11:25:26 +08:00
b76354feb3 feat: add initial implementation of power of four solution with main function 2025-08-18 11:12:23 +08:00
3030d16bad feat: add initial implementation of 24 game solution with main function 2025-08-18 10:55:06 +08:00
51bfae35cd feat: refactor reverse_list function and add reverse_list_2 implementation 2025-08-17 23:33:49 +08:00
11d6a65155 feat: add initial implementation of reverse linked list 2025-08-17 23:04:26 +08:00
0eb65c4ed3 feat: add initial implementation of happy number solution 2025-08-17 22:41:41 +08:00
f98094172e feat: add script to print the 10th line of a file 2025-08-17 22:27:01 +08:00
82 changed files with 904 additions and 41 deletions

7
1108_defanging_an_ip_address/Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "defanging_an_ip_address"
version = "0.1.0"

View File

@ -0,0 +1,6 @@
[package]
name = "defanging_an_ip_address"
version = "0.1.0"
edition = "2024"
[dependencies]

View File

@ -0,0 +1,7 @@
pub struct Solution;
impl Solution {
pub fn defang_i_paddr(address: String) -> String {
address.replace(".", "[.]")
}
}

View File

@ -1,6 +1,4 @@
fn main() {} pub struct Solution;
struct Solution;
impl Solution { impl Solution {
pub fn maximum69_number(num: i32) -> i32 { pub fn maximum69_number(num: i32) -> i32 {

7
1512_number_of_good_pairs/Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "number_of_good_pairs"
version = "0.1.0"

View File

@ -0,0 +1,6 @@
[package]
name = "number_of_good_pairs"
version = "0.1.0"
edition = "2024"
[dependencies]

View File

@ -0,0 +1,17 @@
pub struct Solution;
impl Solution {
pub fn num_identical_pairs(nums: Vec<i32>) -> i32 {
let mut result = 0;
for i in 0..nums.len() {
for j in (i + 1)..nums.len() {
if nums[i] == nums[j] {
result += 1;
}
}
}
result
}
}

View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "minimum_number_of_operations_to_move_all_balls_to_each_box"
version = "0.1.0"

View File

@ -0,0 +1,6 @@
[package]
name = "minimum_number_of_operations_to_move_all_balls_to_each_box"
version = "0.1.0"
edition = "2024"
[dependencies]

View File

@ -0,0 +1,33 @@
pub struct Solution;
impl Solution {
pub fn min_operations(boxes: String) -> Vec<i32> {
let boxes: Vec<i32> = boxes.chars().map(|c| c as i32 - 48).collect();
let mut balls_counted_from_left = vec![0; boxes.len()];
for i in 1..boxes.len() {
balls_counted_from_left[i] = balls_counted_from_left[i - 1] + boxes[i - 1];
}
let mut balls_counted_from_right = vec![0; boxes.len()];
for i in (0..(boxes.len() - 1)).rev() {
balls_counted_from_right[i] = balls_counted_from_right[i + 1] + boxes[i + 1];
}
let mut steps_moving_to_right = vec![0; boxes.len()];
for i in 1..boxes.len() {
steps_moving_to_right[i] = steps_moving_to_right[i - 1] + balls_counted_from_left[i];
}
let mut steps_moving_to_left = vec![0; boxes.len()];
for i in (0..(boxes.len() - 1)).rev() {
steps_moving_to_left[i] = steps_moving_to_left[i + 1] + balls_counted_from_right[i];
}
steps_moving_to_left
.iter()
.zip(steps_moving_to_right.iter())
.map(|(&l, &r)| l + r)
.collect()
}
}

View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "sum_of_all_subset_xor_totals"
version = "0.1.0"

View File

@ -0,0 +1,6 @@
[package]
name = "sum_of_all_subset_xor_totals"
version = "0.1.0"
edition = "2024"
[dependencies]

View File

@ -0,0 +1,8 @@
pub struct Solution;
impl Solution {
pub fn subset_xor_sum(nums: Vec<i32>) -> i32 {
let or_sum = nums.iter().fold(0, |acc, &num| acc | num);
or_sum << (nums.len() - 1)
}
}

View File

@ -1,8 +1,4 @@
fn main() { pub struct Solution;
println!("Hello, world!");
}
struct Solution;
impl Solution { impl Solution {
pub fn hamming_weight(mut n: i32) -> i32 { pub fn hamming_weight(mut n: i32) -> i32 {

View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "build_array_from_permutation"
version = "0.1.0"

View File

@ -0,0 +1,6 @@
[package]
name = "build_array_from_permutation"
version = "0.1.0"
edition = "2024"
[dependencies]

View File

@ -0,0 +1,7 @@
pub struct Solution;
impl Solution {
pub fn build_array(nums: Vec<i32>) -> Vec<i32> {
nums.iter().map(|&val| nums[val as usize]).collect()
}
}

7
1929_concatenation_of_array/Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "concatenation_of_array"
version = "0.1.0"

View File

@ -0,0 +1,6 @@
[package]
name = "concatenation_of_array"
version = "0.1.0"
edition = "2024"
[dependencies]

View File

@ -0,0 +1,7 @@
pub struct Solution;
impl Solution {
pub fn get_concatenation(nums: Vec<i32>) -> Vec<i32> {
vec![nums.clone(), nums].concat()
}
}

View File

@ -0,0 +1 @@
cat words.txt | tr -s '[:space:]' '\n' | sort | uniq -c | sort -nr | awk '{print $2, $1}'

23
194_transpose_file/script.sh Executable file
View File

@ -0,0 +1,23 @@
cat file.txt | awk '
{
max_cols = 0
for (i = 1; i <= NF; i++) {
data[i, NR] = $i
}
if (NF > max_cols) {
max_cols = NF
}
}
END {
for (i = 1; i <= max_cols; i++) {
for (j = 1; j <= NR; j++) {
printf "%s", data[i, j]
if (j < NR) {
printf " "
}
}
printf "\n"
}
}
'

View File

@ -0,0 +1 @@
sed -n '10p' file.txt

View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "final_value_of_variable_after_performing_operations"
version = "0.1.0"

View File

@ -0,0 +1,6 @@
[package]
name = "final_value_of_variable_after_performing_operations"
version = "0.1.0"
edition = "2024"
[dependencies]

View File

@ -0,0 +1,17 @@
pub struct Solution;
impl Solution {
pub fn final_value_after_operations(operations: Vec<String>) -> i32 {
let mut result = 0;
for op in operations {
if op.contains('+') {
result += 1;
} else {
result -= 1;
}
}
result
}
}

7
202_happy_number/Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "happy_number"
version = "0.1.0"

View File

@ -0,0 +1,6 @@
[package]
name = "happy_number"
version = "0.1.0"
edition = "2024"
[dependencies]

View File

@ -0,0 +1,25 @@
use std::collections::HashSet;
pub struct Solution;
impl Solution {
pub fn is_happy(mut n: i32) -> bool {
let mut numbers_shown = HashSet::<i32>::new();
numbers_shown.insert(n);
while n != 1 {
let mut new_n = 0;
for c in n.to_string().chars() {
new_n += (c as i32 - 48).pow(2);
}
n = new_n;
if numbers_shown.contains(&n) {
return false;
}
numbers_shown.insert(n);
}
true
}
}

View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "count_number_of_maximum_bitwise_or_subsets"
version = "0.1.0"

View File

@ -0,0 +1,6 @@
[package]
name = "count_number_of_maximum_bitwise_or_subsets"
version = "0.1.0"
edition = "2024"
[dependencies]

View File

@ -0,0 +1,37 @@
pub struct Solution;
impl Solution {
pub fn count_max_or_subsets(nums: Vec<i32>) -> i32 {
let max_or_sum = nums.iter().fold(0, |acc, &num| acc | num);
let mut result = 0;
let mut stack: Vec<DFSParams> = nums
.iter()
.enumerate()
.map(|(index, &num)| DFSParams { index, or_sum: num })
.collect();
while !stack.is_empty() {
let current = stack.pop().unwrap();
if current.or_sum == max_or_sum {
result += 1 << (nums.len() - current.index - 1);
continue;
}
for i in (current.index + 1)..nums.len() {
stack.push(DFSParams {
index: i,
or_sum: current.or_sum | nums[i],
});
}
}
result
}
}
pub struct DFSParams {
index: usize,
or_sum: i32,
}

7
206_reverse_linked_list/Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "reverse_linked_list"
version = "0.1.0"

View File

@ -0,0 +1,6 @@
[package]
name = "reverse_linked_list"
version = "0.1.0"
edition = "2024"
[dependencies]

View File

@ -0,0 +1,56 @@
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct ListNode {
pub val: i32,
pub next: Option<Box<ListNode>>,
}
impl ListNode {
#[inline]
fn new(val: i32) -> Self {
ListNode { next: None, val }
}
}
pub struct Solution;
impl Solution {
pub fn reverse_list(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let mut arr: Vec<i32> = vec![];
let mut current_node = head;
while let Some(node) = current_node.take() {
arr.push(node.val);
current_node = node.next;
}
if arr.is_empty() {
return None;
}
let mut head = Some(Box::new(ListNode::new(*arr.last().unwrap())));
let mut current_node = &mut head;
for &num in arr[..arr.len() - 1].iter().rev() {
current_node.as_mut().unwrap().next = Some(Box::new(ListNode::new(num)));
current_node = &mut current_node.as_mut().unwrap().next;
}
head
}
pub fn reverse_list_2(mut head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
if head.is_none() {
return None;
}
let mut p: Option<Box<ListNode>> = None;
while let Some(mut node) = head.take() {
let next = node.next;
node.next = p;
p = Some(node);
head = next;
}
p
}
}

View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "merge_nodes_in_between_zeros"
version = "0.1.0"

View File

@ -0,0 +1,6 @@
[package]
name = "merge_nodes_in_between_zeros"
version = "0.1.0"
edition = "2024"
[dependencies]

View File

@ -0,0 +1,39 @@
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct ListNode {
pub val: i32,
pub next: Option<Box<ListNode>>,
}
impl ListNode {
#[inline]
fn new(val: i32) -> Self {
ListNode { next: None, val }
}
}
pub struct Solution;
impl Solution {
pub fn merge_nodes(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let mut result_head = Some(Box::new(ListNode::new(0)));
let mut result_current_node = &mut result_head;
let mut current_node = head.unwrap().next;
while let Some(node) = current_node.take() {
if node.val != 0 {
result_current_node.as_mut().unwrap().val += node.val;
} else {
if node.next.as_ref().is_none() {
break;
}
result_current_node.as_mut().unwrap().next = Some(Box::new(ListNode::new(0)));
result_current_node = &mut result_current_node.as_mut().unwrap().next;
}
current_node = node.next;
}
result_head
}
}

View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "number_of_zero_filled_subarrays"
version = "0.1.0"

View File

@ -0,0 +1,6 @@
[package]
name = "number_of_zero_filled_subarrays"
version = "0.1.0"
edition = "2024"
[dependencies]

View File

@ -0,0 +1,20 @@
pub struct Solution;
impl Solution {
pub fn zero_filled_subarray(mut nums: Vec<i32>) -> i64 {
let mut result = 0i64;
let mut continuous_count = 0i64;
nums.push(1);
for num in nums {
if num == 0 {
continuous_count += 1;
} else {
result += (1 + continuous_count) * continuous_count / 2;
continuous_count = 0;
}
}
result
}
}

7
2469_convert_the_temperature/Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "convert_the_temperature"
version = "0.1.0"

View File

@ -0,0 +1,6 @@
[package]
name = "convert_the_temperature"
version = "0.1.0"
edition = "2024"
[dependencies]

View File

@ -0,0 +1,7 @@
pub struct Solution;
impl Solution {
pub fn convert_temperature(celsius: f64) -> Vec<f64> {
vec![celsius + 273.15, celsius * 1.8 + 32.0]
}
}

View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "find_the_maximum_achievable_number"
version = "0.1.0"

View File

@ -0,0 +1,6 @@
[package]
name = "find_the_maximum_achievable_number"
version = "0.1.0"
edition = "2024"
[dependencies]

View File

@ -0,0 +1,7 @@
pub struct Solution;
impl Solution {
pub fn the_maximum_achievable_x(num: i32, t: i32) -> i32 {
num + 2 * t
}
}

View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "insert_greatest_common_divisors_in_linked_list"
version = "0.1.0"

View File

@ -0,0 +1,6 @@
[package]
name = "insert_greatest_common_divisors_in_linked_list"
version = "0.1.0"
edition = "2024"
[dependencies]

View File

@ -0,0 +1,52 @@
pub struct Solution;
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct ListNode {
pub val: i32,
pub next: Option<Box<ListNode>>,
}
impl Solution {
pub fn insert_greatest_common_divisors(
mut head: Option<Box<ListNode>>,
) -> Option<Box<ListNode>> {
let mut current = &mut head;
while let Some(node) = current {
if let Some(next_node) = &node.next {
let a = node.val;
let b = next_node.val;
let gcd = Solution::gcd(a, b);
let next = node.next.take();
node.next = Some(Box::new(ListNode { val: gcd, next }));
current = &mut node.next.as_mut().unwrap().next;
} else {
break;
}
}
head
}
fn gcd(mut a: i32, mut b: i32) -> i32 {
if a > b {
let tmp = a;
a = b;
b = tmp;
}
loop {
let rem = b % a;
b = a;
a = rem;
if rem == 0 {
break;
}
}
b
}
}

View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "divisible_and_non_divisible_sums_difference"
version = "0.1.0"

View File

@ -0,0 +1,6 @@
[package]
name = "divisible_and_non_divisible_sums_difference"
version = "0.1.0"
edition = "2024"
[dependencies]

View File

@ -0,0 +1,10 @@
pub struct Solution;
impl Solution {
pub fn difference_of_sums(n: i32, m: i32) -> i32 {
let (dividable, undividable): (Vec<i32>, Vec<i32>) =
(1..(n + 1)).partition(|&val| val % m == 0);
undividable.iter().sum::<i32>() - dividable.iter().sum::<i32>()
}
}

View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "find_words_containing_character"
version = "0.1.0"

View File

@ -0,0 +1,6 @@
[package]
name = "find_words_containing_character"
version = "0.1.0"
edition = "2024"
[dependencies]

View File

@ -0,0 +1,12 @@
pub struct Solution;
impl Solution {
pub fn find_words_containing(words: Vec<String>, x: char) -> Vec<i32> {
words
.iter()
.enumerate()
.filter(|(_, word)| word.contains(x))
.map(|(index, _)| index as i32)
.collect()
}
}

7
29_divide_two_integer/Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "divide_two_integer"
version = "0.1.0"

View File

@ -0,0 +1,6 @@
[package]
name = "divide_two_integer"
version = "0.1.0"
edition = "2024"
[dependencies]

View File

@ -0,0 +1,58 @@
pub struct Solution;
impl Solution {
/// - Dividend = Quotient * Divisor + Rem
/// - Rem < Divisor
pub fn divide(mut dividend: i32, mut divisor: i32) -> i32 {
if divisor == i32::MIN {
return if dividend == i32::MIN { 1 } else { 0 };
}
let is_positive = dividend.is_positive() == divisor.is_positive();
let mut is_overflow = false;
if dividend == i32::MIN {
is_overflow = true;
dividend += 1;
}
dividend = dividend.abs();
divisor = divisor.abs();
if dividend < divisor {
return 0;
}
let mut quotient = 0;
let mut rem;
loop {
let mut exp = 1;
while divisor << exp <= dividend && divisor << exp > 0 {
exp += 1;
}
exp -= 1;
quotient += 1 << exp;
rem = dividend - (divisor << exp);
if rem < divisor {
break;
}
dividend = rem;
}
if is_overflow && rem + 1 >= divisor {
if is_positive {
if quotient == i32::MAX {
quotient
} else {
quotient + 1
}
} else {
-quotient - 1
}
} else {
if is_positive { quotient } else { -quotient }
}
}
}

7
3110_score_of_a_string/Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "score_of_a_string"
version = "0.1.0"

View File

@ -0,0 +1,6 @@
[package]
name = "score_of_a_string"
version = "0.1.0"
edition = "2024"
[dependencies]

View File

@ -0,0 +1,10 @@
pub struct Solution;
impl Solution {
pub fn score_of_string(s: String) -> i32 {
s.chars()
.zip(s.chars().skip(1))
.map(|(a, b)| (a as i32 - b as i32).abs())
.sum()
}
}

7
342_power_of_four/Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "power_of_four"
version = "0.1.0"

View File

@ -0,0 +1,6 @@
[package]
name = "power_of_four"
version = "0.1.0"
edition = "2024"
[dependencies]

View File

@ -0,0 +1,21 @@
pub struct Solution;
impl Solution {
pub fn is_power_of_four(mut n: i32) -> bool {
if n <= 0 {
return false;
}
while n > 0 {
if n == 1 {
return true
}
if n % 4 != 0 {
return false;
}
n /= 4;
}
false
}
}

View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "transform_array_by_parity"
version = "0.1.0"

View File

@ -0,0 +1,6 @@
[package]
name = "transform_array_by_parity"
version = "0.1.0"
edition = "2024"
[dependencies]

View File

@ -0,0 +1,10 @@
pub struct Solution;
impl Solution {
pub fn transform_array(nums: Vec<i32>) -> Vec<i32> {
let even_count = nums.iter().filter(|&num| num % 2 == 0).count();
let mut result = vec![0; even_count];
result.extend(vec![1; nums.len() - even_count].iter());
result
}
}

View File

@ -1,12 +1,6 @@
use std::cmp; use std::cmp;
fn main() { pub struct Solution;
let x = "000".to_string();
let y = "0".to_string();
println!("{}", Solution::plus(x, y));
}
struct Solution;
impl Solution { impl Solution {
pub fn multiply(x: String, y: String) -> String { pub fn multiply(x: String, y: String) -> String {

View File

@ -1,8 +1,4 @@
fn main() { pub struct Solution;
println!("Hello, world!");
}
struct Solution;
impl Solution { impl Solution {
pub fn unique_paths(m: i32, n: i32) -> i32 { pub fn unique_paths(m: i32, n: i32) -> i32 {

View File

@ -1,11 +1,4 @@
fn main() { pub struct Solution;
println!(
"{}",
Solution::unique_paths_with_obstacles(vec![vec![0], vec![1]])
);
}
struct Solution;
impl Solution { impl Solution {
pub fn unique_paths_with_obstacles(obstacle_grid: Vec<Vec<i32>>) -> i32 { pub fn unique_paths_with_obstacles(obstacle_grid: Vec<Vec<i32>>) -> i32 {

7
64_minimum_path_sum/Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "minimum_path_sum"
version = "0.1.0"

View File

@ -0,0 +1,6 @@
[package]
name = "minimum_path_sum"
version = "0.1.0"
edition = "2024"
[dependencies]

View File

@ -0,0 +1,24 @@
pub struct Solution;
impl Solution {
pub fn min_path_sum(mut grid: Vec<Vec<i32>>) -> i32 {
let rows = grid.len();
let cols = grid[0].len();
for i in 1..rows {
grid[i][0] += grid[i - 1][0];
}
for j in 1..cols {
grid[0][j] += grid[0][j - 1];
}
for i in 1..rows {
for j in 1..cols {
grid[i][j] = std::cmp::min(grid[i][j] + grid[i - 1][j], grid[i][j] + grid[i][j - 1])
}
}
grid[rows - 1][cols - 1]
}
}

7
679_24_game/Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "twenty_four_game"
version = "0.1.0"

6
679_24_game/Cargo.toml Normal file
View File

@ -0,0 +1,6 @@
[package]
name = "twenty_four_game"
version = "0.1.0"
edition = "2024"
[dependencies]

58
679_24_game/src/lib.rs Normal file
View File

@ -0,0 +1,58 @@
pub struct Solution;
impl Solution {
pub fn judge_point24(cards: Vec<i32>) -> bool {
let mut stack: Vec<Vec<f64>> = vec![cards.iter().map(|&v| v as f64).collect()];
while let Some(cards) = stack.pop() {
if cards.len() == 1 {
if (cards.first().unwrap() - 24f64).abs() < 1e-6 {
return true;
} else {
continue;
}
}
for i in 0..(cards.len() - 1) {
for j in (i + 1)..cards.len() {
let a = cards[i];
let b = cards[j];
let mut left_cards = cards.clone();
left_cards.remove(j);
left_cards.remove(i);
let mut new_cards = left_cards.clone();
new_cards.push(a + b);
stack.push(new_cards);
let mut new_cards = left_cards.clone();
new_cards.push(a * b);
stack.push(new_cards);
let mut new_cards = left_cards.clone();
new_cards.push(a - b);
stack.push(new_cards);
let mut new_cards = left_cards.clone();
new_cards.push(b - a);
stack.push(new_cards);
if b.abs() > 1e-6 {
let mut new_cards = left_cards.clone();
new_cards.push(a / b);
stack.push(new_cards);
}
if a.abs() > 1e-6 {
let mut new_cards = left_cards.clone();
new_cards.push(b / a);
stack.push(new_cards);
}
}
}
}
false
}
}

7
771_jewels_and_stones/Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "jewels_and_stones"
version = "0.1.0"

View File

@ -0,0 +1,6 @@
[package]
name = "jewels_and_stones"
version = "0.1.0"
edition = "2024"
[dependencies]

View File

@ -0,0 +1,18 @@
use std::collections::HashSet;
pub struct Solution;
impl Solution {
pub fn num_jewels_in_stones(jewels: String, stones: String) -> i32 {
let jewels: HashSet<char> = HashSet::from_iter(jewels.chars());
let mut result = 0;
for stone in stones.chars() {
if jewels.contains(&stone) {
result += 1;
}
}
result
}
}

View File

@ -1,8 +1,4 @@
fn main() { pub struct Solution;
println!("Hello, world!");
}
struct Solution;
impl Solution { impl Solution {
pub fn new21_game(n: i32, k: i32, max_pts: i32) -> f64 { pub fn new21_game(n: i32, k: i32, max_pts: i32) -> f64 {

View File

@ -1,11 +1,4 @@
fn main() { pub struct Solution;
println!(
"{}",
Solution::unique_paths_iii(vec![vec![1, 0, 0, 0], vec![0, 0, 0, 0], vec![0, 0, 2, -1]])
);
}
struct Solution;
impl Solution { impl Solution {
pub fn unique_paths_iii(grid: Vec<Vec<i32>>) -> i32 { pub fn unique_paths_iii(grid: Vec<Vec<i32>>) -> i32 {