Compare commits
10 Commits
a61c47a3e1
...
20730d34b7
Author | SHA1 | Date | |
---|---|---|---|
20730d34b7 | |||
1aa56ff3c9 | |||
1e2a86cf52 | |||
ef53a58935 | |||
d5635e7618 | |||
f06e82f965 | |||
66d8753334 | |||
846eaa9521 | |||
d6c8702c7a | |||
eb4903c54f |
7
1108_defanging_an_ip_address/Cargo.lock
generated
Normal file
7
1108_defanging_an_ip_address/Cargo.lock
generated
Normal 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"
|
6
1108_defanging_an_ip_address/Cargo.toml
Normal file
6
1108_defanging_an_ip_address/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "defanging_an_ip_address"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
7
1108_defanging_an_ip_address/src/lib.rs
Normal file
7
1108_defanging_an_ip_address/src/lib.rs
Normal 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
7
1512_number_of_good_pairs/Cargo.lock
generated
Normal 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"
|
6
1512_number_of_good_pairs/Cargo.toml
Normal file
6
1512_number_of_good_pairs/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "number_of_good_pairs"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
17
1512_number_of_good_pairs/src/lib.rs
Normal file
17
1512_number_of_good_pairs/src/lib.rs
Normal 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
|
||||
}
|
||||
}
|
7
1863_sum_of_all_subset_xor_totals/Cargo.lock
generated
Normal file
7
1863_sum_of_all_subset_xor_totals/Cargo.lock
generated
Normal 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"
|
6
1863_sum_of_all_subset_xor_totals/Cargo.toml
Normal file
6
1863_sum_of_all_subset_xor_totals/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "sum_of_all_subset_xor_totals"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
8
1863_sum_of_all_subset_xor_totals/src/lib.rs
Normal file
8
1863_sum_of_all_subset_xor_totals/src/lib.rs
Normal 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)
|
||||
}
|
||||
}
|
7
2011_final_value_of_variable_after_performing_operations/Cargo.lock
generated
Normal file
7
2011_final_value_of_variable_after_performing_operations/Cargo.lock
generated
Normal 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"
|
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "final_value_of_variable_after_performing_operations"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
@ -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
2044_count_number_of_maximum_bitwise_or_subsets/Cargo.lock
generated
Normal file
7
2044_count_number_of_maximum_bitwise_or_subsets/Cargo.lock
generated
Normal 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"
|
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "count_number_of_maximum_bitwise_or_subsets"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
37
2044_count_number_of_maximum_bitwise_or_subsets/src/lib.rs
Normal file
37
2044_count_number_of_maximum_bitwise_or_subsets/src/lib.rs
Normal 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
2181_merge_nodes_in_between_zeros/Cargo.lock
generated
Normal file
7
2181_merge_nodes_in_between_zeros/Cargo.lock
generated
Normal 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"
|
6
2181_merge_nodes_in_between_zeros/Cargo.toml
Normal file
6
2181_merge_nodes_in_between_zeros/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "merge_nodes_in_between_zeros"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
39
2181_merge_nodes_in_between_zeros/src/lib.rs
Normal file
39
2181_merge_nodes_in_between_zeros/src/lib.rs
Normal 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
|
||||
}
|
||||
}
|
7
2348_number_of_zero_filled_subarrays/Cargo.lock
generated
Normal file
7
2348_number_of_zero_filled_subarrays/Cargo.lock
generated
Normal 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"
|
6
2348_number_of_zero_filled_subarrays/Cargo.toml
Normal file
6
2348_number_of_zero_filled_subarrays/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "number_of_zero_filled_subarrays"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
20
2348_number_of_zero_filled_subarrays/src/lib.rs
Normal file
20
2348_number_of_zero_filled_subarrays/src/lib.rs
Normal 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
7
29_divide_two_integer/Cargo.lock
generated
Normal 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"
|
6
29_divide_two_integer/Cargo.toml
Normal file
6
29_divide_two_integer/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "divide_two_integer"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
58
29_divide_two_integer/src/lib.rs
Normal file
58
29_divide_two_integer/src/lib.rs
Normal 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
3467_transform_array_by_parity/Cargo.lock
generated
Normal file
7
3467_transform_array_by_parity/Cargo.lock
generated
Normal 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"
|
6
3467_transform_array_by_parity/Cargo.toml
Normal file
6
3467_transform_array_by_parity/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "transform_array_by_parity"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
10
3467_transform_array_by_parity/src/lib.rs
Normal file
10
3467_transform_array_by_parity/src/lib.rs
Normal 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
7
771_jewels_and_stones/Cargo.lock
generated
Normal 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"
|
6
771_jewels_and_stones/Cargo.toml
Normal file
6
771_jewels_and_stones/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "jewels_and_stones"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
18
771_jewels_and_stones/src/lib.rs
Normal file
18
771_jewels_and_stones/src/lib.rs
Normal 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
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user