Skip to content

Solve some problems #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
9 changes: 9 additions & 0 deletions src/solution/mod.rs
Original file line number Diff line number Diff line change
@@ -235,3 +235,12 @@ mod s0111_minimum_depth_of_binary_tree;
mod s0092_reverse_linked_list_ii;
mod s0303_range_sum_query_immutable;
mod s0102_binary_tree_level_order_traversal;
mod s0318_maximum_product_of_word_lengths;
mod s0319_bulb_switcher;
mod s0326_power_of_three;
mod s0344_reverse_string;
mod s0345_reverse_vowels_of_a_string;
mod s0349_intersection_of_two_arrays;
mod s0350_intersection_of_two_arrays_ii;
mod s0367_valid_perfect_square;
mod s0371_sum_of_two_integers;
64 changes: 64 additions & 0 deletions src/solution/s0318_maximum_product_of_word_lengths.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* [318] Maximum Product of Word Lengths
*
* Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lower case letters. If no such two words exist, return 0.
*
* Example 1:
*
*
* Input: ["abcw","baz","foo","bar","xtfn","abcdef"]
* Output: 16
* Explanation: The two words can be "abcw", "xtfn"<span style="font-family: sans-serif, Arial, Verdana, "Trebuchet MS";">.</span>
*
* Example 2:
*
*
* Input: ["a","ab","abc","d","cd","bcd","abcd"]
* Output: 4
* Explanation: The two words can be "ab", "cd"<span style="font-family: sans-serif, Arial, Verdana, "Trebuchet MS";">.</span>
*
* Example 3:
*
*
* Input: ["a","aa","aaa","aaaa"]
* Output: 0
* Explanation: No such pair of words.
*
*
*/
pub struct Solution {}

// submission codes start here

impl Solution {
pub fn max_product(words: Vec<String>) -> i32 {
let mut ret = 0;
// use a int value to store word characters
// if a word contains 'a', then the resulting int is b'00000000000000000000000000000001'
// if it also contains 'b', then the resulting int is b'00000000000000000000000000000011' and so on
let mut values = vec![0; words.len()];
for (i, word) in words.iter().enumerate() {
for c in word.as_bytes() {
values[i] |= 1 << (c - b'a');
}
}
for i in 0..words.len() {
for j in i + 1..words.len() {
if values[i] & values[j] == 0 && words[i].len() * words[j].len() > ret {
ret = words[i].len() * words[j].len();
}
}
}
ret as i32
}
}

// submission codes end

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

#[test]
fn test_318() {}
}
43 changes: 43 additions & 0 deletions src/solution/s0319_bulb_switcher.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* [319] Bulb Switcher
*
* There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it's off or turning off if it's on). For the i-th round, you toggle every i bulb. For the n-th round, you only toggle the last bulb. Find how many bulbs are on after n rounds.
*
* Example:
*
*
* Input: 3
* Output: 1
* Explanation:
* At first, the three bulbs are [off, off, off].
* After first round, the three bulbs are [on, on, on].
* After second round, the three bulbs are [on, off, on].
* After third round, the three bulbs are [on, off, off].
*
* So you should return 1, because there is only one bulb is on.
*
*
*/
pub struct Solution {}

// submission codes start here

impl Solution {
pub fn bulb_switch(n: i32) -> i32 {
// number i bulb is toggled when round divides i, e.g.
// bulb 12 is toggled in round 1, 2, 3, 4, 6, 12
// bulb 12's divisor comes in pairs, for example (2, 6), (3, 4)
// except for squres, e.g. 9 => 1, 3, 9, so it will be on
(n as f64).sqrt() as i32
}
}

// submission codes end

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

#[test]
fn test_319() {}
}
58 changes: 58 additions & 0 deletions src/solution/s0326_power_of_three.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* [326] Power of Three
*
* Given an integer, write a function to determine if it is a power of three.
*
* Example 1:
*
*
* Input: 27
* Output: true
*
*
* Example 2:
*
*
* Input: 0
* Output: false
*
* Example 3:
*
*
* Input: 9
* Output: true
*
* Example 4:
*
*
* Input: 45
* Output: false
*
* Follow up:<br />
* Could you do it without using any loop / recursion?
*/
pub struct Solution {}

// submission codes start here

impl Solution {
pub fn is_power_of_three(n: i32) -> bool {
if n == 0 {
return false;
}
if n == 1 {
return true;
}
n % 3 == 0 && Self::is_power_of_three(n / 3)
}
}

