Skip to content

Commit 9108bf4

Browse files
committed
6-23-2020
1 parent 7fb4275 commit 9108bf4

18 files changed

+481
-0
lines changed

src/_0009_palindrome_number.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn is_palindrome(x: i32) -> bool {
5+
if x < 0 {
6+
return false;
7+
}
8+
let mut digits = Vec::new();
9+
let mut number = x;
10+
while number > 0 {
11+
digits.push(number % 10);
12+
number /= 10;
13+
}
14+
digits == digits.iter().copied().rev().collect::<Vec<i32>>()
15+
}
16+
}
17+
18+
#[test]
19+
fn test() {
20+
assert_eq!(Solution::is_palindrome(121), true);
21+
assert_eq!(Solution::is_palindrome(-121), false);
22+
assert_eq!(Solution::is_palindrome(10), false);
23+
}

src/_0067_add_binary.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn add_binary(a: String, b: String) -> String {
5+
let a_ = u128::from_str_radix(&a, 2).unwrap_or(0);
6+
let b_ = u128::from_str_radix(&b, 2).unwrap_or(0);
7+
format!("{:b}", a_ + b_)
8+
}
9+
}
10+
11+
#[test]
12+
fn test() {
13+
assert_eq!(
14+
Solution::add_binary("11".to_string(), "1".to_string()),
15+
"100".to_string()
16+
);
17+
18+
assert_eq!(
19+
Solution::add_binary("1010".to_string(), "1011".to_string()),
20+
"10101".to_string()
21+
);
22+
}

src/_0070_climbing_stairs.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn climb_stairs(n: i32) -> i32 {
5+
match n {
6+
0 => 1,
7+
1 => 1,
8+
_ => {
9+
let mut step = vec![0 as i32; n as usize + 1];
10+
step[0] = 1;
11+
step[1] = 1;
12+
for i in 2..=n {
13+
step[i as usize] = step[i as usize - 1] + step[i as usize - 2];
14+
}
15+
return step[n as usize];
16+
}
17+
}
18+
}
19+
}
20+
21+
#[test]
22+
fn test() {
23+
assert_eq!(Solution::climb_stairs(2), 2);
24+
assert_eq!(Solution::climb_stairs(3), 3);
25+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn max_profit(prices: Vec<i32>) -> i32 {
5+
let mut min_value: i32 = std::i32::MAX;
6+
let mut max_profit: i32 = 0;
7+
8+
for v in prices.into_iter() {
9+
if v < min_value {
10+
min_value = v - 0;
11+
} else if v - min_value > max_profit {
12+
max_profit = v - min_value;
13+
}
14+
}
15+
16+
max_profit
17+
}
18+
}
19+
20+
#[test]
21+
fn test() {
22+
assert_eq!(Solution::max_profit(vec![7, 1, 5, 3, 6, 4]), 5);
23+
assert_eq!(Solution::max_profit(vec![7, 6, 4, 3, 1]), 0);
24+
}

src/_0136_single_number.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn single_number(nums: Vec<i32>) -> i32 {
5+
let mut result = 0;
6+
for v in nums {
7+
result ^= v;
8+
}
9+
result
10+
}
11+
}
12+
13+
#[test]
14+
fn test() {
15+
assert_eq!(Solution::single_number(vec![4, 1, 2, 1, 2]), 4);
16+
assert_eq!(Solution::single_number(vec![2, 2, 1]), 1);
17+
}

src/_0204_count_primes.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn count_primes(n: i32) -> i32 {
5+
if n < 3 {
6+
return 0;
7+
}
8+
let mut is_prime = vec![true; n as usize];
9+
is_prime[0] = false;
10+
is_prime[1] = false;
11+
let mut i: usize = 2;
12+
while i * i < n as usize {
13+
if is_prime[i] {
14+
let mut j = i * 2;
15+
while j < n as usize {
16+
is_prime[j] = false;
17+
j += i;
18+
}
19+
}
20+
i += 1;
21+
}
22+
23+
let mut count: i32 = 0;
24+
for v in is_prime {
25+
if v {
26+
count += 1;
27+
}
28+
}
29+
count
30+
}
31+
}
32+
33+
#[test]
34+
fn test() {
35+
assert_eq!(Solution::count_primes(345), 68);
36+
assert_eq!(Solution::count_primes(2), 0);
37+
}

src/_0219_contains_duplicate_ii.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
struct Solution;
2+
use std::collections::HashMap;
3+
4+
impl Solution {
5+
pub fn contains_nearby_duplicate(nums: Vec<i32>, k: i32) -> bool {
6+
let mut map: HashMap<i32, usize> = HashMap::new();
7+
let k = k as usize;
8+
for (i, v) in nums.into_iter().enumerate() {
9+
if let Some(j) = map.get(&v) {
10+
if i - j < k {
11+
return true;
12+
}
13+
}
14+
map.insert(v, i);
15+
}
16+
false
17+
}
18+
}
19+
20+
#[test]
21+
fn test() {
22+
assert_eq!(
23+
Solution::contains_nearby_duplicate(vec![1, 2, 3, 1], 3),
24+
true
25+
);
26+
assert_eq!(
27+
Solution::contains_nearby_duplicate(vec![1, 0, 1, 1], 1),
28+
true
29+
);
30+
assert_eq!(
31+
Solution::contains_nearby_duplicate(vec![1, 2, 3, 1, 2, 3], 2),
32+
false
33+
);
34+
}

