Initial commit
This commit is contained in:
commit
4b0a6a01b0
16
.gitignore
vendored
Normal file
16
.gitignore
vendored
Normal file
|
@ -0,0 +1,16 @@
|
|||
# Generated by Cargo
|
||||
# will have compiled files and executables
|
||||
debug/
|
||||
target/
|
||||
|
||||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
||||
Cargo.lock
|
||||
|
||||
# These are backup files generated by rustfmt
|
||||
**/*.rs.bk
|
||||
|
||||
# MSVC Windows builds of rustc generate these, which store debugging information
|
||||
*.pdb
|
||||
|
||||
main
|
8
_missing/Cargo.toml
Normal file
8
_missing/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "leetcode"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
37
_missing/src/main.rs
Normal file
37
_missing/src/main.rs
Normal file
|
@ -0,0 +1,37 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
let tests = [
|
||||
(vec![100,4,200,1,3,2], 4),
|
||||
(vec![0,3,7,2,5,8,4,6,0,1], 9)
|
||||
];
|
||||
|
||||
for test in tests {
|
||||
println!("{:?} is {} should be {}", test.0, Solution::longest_consecutive(test.0.clone()), test.1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
struct Solution;
|
||||
|
||||
impl Solution {
|
||||
pub fn longest_consecutive(nums: Vec<i32>) -> i32 {
|
||||
let mut s = nums.clone();
|
||||
s.sort();
|
||||
s.dedup();
|
||||
let mut longest = (nums.len() > 0) as i32;
|
||||
let mut longest_old = 0;
|
||||
|
||||
for i in s.windows(2) {
|
||||
if &i[1] != &(&i[0] + 1) {
|
||||
if longest > longest_old {
|
||||
longest_old = longest;
|
||||
}
|
||||
longest = 1;
|
||||
continue;
|
||||
}
|
||||
longest += 1;
|
||||
}
|
||||
|
||||
longest.max(longest_old)
|
||||
}
|
||||
}
|
8
a-number-after-a-double-reversal/Cargo.toml
Normal file
8
a-number-after-a-double-reversal/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "a-number-after-a-double-reversal"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
11
a-number-after-a-double-reversal/src/main.rs
Normal file
11
a-number-after-a-double-reversal/src/main.rs
Normal file
|
@ -0,0 +1,11 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
|
||||
struct Solution;
|
||||
|
||||
impl Solution {
|
||||
pub fn is_same_after_reversals(num: i32) -> bool {
|
||||
num == 0 || num % 10 != 0
|
||||
}
|
||||
}
|
8
add-two-numbers/Cargo.toml
Normal file
8
add-two-numbers/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "add-two-numbers"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
46
add-two-numbers/src/main.rs
Normal file
46
add-two-numbers/src/main.rs
Normal file
|
@ -0,0 +1,46 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
|
||||
#[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
|
||||
}
|
||||
}
|
||||
}
|
||||
// Comprehensive 3ms Recursive
|
||||
|
||||
struct Solution {}
|
||||
impl Default for ListNode {
|
||||
fn default() -> Self {
|
||||
ListNode::new(0)
|
||||
}
|
||||
}
|
||||
|
||||
impl Solution {
|
||||
pub fn add_two_numbers(l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
|
||||
Solution::add_two_numbers_with_carry(l1, l2, 0)
|
||||
}
|
||||
pub fn add_two_numbers_with_carry(l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>, carry: i32) -> Option<Box<ListNode>> {
|
||||
if l1.is_none() && l2.is_none() && carry == 0 {return None;}
|
||||
let a = l1.unwrap_or_default();
|
||||
let b = l2.unwrap_or_default();
|
||||
let sum = a.val + b.val + carry;
|
||||
|
||||
let result = ListNode {
|
||||
val: sum % 10,
|
||||
next: Solution::add_two_numbers_with_carry(a.next, b.next, sum / 10)
|
||||
};
|
||||
Some(Box::new(result))
|
||||
}
|
||||
}
|
||||
|
8
buddy-strings/Cargo.toml
Normal file
8
buddy-strings/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "buddy-strings"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
31
buddy-strings/src/main.rs
Normal file
31
buddy-strings/src/main.rs
Normal file
|
@ -0,0 +1,31 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
let tests = [
|
||||
("ab", "ba", true),
|
||||
("ab", "ab", false),
|
||||
("aa", "aa", true),
|
||||
("abcaa", "abcbb", false)
|
||||
];
|
||||
|
||||
for test in tests {
|
||||
println!("{:?} {:?} is {} should be {}", test.0, test.1,
|
||||
Solution::buddy_strings(test.0.clone().to_string(), test.1.clone().to_string()),
|
||||
test.2);
|
||||
}
|
||||
}
|
||||
struct Solution;
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
impl Solution {
|
||||
pub fn buddy_strings(s: String, goal: String) -> bool {
|
||||
let mut occurences_s = HashMap::new();
|
||||
let mut occurences_goal = HashMap::new();
|
||||
let similarities = s.chars().zip(goal.chars()).filter(|(a,b)| {
|
||||
*occurences_s.entry(a.clone()).or_insert(1) += 1;
|
||||
*occurences_goal.entry(b.clone()).or_insert(1) += 1;
|
||||
a != b})
|
||||
.count();
|
||||
s.len() == goal.len() && occurences_s == occurences_goal && (similarities == 2 || (similarities == 0 && occurences_s.values().any(|&x| x > 2)))
|
||||
}
|
||||
}
|
8
candy/Cargo.toml
Normal file
8
candy/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "candy"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
18
candy/src/main.rs
Normal file
18
candy/src/main.rs
Normal file
|
@ -0,0 +1,18 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
|
||||
|
||||
struct Solution {}
|
||||
impl Solution {
|
||||
pub fn candy(ratings: Vec<i32>) -> i32 {
|
||||
let result = Vec::with_capacity(ratings.len());
|
||||
|
||||
let mut candy = 1;
|
||||
|
||||
while let Some(s)= ratings.iter().enumerate()
|
||||
.fold(i32::MAX, |acc, (i,v)| if v < acc && {v})
|
||||
|
||||
result.iter().sum::<i32>()
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "check-if-a-string-contains-all-binary-codes-of-size-k"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
|
@ -0,0 +1,32 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
let tests = [
|
||||
("00110110", 2, true),
|
||||
("0110", 1, true),
|
||||
("0110", 2, false),
|
||||
("00000000001011100", 3, true),
|
||||
("000011010111011001001111111001000100100100010100101100001101101101110001100100101111100111001001111001001010111010010101101001001110011100110101001001001000000110101001010011101100110110100010000", 7, false)
|
||||
];
|
||||
for (s, k, o) in tests {
|
||||
let result = Solution::has_all_codes(s.to_string(),k);
|
||||
println!("{s:?} with {k} is {result} should be {o}");
|
||||
}
|
||||
}
|
||||
|
||||
struct Solution;
|
||||
|
||||
impl Solution {
|
||||
pub fn has_all_codes(s: String, k: i32) -> bool {
|
||||
// (00..=2_i32.pow(k as u32) -1).all(|x| s.contains(&format!("{x:00$b}", k as usize)))j
|
||||
let mut m: u128 = 0;
|
||||
let mut m = (0..)
|
||||
for w in s.chars().collect::<Vec<_>>().windows(k as usize) {
|
||||
m |= 1 << i32::from_str_radix(&w.iter().collect::<String>(), 2).unwrap();
|
||||
// println!("{m:b}");
|
||||
}
|
||||
// println!("{m:b}");
|
||||
let h = 2_u128.pow(2_u32.pow(k as u32)) -1;
|
||||
// println!("{h} {h:b}");
|
||||
m == h
|
||||
}
|
||||
}
|
8
check-if-all-as-appears-before-all-bs/Cargo.toml
Normal file
8
check-if-all-as-appears-before-all-bs/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "check-if-all-as-appears-before-all-bs"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
11
check-if-all-as-appears-before-all-bs/src/main.rs
Normal file
11
check-if-all-as-appears-before-all-bs/src/main.rs
Normal file
|
@ -0,0 +1,11 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
|
||||
struct Solution;
|
||||
|
||||
impl Solution {
|
||||
pub fn check_string(s: String) -> bool {
|
||||
!s.chars().skip_while(|&x| x == 'a').any(|x| x == 'a')
|
||||
}
|
||||
}
|
8
check-if-it-is-a-straight-line/Cargo.toml
Normal file
8
check-if-it-is-a-straight-line/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "check-if-it-is-a-straight-line"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
37
check-if-it-is-a-straight-line/src/main.rs
Normal file
37
check-if-it-is-a-straight-line/src/main.rs
Normal file
|
@ -0,0 +1,37 @@
|
|||
fn main() {
|
||||
let tests = [
|
||||
(vec![[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]], true),
|
||||
(vec![[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]], false),
|
||||
(vec![[0,0],[0,1],[0,-1]], true)
|
||||
];
|
||||
|
||||
for test in tests {
|
||||
println!("{:?} is {} should be {}",
|
||||
test.0,
|
||||
check_straight_line(test.0.iter().map(|c| c.to_vec()).collect()),
|
||||
test.1);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn check_straight_line(coordinates: Vec<Vec<i32>>) -> bool {
|
||||
let base = get_lin_fn(coordinates[0].clone(), coordinates[1].clone());
|
||||
println!("{:?} base", base);
|
||||
for c in 1..coordinates.len() {
|
||||
let lin_fn = get_lin_fn(coordinates[0].clone(), coordinates[c].clone());
|
||||
println!("{} {:?}", c, lin_fn);
|
||||
if lin_fn != base {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
pub fn get_lin_fn(a: Vec<i32>, b: Vec<i32>) -> (f32, f32) {
|
||||
let mx = b[0] - a[0];
|
||||
let my = b[1] - a[1];
|
||||
let m = my as f32/mx as f32;
|
||||
let b = a[1] as f32 - m*a[0] as f32;
|
||||
|
||||
return (m,b);
|
||||
}
|
||||
|
8
check-if-two-string-arrays-are-equivalent/Cargo.toml
Normal file
8
check-if-two-string-arrays-are-equivalent/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "check-if-two-string-arrays-are-equivalent"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
13
check-if-two-string-arrays-are-equivalent/src/main.rs
Normal file
13
check-if-two-string-arrays-are-equivalent/src/main.rs
Normal file
|
@ -0,0 +1,13 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
|
||||
struct Solution;
|
||||
|
||||
impl Solution {
|
||||
pub fn array_strings_are_equal(word1: Vec<String>, word2: Vec<String>) -> bool {
|
||||
//word1.iter_mut().reduce(|a, b| {a.push_str(b); a}).unwrap() == word2.iter_mut().reduce(|a, b| {a.push_str(b); a}).unwrap()
|
||||
//word1.concat() == word2.concat()
|
||||
word1.iter().flat_map(|x| x.chars()).eq(word2.iter().flat_map(|x| x.chars()))
|
||||
}
|
||||
}
|
8
combination-sum-iv/Cargo.toml
Normal file
8
combination-sum-iv/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "combination-sum-iv"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
37
combination-sum-iv/src/main.rs
Normal file
37
combination-sum-iv/src/main.rs
Normal file
|
@ -0,0 +1,37 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
let tests = [
|
||||
(vec![1,2,3], 4, 7),
|
||||
// (vec![9], 3, 0),
|
||||
(vec![2,1,3], 35, 1_132_436_852),
|
||||
// (vec![10,20,30,40,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190,200,210,220,230,240,250,260,270,280,290,300,310,320,330,340,350,360,370,380,390,400,410,420,430,440,450,460,470,480,490,500,510,520,530,540,550,560,570,580,590,600,610,620,630,640,650,660,670,680,690,700,710,720,730,740,750,760,770,780,790,800,810,820,830,840,850,860,870,880,890,900,910,920,930,940,950,960,970,980,990,111], 999, 2)
|
||||
];
|
||||
|
||||
for test in tests {
|
||||
println!("{:?} {} is {} should be {}",
|
||||
test.0, test.1,
|
||||
Solution::combination_sum4(test.0.clone(), test.1),
|
||||
test.2
|
||||
);
|
||||
}
|
||||
}
|
||||
struct Solution {}
|
||||
|
||||
|
||||
impl Solution {
|
||||
pub fn combination_sum4(nums: Vec<i32>, target: i32) -> i32 {
|
||||
let mut cache = vec![0; target as usize];
|
||||
Solution::combination_sum4_from(&nums, 0, target, &mut cache)
|
||||
}
|
||||
|
||||
pub fn combination_sum4_from(nums: &Vec<i32>, from: i32, target: i32, cache: &mut Vec<i32>) -> i32 {
|
||||
if from > target {return 0};
|
||||
if from == target {return 1};
|
||||
if cache[from as usize] > 0 {return cache[from as usize];}
|
||||
|
||||
let result = nums.iter().rev().map(|x| Solution::combination_sum4_from(&nums, from + x, target, cache)).sum();
|
||||
cache[from as usize] = result;
|
||||
println!("{:?}", cache);
|
||||
result
|
||||
}
|
||||
}
|
8
concatenation-of-array/Cargo.toml
Normal file
8
concatenation-of-array/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "concatenation-of-array"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
13
concatenation-of-array/src/main.rs
Normal file
13
concatenation-of-array/src/main.rs
Normal file
|
@ -0,0 +1,13 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
|
||||
struct Solution;
|
||||
|
||||
impl Solution {
|
||||
pub fn get_concatenation(nums: Vec<i32>) -> Vec<i32> {
|
||||
let mut result = nums.clone();
|
||||
result.append(&mut nums.clone());
|
||||
result
|
||||
}
|
||||
}
|
8
contains-duplicate/Cargo.toml
Normal file
8
contains-duplicate/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "contains-duplicate"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
10
contains-duplicate/src/main.rs
Normal file
10
contains-duplicate/src/main.rs
Normal file
|
@ -0,0 +1,10 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
|
||||
struct Solution {}
|
||||
impl Solution {
|
||||
pub fn contains_duplicate(nums: Vec<i32>) -> bool {
|
||||
nums.iter().
|
||||
}
|
||||
}
|
8
counting-bits/Cargo.toml
Normal file
8
counting-bits/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "counting-bits"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
23
counting-bits/src/main.rs
Normal file
23
counting-bits/src/main.rs
Normal file
|
@ -0,0 +1,23 @@
|
|||
fn main() {
|
||||
let tests = [
|
||||
(2, vec![0,1,1]),
|
||||
(5, vec![0,1,1,2,1,2])
|
||||
];
|
||||
|
||||
for test in tests {
|
||||
println!("{} is {:?} should be {:?}", test.0, count_bits(test.0), test.1);
|
||||
}
|
||||
}
|
||||
|
||||
fn count_bits(n: i32) -> Vec<i32> {
|
||||
let mut solution: Vec<i32> = Vec::new();
|
||||
for i in 0..=n {
|
||||
let mut ones = 0;
|
||||
for c in 0..32 {
|
||||
ones += (1<<c & i != 0) as i32;
|
||||
}
|
||||
solution.push(ones);
|
||||
}
|
||||
|
||||
return solution;
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "decrypt-string-from-alphabet-to-integer-mapping"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
22
decrypt-string-from-alphabet-to-integer-mapping/src/main.rs
Normal file
22
decrypt-string-from-alphabet-to-integer-mapping/src/main.rs
Normal file
|
@ -0,0 +1,22 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
|
||||
struct Solution;
|
||||
|
||||
impl Solution {
|
||||
pub fn freq_alphabets(mut s: String) -> String {
|
||||
for i in 10..=26 {
|
||||
let mut integer_str = i.to_string();
|
||||
integer_str.push_str("#");
|
||||
s = s.replace(&integer_str, &char::from_u32(96 + i).unwrap().to_string() );
|
||||
}
|
||||
for i in 1..=9 {
|
||||
let integer_str = i.to_string();
|
||||
s = s.replace(&integer_str, &char::from_u32(96 + i).unwrap().to_string() );
|
||||
}
|
||||
let i = s.chars().rev();
|
||||
i.map(|x| if x == '#' {i.take()})
|
||||
s
|
||||
}
|
||||
}
|
8
design-hashmap/Cargo.toml
Normal file
8
design-hashmap/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "design-hashmap"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
43
design-hashmap/src/main.rs
Normal file
43
design-hashmap/src/main.rs
Normal file
|
@ -0,0 +1,43 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
let mut a = MyHashMap::new();
|
||||
a.put(1,1);
|
||||
a.put(2,2);
|
||||
a.get(1);
|
||||
a.get(3);
|
||||
a.put(2,1);
|
||||
a.get(2);
|
||||
a.remove(2);
|
||||
a.get(2);
|
||||
}
|
||||
|
||||
struct MyHashMap {
|
||||
map: Vec<(i32,i32)>
|
||||
}
|
||||
|
||||
impl MyHashMap {
|
||||
|
||||
fn new() -> Self {
|
||||
MyHashMap {
|
||||
map: Vec::new()
|
||||
}
|
||||
}
|
||||
|
||||
fn put(&mut self, key: i32, value: i32) {
|
||||
match self.map.iter_mut().find(|x| x.0 == key) {
|
||||
Some(e) => e.1 = value,
|
||||
None => self.map.push((key, value))
|
||||
};
|
||||
}
|
||||
|
||||
fn get(&self, key: i32) -> i32 {
|
||||
self.map.iter().find(|&x| x.0 == key).map(|x| x.1).unwrap_or(-1)
|
||||
}
|
||||
|
||||
fn remove(&mut self, key: i32) {
|
||||
if let Some(e) = self.map.iter().position(|x| x.0 == key) {
|
||||
self.map.swap_remove(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
8
diagonal-traverse/Cargo.toml
Normal file
8
diagonal-traverse/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "diagonal-traverse"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
16
diagonal-traverse/src/main.rs
Normal file
16
diagonal-traverse/src/main.rs
Normal file
|
@ -0,0 +1,16 @@
|
|||
struct Solution;
|
||||
impl Solution {
|
||||
pub fn find_diagonal_order(mat: Vec<Vec<i32>>) -> Vec<i32> {
|
||||
let mut solution: Vec<i32> = Vec::new();
|
||||
let x: usize = 0;
|
||||
let y: usize = 0;
|
||||
|
||||
solution.push(mat[x][y]);
|
||||
|
||||
solution
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
8
extra-characters-in-a-string/Cargo.toml
Normal file
8
extra-characters-in-a-string/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "extra-characters-in-a-string"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
23
extra-characters-in-a-string/src/main.rs
Normal file
23
extra-characters-in-a-string/src/main.rs
Normal file
|
@ -0,0 +1,23 @@
|
|||
fn main() {
|
||||
let tests = vec![
|
||||
("leetscode", vec!["leet","code","leetcode"], 1),
|
||||
("sayhelloworld", vec!["hello","world"], 3),
|
||||
];
|
||||
|
||||
for test in tests {
|
||||
let result = min_extra_char(
|
||||
test.0.to_string(),
|
||||
test.1.iter().map(|s| s.to_string()).collect()
|
||||
);
|
||||
println!("{} has {} should be {}", test.0, result, test.2);
|
||||
}
|
||||
}
|
||||
|
||||
fn min_extra_char(s: String, dictionary: Vec<String>) -> i32 {
|
||||
for i in 0..s.len() {
|
||||
for w in dictionary {
|
||||
s.starts_with(w);
|
||||
}
|
||||
}
|
||||
1
|
||||
}
|
8
find-all-lonely-numbers-in-the-array/Cargo.toml
Normal file
8
find-all-lonely-numbers-in-the-array/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "find-all-lonely-numbers-in-the-array"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
17
find-all-lonely-numbers-in-the-array/src/main.rs
Normal file
17
find-all-lonely-numbers-in-the-array/src/main.rs
Normal file
|
@ -0,0 +1,17 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
|
||||
struct Solution;
|
||||
|
||||
impl Solution {
|
||||
pub fn find_lonely(mut nums: Vec<i32>) -> Vec<i32> {
|
||||
if nums.len() == 1 {return nums;}
|
||||
|
||||
nums.sort();
|
||||
|
||||
let mut result: Vec<i32> = nums.windows(3).filter(|arr| (arr[0] - arr[1]).abs() > 1).flat_map(|arr| Vec::from(arr)).collect();
|
||||
result.dedup();
|
||||
result
|
||||
}
|
||||
}
|
8
find-first-palindromic-string-in-the-array/Cargo.toml
Normal file
8
find-first-palindromic-string-in-the-array/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "find-first-palindromic-string-in-the-array"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
11
find-first-palindromic-string-in-the-array/src/main.rs
Normal file
11
find-first-palindromic-string-in-the-array/src/main.rs
Normal file
|
@ -0,0 +1,11 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
|
||||
struct Solution;
|
||||
|
||||
impl Solution {
|
||||
pub fn first_palindrome(words: Vec<String>) -> String {
|
||||
words.iter().find(|&s| s.chars().take(s.len() / 2).eq(s.chars().rev().take(s.len() / 2))).map(|x| x.clone()).unwrap_or(String::new())
|
||||
}
|
||||
}
|
8
find-in-mountain-array/Cargo.toml
Normal file
8
find-in-mountain-array/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "find-in-mountain-array"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
28
find-in-mountain-array/src/main.rs
Normal file
28
find-in-mountain-array/src/main.rs
Normal file
|
@ -0,0 +1,28 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
/**
|
||||
* // This is the MountainArray's API interface.
|
||||
* // You should not implement it, or speculate about its implementation
|
||||
* struct MountainArray;
|
||||
* impl MountainArray {
|
||||
* fn get(index:i32)->i32;
|
||||
* fn length()->i32;
|
||||
* };
|
||||
*/
|
||||
|
||||
struct MountainArray;
|
||||
|
||||
impl MountainArray {
|
||||
fn get(self, index:i32)->i32;
|
||||
fn length(self)->i32;
|
||||
};
|
||||
|
||||
impl Solution {
|
||||
pub fn find_in_mountain_array(target: i32, mountainArr: &MountainArray) -> i32 {
|
||||
for i in 0..mountainArr.length() {
|
||||
if mountainArr.get(i) == target {return i;}
|
||||
}
|
||||
-1
|
||||
}
|
||||
}
|
8
find-smallest-letter-greater-than-target/Cargo.toml
Normal file
8
find-smallest-letter-greater-than-target/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "find-smallest-letter-greater-than-target"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
17
find-smallest-letter-greater-than-target/src/main.rs
Normal file
17
find-smallest-letter-greater-than-target/src/main.rs
Normal file
|
@ -0,0 +1,17 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
|
||||
pub fn next_greatest_letter(letters: Vec<char>, target: char) -> char {
|
||||
let mut smallest: char = '~';
|
||||
let first = letters[0];
|
||||
|
||||
for c in letters {
|
||||
if c > target && c < smallest {smallest = c;}
|
||||
}
|
||||
if smallest == '~' {
|
||||
return first;
|
||||
}
|
||||
|
||||
return smallest;
|
||||
}
|
9
find-the-difference-of-two-arrays/Cargo.toml
Normal file
9
find-the-difference-of-two-arrays/Cargo.toml
Normal file
|
@ -0,0 +1,9 @@
|
|||
[package]
|
||||
name = "find-the-difference-of-two-arrays"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
itertools = "0.9.0"
|
25
find-the-difference-of-two-arrays/src/main.rs
Normal file
25
find-the-difference-of-two-arrays/src/main.rs
Normal file
|
@ -0,0 +1,25 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
|
||||
struct Solution {}
|
||||
impl Solution {
|
||||
pub fn find_difference(nums1: Vec<i32>, nums2: Vec<i32>) -> Vec<Vec<i32>> {
|
||||
let mut a: Vec<i32> = Vec::new();
|
||||
let mut b: Vec<i32> = Vec::new();
|
||||
|
||||
for i in nums1 {
|
||||
if !nums2.contains(&i) && !nums1.contains(&i) {
|
||||
a.push(i);
|
||||
}
|
||||
}
|
||||
for i in nums2 {
|
||||
if !nums2.contains(&i) && !nums1.contains(&i) {
|
||||
b.push(i);
|
||||
}
|
||||
}
|
||||
|
||||
vec![a,b]
|
||||
|
||||
}
|
||||
}
|
8
find-the-duplicate-number/Cargo.toml
Normal file
8
find-the-duplicate-number/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "find-the-duplicate-number"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
17
find-the-duplicate-number/src/main.rs
Normal file
17
find-the-duplicate-number/src/main.rs
Normal file
|
@ -0,0 +1,17 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
|
||||
|
||||
struct Solution {}
|
||||
|
||||
impl Solution {
|
||||
pub fn find_duplicate(nums: Vec<i32>) -> i32 {
|
||||
for i in 0..nums.len()-1 {
|
||||
if nums[i+1..nums.len()].contains(&nums[i]) {
|
||||
return nums[i];
|
||||
}
|
||||
}
|
||||
0
|
||||
}
|
||||
}
|
8
find-the-highest-altitude/Cargo.toml
Normal file
8
find-the-highest-altitude/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "find-the-highest-altitude"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
17
find-the-highest-altitude/src/main.rs
Normal file
17
find-the-highest-altitude/src/main.rs
Normal file
|
@ -0,0 +1,17 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
|
||||
struct Solution;
|
||||
|
||||
impl Solution {
|
||||
pub fn largest_altitude(gain: Vec<i32>) -> i32 {
|
||||
let mut altitude = 0;
|
||||
let mut result = 0;
|
||||
for x in gain {
|
||||
altitude += x;
|
||||
result = result.max(altitude);
|
||||
}
|
||||
result
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "find-the-index-of-the-first-occurrence-in-a-string"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
|
@ -0,0 +1,14 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
|
||||
struct Solution;
|
||||
|
||||
impl Solution {
|
||||
pub fn str_str(haystack: String, needle: String) -> i32 {
|
||||
match haystack.find(&needle) {
|
||||
None => -1,
|
||||
Some(e) => e as i32
|
||||
}
|
||||
}
|
||||
}
|
8
fizz-buzz/Cargo.toml
Normal file
8
fizz-buzz/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "fizz-buzz"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
14
fizz-buzz/src/main.rs
Normal file
14
fizz-buzz/src/main.rs
Normal file
|
@ -0,0 +1,14 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
|
||||
impl Solution {
|
||||
pub fn fizz_buzz(n: i32) -> Vec<String> {
|
||||
(1..=n).map(|x| match x {
|
||||
i if i % 3 == 0 && x % 5 == 0 => "FizzBuzz".to_string(),
|
||||
i if i % 3 == 0 => "Fizz".to_string(),
|
||||
i if i % 5 == 0 => "Buzz".to_string(),
|
||||
i => i.to_string()
|
||||
}).collect()
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "group-the-people-given-the-group-size-they-belong-to"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
|
@ -0,0 +1,26 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
|
||||
struct Solution {}
|
||||
impl Solution {
|
||||
pub fn group_the_people(group_sizes: Vec<i32>) -> Vec<Vec<i32>> {
|
||||
let mut result: Vec<Vec<i32>> = Vec::new();
|
||||
|
||||
|
||||
for (i, size) in group_sizes.iter().enumerate() {
|
||||
let group = result.iter_mut()
|
||||
.find(|x| x.capacity() == *size as usize && x.len() != x.capacity());
|
||||
|
||||
match group {
|
||||
Some(entry) => entry.push(i as i32),
|
||||
None => {
|
||||
let mut new_group = Vec::with_capacity(*size as usize);
|
||||
new_group.push(i as i32);
|
||||
result.push(new_group);
|
||||
}
|
||||
};
|
||||
}
|
||||
result
|
||||
}
|
||||
}
|
8
increasing-order-search-tree/Cargo.toml
Normal file
8
increasing-order-search-tree/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "increasing-order-search-tree"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
33
increasing-order-search-tree/src/main.rs
Normal file
33
increasing-order-search-tree/src/main.rs
Normal file
|
@ -0,0 +1,33 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
|
||||
struct Solution {}
|
||||
|
||||
// Definition for a binary tree node.
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct TreeNode {
|
||||
pub val: i32,
|
||||
pub left: Option<Rc<RefCell<TreeNode>>>,
|
||||
pub right: Option<Rc<RefCell<TreeNode>>>,
|
||||
}
|
||||
|
||||
impl TreeNode {
|
||||
#[inline]
|
||||
pub fn new(val: i32) -> Self {
|
||||
TreeNode {
|
||||
val,
|
||||
left: None,
|
||||
right: None
|
||||
}
|
||||
}
|
||||
}
|
||||
use std::rc::Rc;
|
||||
use std::cell::RefCell;
|
||||
impl Solution {
|
||||
pub fn increasing_bst(root: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> {
|
||||
match root.unwrap().get_mut() {
|
||||
TreeNode { val, left: None, right: None } => {root}
|
||||
}
|
||||
}
|
||||
}
|
8
k-th-symbol-in-grammar/Cargo.toml
Normal file
8
k-th-symbol-in-grammar/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "k-th-symbol-in-grammar"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
28
k-th-symbol-in-grammar/src/main.rs
Normal file
28
k-th-symbol-in-grammar/src/main.rs
Normal file
|
@ -0,0 +1,28 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
let tests = vec![
|
||||
(1,1,0),
|
||||
(2,1,0),
|
||||
(2,2,1),
|
||||
(30, 434991989, 0)
|
||||
];
|
||||
for (n,k,o) in tests {
|
||||
let result = Solution::kth_grammar(n,k);
|
||||
println!("{n} {k} {result} {o}");
|
||||
}
|
||||
}
|
||||
|
||||
struct Solution;
|
||||
|
||||
impl Solution {
|
||||
pub fn kth_grammar(n: i32, k: i32) -> i32 {
|
||||
let mut s: Vec<bool> = vec![false];
|
||||
for _ in 0..n {
|
||||
s = s.iter().flat_map(|x| match x {
|
||||
false => [false,true],
|
||||
true => [true,false]
|
||||
}).collect::<Vec<_>>();
|
||||
}
|
||||
s[(k - 1) as usize] as i32
|
||||
}
|
||||
}
|
8
kth-largest-element-in-a-stream/Cargo.toml
Normal file
8
kth-largest-element-in-a-stream/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "kth-largest-element-in-a-stream"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
30
kth-largest-element-in-a-stream/src/main.rs
Normal file
30
kth-largest-element-in-a-stream/src/main.rs
Normal file
|
@ -0,0 +1,30 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
|
||||
|
||||
struct KthLargest {
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* `&self` means the method takes an immutable reference.
|
||||
* If you need a mutable reference, change it to `&mut self` instead.
|
||||
*/
|
||||
impl KthLargest {
|
||||
|
||||
fn new(k: i32, nums: Vec<i32>) -> Self {
|
||||
|
||||
}
|
||||
|
||||
fn add(&self, val: i32) -> i32 {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Your KthLargest object will be instantiated and called as such:
|
||||
* let obj = KthLargest::new(k, nums);
|
||||
* let ret_1: i32 = obj.add(val);
|
||||
*/
|
8
length-of-last-word/Cargo.toml
Normal file
8
length-of-last-word/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "length-of-last-word"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
10
length-of-last-word/src/main.rs
Normal file
10
length-of-last-word/src/main.rs
Normal file
|
@ -0,0 +1,10 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
|
||||
struct Solution {}
|
||||
impl Solution {
|
||||
pub fn length_of_last_word(s: String) -> i32 {
|
||||
s.split_whitespace().last().unwrap().len() as i32
|
||||
}
|
||||
}
|
8
longest-common-prefix/Cargo.toml
Normal file
8
longest-common-prefix/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "longest-common-prefix"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
30
longest-common-prefix/src/main.rs
Normal file
30
longest-common-prefix/src/main.rs
Normal file
|
@ -0,0 +1,30 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
let tests = [
|
||||
(vec!["flower","flow","flight"], "fl"),
|
||||
(vec!["dog","racecar","car"], "")
|
||||
];
|
||||
|
||||
for test in tests {
|
||||
println!("{:?} is {} should be {}", test.0,
|
||||
Solution::longest_common_prefix(test.0.iter().map(|s| s.to_string()).collect()),
|
||||
test.1);
|
||||
}
|
||||
}
|
||||
|
||||
struct Solution {}
|
||||
impl Solution {
|
||||
pub fn longest_common_prefix(strs: Vec<String>) -> String {
|
||||
let mut index = 0;
|
||||
let mut c = String::new();
|
||||
loop {
|
||||
for s in strs {
|
||||
let prefix = s.chars().nth(index);
|
||||
if prefix.is_none() {return c;}
|
||||
}
|
||||
break;
|
||||
}
|
||||
"".to_string()
|
||||
}
|
||||
}
|
||||
|
8
majority-element-ii/Cargo.toml
Normal file
8
majority-element-ii/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "majority-element-ii"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
28
majority-element-ii/src/main.rs
Normal file
28
majority-element-ii/src/main.rs
Normal file
|
@ -0,0 +1,28 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
let tests = [
|
||||
vec![3,2,3],
|
||||
vec![1],
|
||||
vec![1,2]
|
||||
];
|
||||
|
||||
for test in tests {
|
||||
let result = Solution::majority_element(test.clone());
|
||||
println!("{test:?} is {result:?}");
|
||||
}
|
||||
}
|
||||
|
||||
struct Solution;
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
impl Solution {
|
||||
pub fn majority_element(nums: Vec<i32>) -> Vec<i32> {
|
||||
let mut occurance: HashMap<&i32, i32> = HashMap::new();
|
||||
|
||||
for num in &nums {
|
||||
occurance.entry(num).and_modify(|x| {*x += 1}).or_insert(0);
|
||||
}
|
||||
occurance.iter().filter(|(_, &occ)| occ >= nums.len() as i32 / 3).map(|(&&num,_)| num).collect()
|
||||
}
|
||||
}
|
8
majority-element/Cargo.toml
Normal file
8
majority-element/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "majority-element"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
19
majority-element/src/main.rs
Normal file
19
majority-element/src/main.rs
Normal file
|
@ -0,0 +1,19 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
|
||||
struct Solution;
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
impl Solution {
|
||||
pub fn majority_element(nums: Vec<i32>) -> i32 {
|
||||
let mut table = HashMap::new();
|
||||
|
||||
for num in nums {
|
||||
*table.entry(num).or_insert(1) += 1;
|
||||
}
|
||||
|
||||
*table.iter().reduce(|a,b| if a.1 > b.1 {a} else {b}).unwrap().0
|
||||
}
|
||||
}
|
8
matrix-diagonal-sum/Cargo.toml
Normal file
8
matrix-diagonal-sum/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "matrix-diagonal-sum"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
11
matrix-diagonal-sum/src/main.rs
Normal file
11
matrix-diagonal-sum/src/main.rs
Normal file
|
@ -0,0 +1,11 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
|
||||
struct Solution {}
|
||||
impl Solution {
|
||||
pub fn diagonal_sum(mat: Vec<Vec<i32>>) -> i32 {
|
||||
mat.iter().enumerate()
|
||||
.fold(0, |acc, (i, row)| acc + row[i] + if i != row.len()-1 -i {row[row.len()-1 -i]} else {0})
|
||||
}
|
||||
}
|
8
median-of-two-sorted-arrays/Cargo.toml
Normal file
8
median-of-two-sorted-arrays/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "median-of-two-sorted-arrays"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
23
median-of-two-sorted-arrays/src/main.rs
Normal file
23
median-of-two-sorted-arrays/src/main.rs
Normal file
|
@ -0,0 +1,23 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
let tests = vec![
|
||||
(vec![1,3], vec![2], 2.0),
|
||||
(vec![1,2], vec![3,4], 2.5)
|
||||
];
|
||||
for test in tests {
|
||||
println!("{:?}{:?} is {} should be {}", test.0, test.1,
|
||||
Solution::find_median_sorted_arrays(test.0.clone(), test.1.clone()), test.2);
|
||||
}
|
||||
}
|
||||
|
||||
struct Solution {}
|
||||
impl Solution {
|
||||
pub fn find_median_sorted_arrays(nums1: Vec<i32>, nums2: Vec<i32>) -> f64 {
|
||||
let mut a = nums1.clone();
|
||||
a.append(&mut nums2.clone());
|
||||
a.sort();
|
||||
let median = a.get((a.len() / 2)-1..((a.len() + 1)/2)).unwrap();
|
||||
println!("{:?}", median);
|
||||
(median.iter().sum::<i32>() as f64) / (median.len() as f64)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "minimum-changes-to-make-alternating-binary-string"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
|
@ -0,0 +1,15 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
|
||||
struct Solution;
|
||||
|
||||
impl Solution {
|
||||
pub fn min_operations(s: String) -> i32 {
|
||||
let a: Vec<_> = s.chars().collect();
|
||||
i32::min(
|
||||
a.chunks(2).map(|x| match x { ['0','1'] | ['0'] => 0, ['1', '0'] => 2, _ => 1 }).sum(),
|
||||
a.chunks(2).map(|x| match x { ['1','0'] | ['1'] => 0, ['0', '1'] => 2, _ => 1 }).sum()
|
||||
)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "minimum-deletions-to-make-character-frequencies-unique"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
|
@ -0,0 +1,79 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
let tests = [
|
||||
("aab", 0),
|
||||
("aaabbbcc", 2),
|
||||
("ceabaacb", 2),
|
||||
("accdcdadddbaadbc", 1),
|
||||
("abcabc", 3),
|
||||
("bbcebab",2)
|
||||
];
|
||||
|
||||
for test in tests {
|
||||
println!("{:?} is {} should be {}",
|
||||
test.0,
|
||||
Solution::min_deletions(test.0.to_string()),
|
||||
test.1
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
struct Solution {}
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
impl Solution {
|
||||
pub fn min_deletions(s: String) -> i32 {
|
||||
// aaabbbcc => a: 3, b: 3, c: 2
|
||||
let mut char_occurance = HashMap::new();
|
||||
|
||||
for c in s.chars() {
|
||||
char_occurance.entry(c).and_modify(|x| *x += 1).or_insert(1);
|
||||
}
|
||||
|
||||
// 2, 3, 3
|
||||
let mut occurences: Vec<i32> = char_occurance.values().cloned().collect();
|
||||
occurences.sort();
|
||||
|
||||
// 2: 1, 3: 2
|
||||
let mut occurence_occurences = HashMap::new();
|
||||
// println!("{:?}", occurences);
|
||||
for i in occurences {
|
||||
occurence_occurences.entry(i).and_modify(|x| *x += 1).or_insert(1);
|
||||
}
|
||||
|
||||
let a = occurence_occurences.iter().collect::<Vec<_>>();
|
||||
// println!("{:?}", a);
|
||||
|
||||
// 3
|
||||
let mut needs_change = Vec::new();
|
||||
for (key, val) in occurence_occurences.iter() {
|
||||
if *val > 1 {
|
||||
needs_change.push(*key);
|
||||
}
|
||||
}
|
||||
|
||||
let mut changes = 0;
|
||||
for key in needs_change.iter() {
|
||||
let mut val = occurence_occurences.get(&key).unwrap().clone();
|
||||
while val > 1 {
|
||||
for i in (0..*key).rev() {
|
||||
let occ = occurence_occurences.get(&i);
|
||||
if occ.is_none() {
|
||||
val += -1;
|
||||
changes += key - i;
|
||||
if i > 0 {
|
||||
occurence_occurences.insert(i, key-i);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
// println!("{occurence_occurences:?}");
|
||||
changes
|
||||
}
|
||||
}
|
||||
|
8
minimum-operations-to-reduce-x-to-zero/Cargo.toml
Normal file
8
minimum-operations-to-reduce-x-to-zero/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "minimum-operations-to-reduce-x-to-zero"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
39
minimum-operations-to-reduce-x-to-zero/src/main.rs
Normal file
39
minimum-operations-to-reduce-x-to-zero/src/main.rs
Normal file
|
@ -0,0 +1,39 @@
|
|||
use std::cmp::min;
|
||||
|
||||
|
||||
struct Solution;
|
||||
impl Solution {
|
||||
pub fn min_operations(nums: Vec<i32>, x: i32) -> i32 {
|
||||
Self::min_operations_slice(&nums[..], x)
|
||||
}
|
||||
|
||||
pub fn min_operations_slice(nums: &[i32], x: i32) -> i32 {
|
||||
if x == 0 {return 0;}
|
||||
if x < 0 || nums.is_empty() {return -1;}
|
||||
|
||||
match (
|
||||
Self::min_operations_slice(&nums[..nums.len() - 1], x - &nums[nums.len() - 1]),
|
||||
Self::min_operations_slice(&nums[1..], x - &nums[0])
|
||||
) {
|
||||
(-1, -1) => -1,
|
||||
(x,-1) | (-1, x) => x+1,
|
||||
(x, y) => min(x,y)+1,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
let tests = vec![
|
||||
(vec![1,1,4,2,3], 5, 2),
|
||||
(vec![5,6,7,8,9], 4, -1),
|
||||
(vec![3,2,20,1,1,3], 10, 5),
|
||||
(vec![1,1], 3, -1),
|
||||
(vec![1241,8769,9151,3211,2314,8007,3713,5835,2176,8227,5251,9229,904,1899,5513,7878,8663,3804,2685,3501,1204,9742,2578,8849,1120,4687,5902,9929,6769,8171,5150,1343,9619,3973,3273,6427,47,8701,2741,7402,1412,2223,8152,805,6726,9128,2794,7137,6725,4279,7200,5582,9583,7443,6573,7221,1423,4859,2608,3772,7437,2581,975,3893,9172,3,3113,2978,9300,6029,4958,229,4630,653,1421,5512,5392,7287,8643,4495,2640,8047,7268,3878,6010,8070,7560,8931,76,6502,5952,4871,5986,4935,3015,8263,7497,8153,384,1136], 894887480, 0)
|
||||
];
|
||||
|
||||
for (nums, x, out) in tests {
|
||||
let result = Solution::min_operations(nums.clone(), x);
|
||||
println!("{nums:?} {x} is {result} should be {out}");
|
||||
}
|
||||
}
|
38
minimum-operations-to-reduce-x-to-zero/src/main2.rs
Normal file
38
minimum-operations-to-reduce-x-to-zero/src/main2.rs
Normal file
|
@ -0,0 +1,38 @@
|
|||
use std::cmp::min;
|
||||
|
||||
struct Solution;
|
||||
impl Solution {
|
||||
pub fn min_operations(nums: Vec<i32>, x: i32) -> i32 {
|
||||
Self::min_operations_slice(&nums[..], x).unwrap_or(-1)
|
||||
}
|
||||
|
||||
pub fn min_operations_slice(nums: &[i32], x: i32) -> Option<i32> {
|
||||
if x < 0 || nums.is_empty() {return None;}
|
||||
if x == 0 {return Some(0);}
|
||||
|
||||
match (
|
||||
Self::min_operations_slice(&nums[..nums.len() - 1], x - &nums[nums.len() - 1]),
|
||||
Self::min_operations_slice(&nums[1..], x - &nums[0])
|
||||
) {
|
||||
(None, None) => None,
|
||||
(Some(x), Some(y)) => Some(min(x,y)+1),
|
||||
(Some(x),None) | (None, Some(x)) => Some(x+1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
let tests = vec![
|
||||
(vec![1,1,4,2,3], 5, 2),
|
||||
(vec![5,6,7,8,9], 4, -1),
|
||||
(vec![3,2,20,1,1,3], 10, 5),
|
||||
(vec![1,1], 3, -1),
|
||||
(vec![1241,8769,9151,3211,2314,8007,3713,5835,2176,8227,5251,9229,904,1899,5513,7878,8663,3804,2685,3501,1204,9742,2578,8849,1120,4687,5902,9929,6769,8171,5150,1343,9619,3973,3273,6427,47,8701,2741,7402,1412,2223,8152,805,6726,9128,2794,7137,6725,4279,7200,5582,9583,7443,6573,7221,1423,4859,2608,3772,7437,2581,975,3893,9172,3,3113,2978,9300,6029,4958,229,4630,653,1421,5512,5392,7287,8643,4495,2640,8047,7268,3878,6010,8070,7560,8931,76,6502,5952,4871,5986,4935,3015,8263,7497,8153,384,1136], 894887480, 0)
|
||||
];
|
||||
|
||||
for (nums, x, out) in tests {
|
||||
let result = Solution::min_operations(nums.clone(), x);
|
||||
println!("{nums:?} {x} is {result} should be {out}");
|
||||
}
|
||||
}
|
8
monotonic-array/Cargo.toml
Normal file
8
monotonic-array/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "monotonic-array"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
20
monotonic-array/src/main.rs
Normal file
20
monotonic-array/src/main.rs
Normal file
|
@ -0,0 +1,20 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
let tests = [
|
||||
(vec![1,2,2,3],true),
|
||||
(vec![6,5,4,4],true),
|
||||
(vec![1,3,2], false)
|
||||
];
|
||||
|
||||
for test in tests {
|
||||
println!("{:?} is {} should be {}", test.0, Solution::is_monotonic(test.0.clone()),
|
||||
test.1);
|
||||
}
|
||||
}
|
||||
struct Solution;
|
||||
impl Solution {
|
||||
pub fn is_monotonic(nums: Vec<i32>) -> bool {
|
||||
nums.windows(2).all(|x| x[0] >= x[1]) ||
|
||||
nums.windows(2).all(|x| x[0] <= x[1])
|
||||
}
|
||||
}
|
8
n-ary-tree-postorder-traversal/Cargo.toml
Normal file
8
n-ary-tree-postorder-traversal/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "n-ary-tree-postorder-traversal"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
3
n-ary-tree-postorder-traversal/src/main.rs
Normal file
3
n-ary-tree-postorder-traversal/src/main.rs
Normal file
|
@ -0,0 +1,3 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
8
next-greater-element-i/Cargo.toml
Normal file
8
next-greater-element-i/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "next-greater-element-i"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
17
next-greater-element-i/src/main.rs
Normal file
17
next-greater-element-i/src/main.rs
Normal file
|
@ -0,0 +1,17 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
|
||||
struct Solution {}
|
||||
|
||||
impl Solution {
|
||||
pub fn next_greater_element(nums1: Vec<i32>, nums2: Vec<i32>) -> Vec<i32> {
|
||||
let mut result = nums1.clone();
|
||||
for i in result.iter_mut() {
|
||||
nums2.iter().position(|&x| x == *i);
|
||||
}
|
||||
|
||||
|
||||
result
|
||||
}
|
||||
}
|
8
number-of-different-integers-in-a-string/Cargo.toml
Normal file
8
number-of-different-integers-in-a-string/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "number-of-different-integers-in-a-string"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
29
number-of-different-integers-in-a-string/src/main.rs
Normal file
29
number-of-different-integers-in-a-string/src/main.rs
Normal file
|
@ -0,0 +1,29 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
let tests = [
|
||||
("a123bc34d8ef34", 3),
|
||||
("leet1234code234", 2),
|
||||
("a1b01c001", 1),
|
||||
("035985750011523523129774573439111590559325a1554234973", 2)
|
||||
];
|
||||
for test in tests {
|
||||
println!("{:?} is {} should be {}", test.0,
|
||||
Solution::num_different_integers(test.0.to_string()), test.1);
|
||||
}
|
||||
}
|
||||
|
||||
struct Solution;
|
||||
|
||||
use std::collections::HashSet;
|
||||
|
||||
impl Solution {
|
||||
pub fn num_different_integers(word: String) -> i32 {
|
||||
let result = unsafe { word.as_bytes() }.split(u8::is_ascii_alphabetic)
|
||||
.filter(|x| !x.is_empty())
|
||||
.map(|mut x| {while x.starts_with(b"0") {x = x.strip_prefix(b"0").unwrap(); } x})
|
||||
// .map(|x| x.iter().rev().enumerate().fold(0, |acc, (i,&v)| acc + (v as u32 -48) * 10_u32.pow(i as u32)))
|
||||
.collect::<HashSet<&[u8]>>();
|
||||
println!("{result:?}");
|
||||
result.len() as i32
|
||||
}
|
||||
}
|
8
number-of-equivalent-domino-pairs/Cargo.toml
Normal file
8
number-of-equivalent-domino-pairs/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "number-of-equivalent-domino-pairs"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
17
number-of-equivalent-domino-pairs/src/main.rs
Normal file
17
number-of-equivalent-domino-pairs/src/main.rs
Normal file
|
@ -0,0 +1,17 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
|
||||
struct Solution;
|
||||
|
||||
impl Solution {
|
||||
pub fn num_equiv_domino_pairs(dominoes: Vec<Vec<i32>>) -> i32 {
|
||||
let mut map = [0;100];
|
||||
for d in dominoes {
|
||||
if let [i,j] = d[..] {
|
||||
map[(i.max(j) * 10 + i.min(j)) as usize] += 1;
|
||||
}
|
||||
}
|
||||
map.iter().filter(|&&x| x > 1).map(|&x| (x *(x-1))/2).sum::<i32>()
|
||||
}
|
||||
}
|
8
palindrome-number/Cargo.toml
Normal file
8
palindrome-number/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "palindrome-number"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
31
palindrome-number/src/main.rs
Normal file
31
palindrome-number/src/main.rs
Normal file
|
@ -0,0 +1,31 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
let tests = [
|
||||
(121, true),
|
||||
(-121, false),
|
||||
(10, false),
|
||||
(1410110141, true)
|
||||
];
|
||||
for test in tests {
|
||||
println!("{} is {} should be {}", test.0, Solution::is_palindrome(test.0), test.1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
struct Solution {}
|
||||
impl Solution {
|
||||
pub fn is_palindrome(x: i32) -> bool {
|
||||
if x < 0 {return false;}
|
||||
let len = (x as f32).log10() as usize;
|
||||
//println!("{} {}", len, (len+1)/2);
|
||||
for i in 0..(len+1)/2 {
|
||||
if (x as u32 / 10_u32.pow(i as u32)) % 10 != (x as u32 / 10_u32.pow((len - i) as u32)) % 10 {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
pub fn get(x: i32, i: usize) -> u32 {
|
||||
(x as u32 / 10_u32.pow(i as u32)) % 10
|
||||
}
|
||||
}
|
8
pascals-triangle/Cargo.toml
Normal file
8
pascals-triangle/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "pascals-triangle"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
32
pascals-triangle/src/main.rs
Normal file
32
pascals-triangle/src/main.rs
Normal file
|
@ -0,0 +1,32 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
let tests = vec![
|
||||
(5, vec![
|
||||
vec![1],vec![1,1],vec![1,2,1],vec![1,3,3,1],vec![1,4,6,4,1]
|
||||
]),
|
||||
(1, vec![vec![1]])
|
||||
];
|
||||
|
||||
for test in tests {
|
||||
println!("{} is {:?} should be {:?}", test.0, Solution::generate(test.0), test.1);
|
||||
}
|
||||
}
|
||||
|
||||
struct Solution {}
|
||||
impl Solution {
|
||||
pub fn generate(num_rows: i32) -> Vec<Vec<i32>> {
|
||||
let mut rows = Vec::with_capacity(num_rows as usize);
|
||||
rows.push(vec![1]);
|
||||
for i in 1..num_rows {
|
||||
let mut row: Vec<i32> = Vec::with_capacity((i+1) as usize);
|
||||
for ii in 0..=i {
|
||||
let above = &rows[(i-1) as usize];
|
||||
row.push(if ii == 0 || i == ii {1} else {above[(ii-1) as usize] + above[ii as usize]});
|
||||
}
|
||||
rows.push(row);
|
||||
}
|
||||
|
||||
rows
|
||||
}
|
||||
}
|
||||
|
8
plus-one/Cargo.toml
Normal file
8
plus-one/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "plus-one"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
21
plus-one/src/main.rs
Normal file
21
plus-one/src/main.rs
Normal file
|
@ -0,0 +1,21 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
|
||||
struct Solution {}
|
||||
impl Solution {
|
||||
pub fn plus_one(digits: Vec<i32>) -> Vec<i32> {
|
||||
let mut result = Vec::with_capacity(digits.len()+1);
|
||||
let mut carry = 1;
|
||||
for i in digits.iter().rev() {
|
||||
let sum = *i + carry;
|
||||
result.push(sum % 10);
|
||||
carry = sum / 10;
|
||||
}
|
||||
if carry > 0 {
|
||||
result.push(carry);
|
||||
}
|
||||
result.reverse();
|
||||
result
|
||||
}
|
||||
}
|
8
remove-all-occurrences-of-a-substring/Cargo.toml
Normal file
8
remove-all-occurrences-of-a-substring/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "remove-all-occurrences-of-a-substring"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
14
remove-all-occurrences-of-a-substring/src/main.rs
Normal file
14
remove-all-occurrences-of-a-substring/src/main.rs
Normal file
|
@ -0,0 +1,14 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
|
||||
struct Solution;
|
||||
|
||||
impl Solution {
|
||||
pub fn remove_occurrences(mut s: String, part: String) -> String {
|
||||
while s.contains(&part) {
|
||||
s = s.replacen(&part, "", 1);
|
||||
}
|
||||
s
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "remove-colored-pieces-if-both-neighbors-are-the-same-color"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
|
@ -0,0 +1,31 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
let tests = [
|
||||
("AAABABB", true),
|
||||
("AA", false),
|
||||
("ABBBBBBBAAA", false),
|
||||
("AAAABBBB", false)
|
||||
];
|
||||
for (input, output) in tests {
|
||||
let result = Solution::winner_of_game(input.to_string());
|
||||
println!("{input:?} is {result} should be {output:?}");
|
||||
}
|
||||
}
|
||||
|
||||
struct Solution;
|
||||
|
||||
impl Solution {
|
||||
pub fn winner_of_game(mut colors: String) -> bool {
|
||||
if colors.len() < 3 {return false;}
|
||||
let mut is_alice_turn = true;
|
||||
while ((is_alice_turn && colors.contains("AAA")) || (!is_alice_turn) && colors.contains("BBB")) {
|
||||
if is_alice_turn {
|
||||
colors = colors.replacen("AAA", "AA", 1);
|
||||
} else {
|
||||
colors = colors.replacen("BBB", "BB", 1);
|
||||
}
|
||||
is_alice_turn = !is_alice_turn
|
||||
}
|
||||
!is_alice_turn
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue