Compare commits

...

30 Commits

Author SHA1 Message Date
5428c96ba7 feat: 771_jewels_and_stones 2025-08-20 01:58:29 +08:00
eb5e426fdb feat: 3467_transform_array_by_parity 2025-08-20 01:58:29 +08:00
349b9de617 feat: 2181_merge_nodes_in_between_zeros 2025-08-20 01:58:29 +08:00
ea3f8dfd88 feat: 2044_count_number_of_maximum_bitwise_or_subsets 2025-08-20 01:58:29 +08:00
a296a13e73 feat: 29_divide_two_integer 2025-08-20 01:58:29 +08:00
cb2281d912 feat: 2011_final_value_of_variable_after_performing_operations 2025-08-20 01:58:29 +08:00
80bad5db79 feat: 1512_number_of_good_pairs 2025-08-20 01:58:29 +08:00
925fee99af feat: 1108_defanging_an_ip_address 2025-08-20 01:58:29 +08:00
e7b97921a8 feat: 2348_number_of_zero_filled_subarrays 2025-08-20 01:58:29 +08:00
8ae5954097 feat: 1863_sum_of_all_subset_xor_totals 2025-08-20 01:58:29 +08:00
bb0ae9bf73 feat: 2469_convert_the_temperature 2025-08-20 01:58:27 +08:00
ea7ae7b6ad feat: 1769_minimum_number_of_operations_to_move_all_balls_to_each_box 2025-08-20 01:58:25 +08:00
c98a2c5518 feat: 1929_concatenation_of_array 2025-08-20 01:58:23 +08:00
c66081ba92 feat: 2942_find_words_containing_character 2025-08-20 01:58:21 +08:00
8052507e9d refactor: rename main.rs as lib.rs 2025-08-20 01:58:21 +08:00
7a9666cb32 feat: 2769_find_the_maximum_achievable_number 2025-08-20 01:58:19 +08:00
c13ac3ca2a feat: 1920_build_array_from_permutation 2025-08-20 01:58:15 +08:00
476faf16f8 feat: 2894_divisible_and_non_divisible_sums_difference 2025-08-20 01:58:13 +08:00
d259727b80 feat: 2807_insert_greatest_common_divisors_in_linked_list 2025-08-20 01:58:09 +08:00
ed1fa0b0c0 feat: 3110_score_of_a_string 2025-08-20 01:58:06 +08:00
d0ade64745 feat: simplify min_path_sum implementation by removing unused code and optimizing grid updates 2025-08-20 01:58:06 +08:00
99ed22d02b feat: 64_minimum_path_sum 2025-08-20 01:58:01 +08:00
763b5d5945 feat: 194_transpose_file 2025-08-20 01:57:56 +08:00
c9528fc389 feat: 192_word_frequency 2025-08-20 01:57:54 +08:00
b1d019a05d feat: 342_power_of_four 2025-08-20 01:57:52 +08:00
df5a8b2939 feat: 679_24_game 2025-08-20 01:57:51 +08:00
e4356973e6 feat: refactor reverse_list function and add reverse_list_2 implementation 2025-08-20 01:57:51 +08:00
9767170fb6 feat: 206_reverse_linked_list 2025-08-20 01:57:48 +08:00
cf6277cbec feat: 202_happy_number 2025-08-20 01:57:45 +08:00
a916917fae feat: 195_thenth_line 2025-08-20 01:57:37 +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() {}
struct Solution;
pub struct Solution;
impl Solution {
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() {
println!("Hello, world!");
}
struct Solution;
pub struct Solution;
impl Solution {
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;
fn main() {
let x = "000".to_string();
let y = "0".to_string();
println!("{}", Solution::plus(x, y));
}
struct Solution;
pub struct Solution;
impl Solution {
pub fn multiply(x: String, y: String) -> String {

View File

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

View File

@ -1,11 +1,4 @@
fn main() {
println!(
"{}",
Solution::unique_paths_with_obstacles(vec![vec![0], vec![1]])
);
}
struct Solution;
pub struct Solution;
impl Solution {
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() {
println!("Hello, world!");
}
struct Solution;
pub struct Solution;
impl Solution {
pub fn new21_game(n: i32, k: i32, max_pts: i32) -> f64 {

View File

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