src/_0266_palindrome_permutation.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn can_permute_palindrome(s: String) -> bool {
5+
let mut count = vec![0 as u8; 256];
6+
for v in s.chars() {
7+
count[(v as u8 - 'a' as u8) as usize] += 1;
8+
}
9+
let mut switch = false;
10+
for v in count {
11+
if v % 2 != 0 {
12+
if switch {
13+
return false;
14+
} else {
15+
switch = true;
16+
}
17+
}
18+
}
19+
true
20+
}
21+
}
22+
23+
#[test]
24+
fn test() {
25+
assert_eq!(Solution::can_permute_palindrome("code".to_string()), false);
26+
assert_eq!(Solution::can_permute_palindrome("aab".to_string()), true);
27+
assert_eq!(
28+
Solution::can_permute_palindrome("carerac".to_string()),
29+
true
30+
);
31+
}

src/_0412_fizz_buzz.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
struct Solution;
2+
// use vec_string;
3+
4+
impl Solution {
5+
pub fn fizz_buzz(n: i32) -> Vec<String> {
6+
let mut result: Vec<String> = vec![];
7+
let mut fizz: bool;
8+
let mut buzz: bool;
9+
for i in 1..=n {
10+
fizz = i % 3 == 0;
11+
buzz = i % 5 == 0;
12+
let res = match (fizz, buzz) {
13+
(true, true) => "FizzBuzz".to_string(),
14+
(true, false) => "Fizz".to_string(),
15+
(false, true) => "Buzz".to_string(),
16+
(false, false) => format!("{}", i),
17+
};
18+
result.push(res);
19+
}
20+
result
21+
}
22+
}
23+
24+
#[test]
25+
fn test() {
26+
let output: Vec<String> = vec_string![
27+
"1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14",
28+
"FizzBuzz"
29+
];
30+
assert_eq!(Solution::fizz_buzz(15), output);
31+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn reverse_words(s: String) -> String {
5+
let mut result = vec![];
6+
for v in s.split_whitespace().collect::<Vec<&str>>() {
7+
result.push(v.chars().rev().collect::<String>());
8+
}
9+
result.join(" ")
10+
}
11+
}
12+
13+
#[test]
14+
fn test() {
15+
assert_eq!(
16+
Solution::reverse_words("Let's take LeetCode contest".to_string()),
17+
"s'teL ekat edoCteeL tsetnoc".to_string()
18+
);
19+
}

src/_0605_can_place_flowers.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn can_place_flowers(flowerbed: Vec<i32>, n: i32) -> bool {
5+
let mut zero_count: i32 = 1;
6+
let mut count = 0;
7+
let mut flowerbed = flowerbed;
8+
flowerbed.push(0);
9+
flowerbed.push(1);
10+
print!("{:?}", flowerbed);
11+
12+
for v in flowerbed {
13+
if v == 0 {
14+
zero_count += 1;
15+
} else {
16+
count += (zero_count - 1) / 2;
17+
zero_count = 0;
18+
}
19+
}
20+
count >= n
21+
}
22+
}
23+
24+
#[test]
25+
fn test() {
26+
assert_eq!(Solution::can_place_flowers(vec![1, 0, 0, 0, 1], 1), true);
27+
assert_eq!(Solution::can_place_flowers(vec![1, 0, 0, 0, 1], 2), true);
28+
}

src/_0680_valid_palindrome_ii.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
struct Solution;
2+
3+
fn is_palindrome(s: &str) -> bool {
4+
for (a, b) in s.chars().zip(s.chars().rev()) {
5+
if a != b {
6+
return false;
7+
}
8+
}
9+
true
10+
}
11+
12+
impl Solution {
13+
pub fn valid_palindrome(s: String) -> bool {
14+
let mut l: usize = 0;
15+
let mut r: usize = s.len() - 1;
16+
while l < r {
17+
if s[l..=l] == s[r..=r] {
18+
l += 1;
19+
r -= 1;
20+
} else if is_palindrome(&s[l..r]) {
21+
return true;
22+
} else if is_palindrome(&s[(l + 1)..=r]) {
23+
return true;
24+
} else {
25+
return false;
26+
}
27+
}
28+
true
29+
}
30+
}
31+
#[test]
32+
fn test() {
33+
let s = "aba".to_string();
34+
assert_eq!(Solution::valid_palindrome(s), true);
35+
let s = "abca".to_string();
36+
assert_eq!(Solution::valid_palindrome(s), true);
37+
let s = "accca".to_string();
38+
assert_eq!(Solution::valid_palindrome(s), true);
39+
}

src/_0771_jewels_and_stones.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn num_jewels_in_stones(j: String, s: String) -> i32 {
5+
let mut jewels = vec![false; 58];
6+
for v in j.chars() {
7+
jewels[(v as u8 - 'A' as u8) as usize] = true;
8+
}
9+
10+
let mut result: i32 = 0;
11+
for v in s.chars() {
12+
if jewels[(v as u8 - 'A' as u8) as usize] {
13+
result += 1;
14+
}
15+
}
16+
result
17+
}
18+
}
19+
20+
#[test]
21+
fn test() {
22+
assert_eq!(
23+
Solution::num_jewels_in_stones("aA".to_string(), "aAAbbbb".to_string()),
24+
3
25+
);
26+
assert_eq!(
27+
Solution::num_jewels_in_stones("z".to_string(), "ZZ".to_string()),
28+
0
29+
);
30+
}

src/_0796_rotate_string.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn rotate_string(a: String, b: String) -> bool {
5+
if a.len() != b.len() {
6+
return false;
7+
}
8+
let mut pool = "".to_string();
9+
pool += &a;
10+
pool += &a;
11+
pool.contains(&b)
12+
}
13+
}
14+
15+
#[test]
16+
fn test() {
17+
assert_eq!(
18+
Solution::rotate_string("abcde".to_string(), "cdeab".to_string()),
19+
true
20+
);
21+
assert_eq!(
22+
Solution::rotate_string("abcde".to_string(), "abced".to_string()),
23+
false
24+
);
25+
}

0 commit comments

Comments
 (0)