Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
425 changes: 217 additions & 208 deletions src/solution/mod.rs

Large diffs are not rendered by default.

72 changes: 72 additions & 0 deletions src/solution/s0343_integer_break.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* [343] Integer Break
*
* Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get.
*
* Example 1:
*
* <div>
*
* Input: <span id="example-input-1-1">2</span>
* Output: <span id="example-output-1">1</span>
* Explanation: 2 = 1 + 1, 1 &times; 1 = 1.
*
* <div>
* Example 2:
*
*
* Input: <span id="example-input-2-1">10</span>
* Output: <span id="example-output-2">36</span>
* Explanation: 10 = 3 + 3 + 4, 3 &times; 3 &times; 4 = 36.
*
* Note: You may assume that n is not less than 2 and not larger than 58.
* </div>
* </div>
*/
pub struct Solution {}

// submission codes start here

impl Solution {
pub fn integer_break(n: i32) -> i32 {
// dp
if n == 2 {
return 1;
}
if n == 3 {
return 2;
}
let mut dp = vec![0; (n + 1) as usize];
// when used as factor, no need to break to addends
dp[2] = 2;
dp[3] = 3;
for i in 4..(n + 1) {
Self::helper(&mut dp, i);
}
dp[n as usize]
}

fn helper(dp: &mut Vec<i32>, n: i32) {
let mut num1: usize = 2;
let mut num2: usize = n as usize - 2;
let mut res = 0;
while num1 <= num2 {
res = res.max(dp[num1] * dp[num2]);
num1 += 1;
num2 -= 1;
}
dp[n as usize] = res;
}
}

// submission codes end

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_343() {
println!("{}", Solution::integer_break(10));
}
}
58 changes: 58 additions & 0 deletions src/solution/s0383_ransom_note.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use std::collections::HashMap;

/**
* [383] Ransom Note
*
*
* Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom
* note can be constructed from the magazines ; otherwise, it will return false.
*
*
* Each letter in the magazine string can only be used once in your ransom note.
*
*
* Note:<br />
* You may assume that both strings contain only lowercase letters.
*
*
*
* canConstruct("a", "b") -> false
* canConstruct("aa", "ab") -> false
* canConstruct("aa", "aab") -> true
*
*
*/
pub struct Solution {}

// submission codes start here

impl Solution {
pub fn can_construct(ransom_note: String, magazine: String) -> bool {
use std::collections::HashMap;
let mut map = HashMap::new();
for c in magazine.chars() {
if map.contains_key(&c) {
map.insert(c, map.get(&c).unwrap() + 1);
} else {
map.insert(c, 1);
}
}
for c in ransom_note.chars() {
if !map.contains_key(&c) || map.get(&c).unwrap() < &1 {
return false;
}
map.insert(c, map.get(&c).unwrap() - 1);
}
true
}
}

// submission codes end

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_383() {}
}
48 changes: 48 additions & 0 deletions src/solution/s0387_first_unique_character_in_a_string.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* [387] First Unique Character in a String
*
*
* Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.
*
* Examples:
*
* s = "leetcode"
* return 0.
*
* s = "loveleetcode",
* return 2.
*
*
*
*
* Note: You may assume the string contain only lowercase letters.
*
*/
pub struct Solution {}

// submission codes start here

impl Solution {
pub fn first_uniq_char(s: String) -> i32 {
let mut lookup = [0; 256];
for c in s.chars() {
lookup[c as usize] = lookup[c as usize] + 1;
}
for (i, c) in s.chars().enumerate() {
if lookup[c as usize] == 1 {
return i as i32;
}
}
-1
}
}

// submission codes end

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_387() {}
}
57 changes: 57 additions & 0 deletions src/solution/s0409_longest_palindrome.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* [409] Longest Palindrome
*
* Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.
*
* This is case sensitive, for example "Aa" is not considered a palindrome here.
*
* Note:<br />
* Assume the length of given string will not exceed 1,010.
*
*
* Example:
*
* Input:
* "abccccdd"
*
* Output:
* 7
*
* Explanation:
* One longest palindrome that can be built is "dccaccd", whose length is 7.
*
*
*/
pub struct Solution {}

// submission codes start here

impl Solution {
pub fn longest_palindrome(s: String) -> i32 {
let mut lookup = vec![0; 256];
for c in s.chars() {
lookup[c as usize] = lookup[c as usize] + 1;
}
let mut ret = 0;
let mut extra = 0;
for i in lookup {
if i % 2 == 1 {
extra = 1;
}
ret += i / 2 * 2;
}
ret + extra
}
}

// submission codes end

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_409() {
println!("{}", Solution::longest_palindrome("abccccdd".to_string()));
}
}
63 changes: 63 additions & 0 deletions src/solution/s0412_fizz_buzz.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* [412] Fizz Buzz
*
* Write a program that outputs the string representation of numbers from 1 to n.
*
* But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.
*
* Example:
*
* n = 15,
*
* Return:
* [
* "1",
* "2",
* "Fizz",
* "4",
* "Buzz",
* "Fizz",
* "7",
* "8",
* "Fizz",
* "Buzz",
* "11",
* "Fizz",
* "13",
* "14",
* "FizzBuzz"
* ]
*
*
*/
pub struct Solution {}

// submission codes start here

impl Solution {
pub fn fizz_buzz(n: i32) -> Vec<String> {
let mut ret = vec![];
for i in 1..(n + 1) {
if i % 15 == 0 {
ret.push("FizzBuzz".to_string());
} else if i % 3 == 0 {
ret.push("Fizz".to_string());
} else if i % 5 == 0 {
ret.push("Buzz".to_string());
} else {
ret.push(i.to_string());
}
}
ret
}
}

// submission codes end

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_412() {}
}
81 changes: 81 additions & 0 deletions src/solution/s0414_third_maximum_number.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* [414] Third Maximum Number
*
* Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n).
*
* Example 1:<br />
*
* Input: [3, 2, 1]
*
* Output: 1
*
* Explanation: The third maximum is 1.
*
*
*
* Example 2:<br />
*
* Input: [1, 2]
*
* Output: 2
*
* Explanation: The third maximum does not exist, so the maximum (2) is returned instead.
*
*
*
* Example 3:<br />
*
* Input: [2, 2, 3, 1]
*
* Output: 1
*
* Explanation: Note that the third maximum here means the third maximum distinct number.
* Both numbers with value 2 are both considered as second maximum.
*
*
*/
pub struct Solution {}

// submission codes start here

impl Solution {
pub fn third_max(nums: Vec<i32>) -> i32 {
// use min heap
let mut heap = vec![std::i64::MIN; 3];
for num in nums {
let num = num as i64;
if heap.contains(&num) {
continue;
}
if num > heap[0] {
heap[0] = num;
if heap[0] > heap[1] {
let tmp = heap[0];
heap[0] = heap[1];
heap[1] = tmp;
}
if heap[0] > heap[2] {
let tmp = heap[0];
heap[0] = heap[2];
heap[2] = tmp;
}
}
}
if heap.contains(&std::i64::MIN) {
return heap[1].max(heap[2]) as i32;
}
heap[0] as i32
}
}

// submission codes end

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_414() {
println!("{}", Solution::third_max(vec![1, 2, 2, 4]))
}
}
Loading