Compare commits

...

10 Commits

30 changed files with 361 additions and 0 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(".", "[.]")
}
}

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 = "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

@ -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
}
}

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,
}

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
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 }
}
}
}

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
}
}

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
}
}