// submission codes end

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

#[test]
fn test_326() {}
}
62 changes: 62 additions & 0 deletions src/solution/s0344_reverse_string.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* [344] Reverse String
*
* Write a function that reverses a string. The input string is given as an array of characters char[].
*
* Do not allocate extra space for another array, you must do this by modifying the input array <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a> with O(1) extra memory.
*
* You may assume all the characters consist of <a href="https://en.wikipedia.org/wiki/ASCII#Printable_characters" target="_blank">printable ascii characters</a>.
*
*
*
* <div>
* Example 1:
*
*
* Input: <span id="example-input-1-1">["h","e","l","l","o"]</span>
* Output: <span id="example-output-1">["o","l","l","e","h"]</span>
*
*
* <div>
* Example 2:
*
*
* Input: <span id="example-input-2-1">["H","a","n","n","a","h"]</span>
* Output: <span id="example-output-2">["h","a","n","n","a","H"]</span>
*
* </div>
* </div>
*/
pub struct Solution {}

// submission codes start here

impl Solution {
pub fn reverse_string(s: &mut Vec<char>) {
if s.is_empty() {
return;
}
let (mut left, mut right) = (0, s.len() - 1);
while left < right {
let tmp = s[left];
s[left] = s[right];
s[right] = tmp;
left += 1;
right -= 1;
}
}
}

// submission codes end

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

#[test]
fn test_344() {
let mut s = vec![];
Solution::reverse_string(&mut s);
println!("{:?}", s);
}
}
66 changes: 66 additions & 0 deletions src/solution/s0345_reverse_vowels_of_a_string.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* [345] Reverse Vowels of a String
*
* Write a function that takes a string as input and reverse only the vowels of a string.
*
* Example 1:
*
*
* Input: <span id="example-input-1-1">"hello"</span>
* Output: <span id="example-output-1">"holle"</span>
*
*
* <div>
* Example 2:
*
*
* Input: <span id="example-input-2-1">"leetcode"</span>
* Output: <span id="example-output-2">"leotcede"</span>
* </div>
*
* Note:<br />
* The vowels does not include the letter "y".
*
*
*
*/
pub struct Solution {}

// submission codes start here

impl Solution {
pub fn reverse_vowels(s: String) -> String {
if s.is_empty() {
return s;
}
let vowels: Vec<char> = "aeiouAEIOU".chars().collect();
let mut chars: Vec<char> = s.chars().collect();
let (mut left, mut right) = (0, chars.len() - 1);
while left < right {
while !vowels.contains(&chars[left]) && left < right {
left += 1;
}
while !vowels.contains(&chars[right]) && left < right {
right -= 1;
}
if left < right {
let tmp = chars[left];
chars[left] = chars[right];
chars[right] = tmp;
left += 1;
right -= 1;
}
}
chars.into_iter().collect()
}
}

// submission codes end

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

#[test]
fn test_345() {}
}
60 changes: 60 additions & 0 deletions src/solution/s0349_intersection_of_two_arrays.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use std::hash::Hash;

/**
* [349] Intersection of Two Arrays
*
* Given two arrays, write a function to compute their intersection.
*
* Example 1:
*
*
* Input: nums1 = <span id="example-input-1-1">[1,2,2,1]</span>, nums2 = <span id="example-input-1-2">[2,2]</span>
* Output: <span id="example-output-1">[2]</span>
*
*
* <div>
* Example 2:
*
*
* Input: nums1 = <span id="example-input-2-1">[4,9,5]</span>, nums2 = <span id="example-input-2-2">[9,4,9,8,4]</span>
* Output: <span id="example-output-2">[9,4]</span>
* </div>
*
* Note:
*
*
* Each element in the result must be unique.
* The result can be in any order.
*
*
*
*
*/
pub struct Solution {}

// submission codes start here

impl Solution {
pub fn intersection(nums1: Vec<i32>, nums2: Vec<i32>) -> Vec<i32> {
use std::collections::HashSet;
let s1: HashSet<i32> = nums1.into_iter().collect();
let s2: HashSet<i32> = nums2.into_iter().collect();
let mut ret: HashSet<i32> = HashSet::new();
for num1 in s1 {
if s2.contains(&num1) {
ret.insert(num1);
}
}
ret.into_iter().collect()
}
}

// submission codes end

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

#[test]
fn test_349() {}
}
Loading