refactor: 移动已完成的题目
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
mod p0001_two_sum;
|
||||
mod p0009_palindrome_number;
|
||||
mod p0020_valid_parentheses;
|
||||
mod p2697_lexicographically_smallest_palindrome;
|
||||
mod p0002_add_two_numbers;
|
||||
mod p0003_longest_substring_without_repeating_characters;
|
||||
mod p0162_find_peak_element;
|
||||
mod p2828_check_if_a_string_is_an_acronym_of_words;
|
||||
mod p0052_n_queens_ii;
|
||||
mod p0912_sort_an_array;
|
||||
mod p1276_number_of_burgers_with_no_waste_of_ingredients;
|
||||
mod p0006_zigzag_conversion;
|
||||
mod p0007_reverse_integer;
|
||||
mod p0004_median_of_two_sorted_arrays;
|
||||
mod p0743_network_delay_time;
|
||||
mod p0447_number_of_boomerangs;
|
||||
mod p2085_count_common_words_with_one_occurrence;
|
||||
|
73
src/problem/p0001_two_sum.rs
Normal file
73
src/problem/p0001_two_sum.rs
Normal file
@@ -0,0 +1,73 @@
|
||||
/**
|
||||
* [1] Two Sum
|
||||
*
|
||||
* Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
|
||||
* You may assume that each input would have exactly one solution, and you may not use the same element twice.
|
||||
* You can return the answer in any order.
|
||||
*
|
||||
* <strong class="example">Example 1:
|
||||
*
|
||||
* Input: nums = [2,7,11,15], target = 9
|
||||
* Output: [0,1]
|
||||
* Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
|
||||
*
|
||||
* <strong class="example">Example 2:
|
||||
*
|
||||
* Input: nums = [3,2,4], target = 6
|
||||
* Output: [1,2]
|
||||
*
|
||||
* <strong class="example">Example 3:
|
||||
*
|
||||
* Input: nums = [3,3], target = 6
|
||||
* Output: [0,1]
|
||||
*
|
||||
*
|
||||
* Constraints:
|
||||
*
|
||||
* 2 <= nums.length <= 10^4
|
||||
* -10^9 <= nums[i] <= 10^9
|
||||
* -10^9 <= target <= 10^9
|
||||
* Only one valid answer exists.
|
||||
*
|
||||
*
|
||||
* Follow-up: Can you come up with an algorithm that is less than O(n^2)<font face="monospace"> </font>time complexity?
|
||||
*/
|
||||
pub struct Solution {}
|
||||
|
||||
// problem: https://leetcode.cn/problems/two-sum/
|
||||
// discuss: https://leetcode.cn/problems/two-sum/discuss/?currentPage=1&orderBy=most_votes&query=
|
||||
|
||||
// submission codes start here
|
||||
|
||||
use std::collections::HashMap;
|
||||
impl Solution {
|
||||
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
|
||||
let mut map = HashMap::with_capacity(nums.len());
|
||||
|
||||
for (index, value) in nums.iter().enumerate() {
|
||||
match map.get(&(target - value)) {
|
||||
None => {
|
||||
map.insert(value, index);
|
||||
}
|
||||
Some(target_index) => {
|
||||
return vec![*target_index as i32, index as i32];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
vec![]
|
||||
}
|
||||
}
|
||||
|
||||
// submission codes end
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_1() {
|
||||
assert_eq!(vec![0, 1], Solution::two_sum(vec![2, 7, 11, 15], 9));
|
||||
assert_eq!(vec![1, 2], Solution::two_sum(vec![3, 2, 4], 6));
|
||||
}
|
||||
}
|
122
src/problem/p0002_add_two_numbers.rs
Normal file
122
src/problem/p0002_add_two_numbers.rs
Normal file
@@ -0,0 +1,122 @@
|
||||
/**
|
||||
* [2] Add Two Numbers
|
||||
*
|
||||
* You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
|
||||
* You may assume the two numbers do not contain any leading zero, except the number 0 itself.
|
||||
*
|
||||
* <strong class="example">Example 1:
|
||||
* <img alt="" src="https://assets.leetcode.com/uploads/2020/10/02/addtwonumber1.jpg" style="width: 483px; height: 342px;" />
|
||||
* Input: l1 = [2,4,3], l2 = [5,6,4]
|
||||
* Output: [7,0,8]
|
||||
* Explanation: 342 + 465 = 807.
|
||||
*
|
||||
* <strong class="example">Example 2:
|
||||
*
|
||||
* Input: l1 = [0], l2 = [0]
|
||||
* Output: [0]
|
||||
*
|
||||
* <strong class="example">Example 3:
|
||||
*
|
||||
* Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
|
||||
* Output: [8,9,9,9,0,0,0,1]
|
||||
*
|
||||
*
|
||||
* Constraints:
|
||||
*
|
||||
* The number of nodes in each linked list is in the range [1, 100].
|
||||
* 0 <= Node.val <= 9
|
||||
* It is guaranteed that the list represents a number that does not have leading zeros.
|
||||
*
|
||||
*/
|
||||
pub struct Solution {}
|
||||
use crate::util::linked_list::{ListNode, to_list};
|
||||
|
||||
// problem: https://leetcode.cn/problems/add-two-numbers/
|
||||
// discuss: https://leetcode.cn/problems/add-two-numbers/discuss/?currentPage=1&orderBy=most_votes&query=
|
||||
|
||||
// submission codes start here
|
||||
|
||||
// Definition for singly-linked list.
|
||||
// #[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
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
impl Solution {
|
||||
pub fn add_two_numbers(l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>)
|
||||
-> Option<Box<ListNode>> {
|
||||
let (mut left, mut right) = (l1, l2);
|
||||
let mut dummy_head = Some(Box::new(ListNode::new(0)));
|
||||
let mut now = &mut dummy_head;
|
||||
let mut overflow = 0;
|
||||
|
||||
loop {
|
||||
if left.is_none() && right.is_none() && overflow == 0 {
|
||||
break
|
||||
}
|
||||
|
||||
let left_value = match left {
|
||||
None => {
|
||||
0
|
||||
}
|
||||
Some(node) => {
|
||||
left = node.next;
|
||||
node.val
|
||||
}
|
||||
};
|
||||
|
||||
let right_value = match right {
|
||||
None => {
|
||||
0
|
||||
}
|
||||
Some(node) => {
|
||||
right = node.next;
|
||||
node.val
|
||||
}
|
||||
};
|
||||
|
||||
let value = overflow + left_value + right_value;
|
||||
overflow = value / 10;
|
||||
|
||||
now.as_mut().unwrap().next = Some(Box::new(ListNode::new(value % 10)));
|
||||
now = &mut now.as_mut().unwrap().next;
|
||||
}
|
||||
|
||||
dummy_head.unwrap().next
|
||||
}
|
||||
}
|
||||
|
||||
// submission codes end
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_2() {
|
||||
assert_eq!(
|
||||
Solution::add_two_numbers(to_list(vec![2, 4, 3]), to_list(vec![5, 6, 4])),
|
||||
to_list(vec![7, 0, 8])
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
Solution::add_two_numbers(to_list(vec![9, 9, 9, 9]), to_list(vec![9, 9, 9, 9, 9, 9])),
|
||||
to_list(vec![8, 9, 9, 9, 0, 0, 1])
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
Solution::add_two_numbers(to_list(vec![0]), to_list(vec![0])),
|
||||
to_list(vec![0])
|
||||
);
|
||||
}
|
||||
}
|
@@ -0,0 +1,74 @@
|
||||
/**
|
||||
* [3] Longest Substring Without Repeating Characters
|
||||
*
|
||||
* Given a string s, find the length of the longest <span data-keyword="substring-nonempty">substring</span> without repeating characters.
|
||||
*
|
||||
* <strong class="example">Example 1:
|
||||
*
|
||||
* Input: s = "abcabcbb"
|
||||
* Output: 3
|
||||
* Explanation: The answer is "abc", with the length of 3.
|
||||
*
|
||||
* <strong class="example">Example 2:
|
||||
*
|
||||
* Input: s = "bbbbb"
|
||||
* Output: 1
|
||||
* Explanation: The answer is "b", with the length of 1.
|
||||
*
|
||||
* <strong class="example">Example 3:
|
||||
*
|
||||
* Input: s = "pwwkew"
|
||||
* Output: 3
|
||||
* Explanation: The answer is "wke", with the length of 3.
|
||||
* Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
|
||||
*
|
||||
*
|
||||
* Constraints:
|
||||
*
|
||||
* 0 <= s.length <= 5 * 10^4
|
||||
* s consists of English letters, digits, symbols and spaces.
|
||||
*
|
||||
*/
|
||||
pub struct Solution {}
|
||||
|
||||
// problem: https://leetcode.cn/problems/longest-substring-without-repeating-characters/
|
||||
// discuss: https://leetcode.cn/problems/longest-substring-without-repeating-characters/discuss/?currentPage=1&orderBy=most_votes&query=
|
||||
|
||||
// submission codes start here
|
||||
|
||||
use std::cmp::max;
|
||||
use std::collections::HashSet;
|
||||
impl Solution {
|
||||
pub fn length_of_longest_substring(s: String) -> i32 {
|
||||
let chars: Vec<char> = s.chars().collect();
|
||||
let mut window = HashSet::new();
|
||||
let mut left = 0;
|
||||
let mut result = 0;
|
||||
|
||||
for i in 0..chars.len() {
|
||||
while window.contains(&chars[i]) {
|
||||
window.remove(&chars[left]);
|
||||
left += 1;
|
||||
}
|
||||
|
||||
window.insert(chars[i]);
|
||||
result = max(result, window.len());
|
||||
}
|
||||
|
||||
result as i32
|
||||
}
|
||||
}
|
||||
|
||||
// submission codes end
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_3() {
|
||||
assert_eq!(3, Solution::length_of_longest_substring(String::from("abcabcbb")));
|
||||
assert_eq!(1, Solution::length_of_longest_substring(String::from("bbbbb")));
|
||||
assert_eq!(3, Solution::length_of_longest_substring(String::from("pwwkew")));
|
||||
}
|
||||
}
|
90
src/problem/p0004_median_of_two_sorted_arrays.rs
Normal file
90
src/problem/p0004_median_of_two_sorted_arrays.rs
Normal file
@@ -0,0 +1,90 @@
|
||||
/**
|
||||
* [4] Median of Two Sorted Arrays
|
||||
*
|
||||
* Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.
|
||||
* The overall run time complexity should be O(log (m+n)).
|
||||
*
|
||||
* <strong class="example">Example 1:
|
||||
*
|
||||
* Input: nums1 = [1,3], nums2 = [2]
|
||||
* Output: 2.00000
|
||||
* Explanation: merged array = [1,2,3] and median is 2.
|
||||
*
|
||||
* <strong class="example">Example 2:
|
||||
*
|
||||
* Input: nums1 = [1,2], nums2 = [3,4]
|
||||
* Output: 2.50000
|
||||
* Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.
|
||||
*
|
||||
*
|
||||
* Constraints:
|
||||
*
|
||||
* nums1.length == m
|
||||
* nums2.length == n
|
||||
* 0 <= m <= 1000
|
||||
* 0 <= n <= 1000
|
||||
* 1 <= m + n <= 2000
|
||||
* -10^6 <= nums1[i], nums2[i] <= 10^6
|
||||
*
|
||||
*/
|
||||
pub struct Solution {}
|
||||
|
||||
// problem: https://leetcode.cn/problems/median-of-two-sorted-arrays/
|
||||
// discuss: https://leetcode.cn/problems/median-of-two-sorted-arrays/discuss/?currentPage=1&orderBy=most_votes&query=
|
||||
|
||||
// submission codes start here
|
||||
|
||||
use std::cmp::min;
|
||||
|
||||
impl Solution {
|
||||
pub fn find_median_sorted_arrays(nums1: Vec<i32>, nums2: Vec<i32>) -> f64 {
|
||||
let len1 = nums1.len() as i32;
|
||||
let len2 = nums2.len() as i32;
|
||||
let left = (len1 + len2 + 1) / 2 ;
|
||||
let right = (len1 + len2 + 2) / 2;
|
||||
|
||||
(Solution::get_k_th(&nums1, 0, len1 - 1, &nums2, 0, len2 - 1, left) as f64 +
|
||||
Solution::get_k_th(&nums1, 0, len1 - 1, &nums2, 0, len2 - 1, right) as f64) * 0.5
|
||||
}
|
||||
|
||||
fn get_k_th(nums1: &Vec<i32>, start1: i32, end1: i32,
|
||||
nums2: &Vec<i32>, start2: i32, end2: i32, k: i32) -> i32 {
|
||||
let len1 = end1 + 1 - start1;
|
||||
let len2 = end2 + 1 - start2;
|
||||
|
||||
if len1 > len2 {
|
||||
return Solution::get_k_th(nums2, start2, end2, nums1, start1, end1, k);
|
||||
}
|
||||
|
||||
if len1 == 0 {
|
||||
return nums2[(start2 + k - 1) as usize];
|
||||
}
|
||||
|
||||
if k == 1 {
|
||||
return min(nums1[start1 as usize], nums2[start2 as usize]);
|
||||
}
|
||||
|
||||
let i = start1 + min(len1, k / 2) - 1;
|
||||
let j = start2 + min(len2, k / 2) - 1;
|
||||
|
||||
return if nums1[i as usize] > nums2[j as usize] {
|
||||
Solution::get_k_th(nums1, start1, end1, nums2, j + 1, end2, k - (j - start2 + 1))
|
||||
} else {
|
||||
Solution::get_k_th(nums1, i + 1, end1, nums2, start2, end2, k - (i - start1 + 1))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// submission codes end
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_4() {
|
||||
assert_eq!(2.0, Solution::find_median_sorted_arrays(vec![1, 3], vec![2]));
|
||||
assert_eq!(2.5, Solution::find_median_sorted_arrays(vec![1, 2], vec![3, 4]));
|
||||
assert_eq!(2.0, Solution::find_median_sorted_arrays(vec![2], vec![]));
|
||||
}
|
||||
}
|
98
src/problem/p0006_zigzag_conversion.rs
Normal file
98
src/problem/p0006_zigzag_conversion.rs
Normal file
@@ -0,0 +1,98 @@
|
||||
/**
|
||||
* [6] Zigzag Conversion
|
||||
*
|
||||
* The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
|
||||
*
|
||||
* P A H N
|
||||
* A P L S I I G
|
||||
* Y I R
|
||||
*
|
||||
* And then read line by line: "PAHNAPLSIIGYIR"
|
||||
* Write the code that will take a string and make this conversion given a number of rows:
|
||||
*
|
||||
* string convert(string s, int numRows);
|
||||
*
|
||||
*
|
||||
* <strong class="example">Example 1:
|
||||
*
|
||||
* Input: s = "PAYPALISHIRING", numRows = 3
|
||||
* Output: "PAHNAPLSIIGYIR"
|
||||
*
|
||||
* <strong class="example">Example 2:
|
||||
*
|
||||
* Input: s = "PAYPALISHIRING", numRows = 4
|
||||
* Output: "PINALSIGYAHRPI"
|
||||
* Explanation:
|
||||
* P I N
|
||||
* A L S I G
|
||||
* Y A H R
|
||||
* P I
|
||||
*
|
||||
* <strong class="example">Example 3:
|
||||
*
|
||||
* Input: s = "A", numRows = 1
|
||||
* Output: "A"
|
||||
*
|
||||
*
|
||||
* Constraints:
|
||||
*
|
||||
* 1 <= s.length <= 1000
|
||||
* s consists of English letters (lower-case and upper-case), ',' and '.'.
|
||||
* 1 <= numRows <= 1000
|
||||
*
|
||||
*/
|
||||
pub struct Solution {}
|
||||
|
||||
// problem: https://leetcode.cn/problems/zigzag-conversion/
|
||||
// discuss: https://leetcode.cn/problems/zigzag-conversion/discuss/?currentPage=1&orderBy=most_votes&query=
|
||||
|
||||
// submission codes start here
|
||||
|
||||
impl Solution {
|
||||
pub fn convert(s: String, num_rows: i32) -> String {
|
||||
let length = s.len() as i32;
|
||||
if num_rows < 2 {
|
||||
return s;
|
||||
}
|
||||
|
||||
let mut result = String::new();
|
||||
let s: Vec<char> = s.chars().collect();
|
||||
|
||||
for row in 0..num_rows {
|
||||
for begin in (0..length).step_by((2 * num_rows - 2) as usize) {
|
||||
// 竖线上的元素
|
||||
let i = begin + row;
|
||||
|
||||
if i >= length {
|
||||
break;
|
||||
}
|
||||
result.push(s[i as usize]);
|
||||
|
||||
// 斜线上的元素
|
||||
let j = i + 2 * (num_rows - row - 1);
|
||||
if j >= length {
|
||||
break;
|
||||
} else if j == i + 2 * (num_rows - 1) || i == j {
|
||||
continue;
|
||||
}
|
||||
result.push(s[j as usize]);
|
||||
}
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
// submission codes end
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_6() {
|
||||
assert_eq!(String::from("PAHNAPLSIIGYIR"), Solution::convert(
|
||||
String::from("PAYPALISHIRING"), 3
|
||||
));
|
||||
}
|
||||
}
|
66
src/problem/p0007_reverse_integer.rs
Normal file
66
src/problem/p0007_reverse_integer.rs
Normal file
@@ -0,0 +1,66 @@
|
||||
/**
|
||||
* [7] Reverse Integer
|
||||
*
|
||||
* Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-2^31, 2^31 - 1], then return 0.
|
||||
* Assume the environment does not allow you to store 64-bit integers (signed or unsigned).
|
||||
*
|
||||
* <strong class="example">Example 1:
|
||||
*
|
||||
* Input: x = 123
|
||||
* Output: 321
|
||||
*
|
||||
* <strong class="example">Example 2:
|
||||
*
|
||||
* Input: x = -123
|
||||
* Output: -321
|
||||
*
|
||||
* <strong class="example">Example 3:
|
||||
*
|
||||
* Input: x = 120
|
||||
* Output: 21
|
||||
*
|
||||
*
|
||||
* Constraints:
|
||||
*
|
||||
* -2^31 <= x <= 2^31 - 1
|
||||
*
|
||||
*/
|
||||
pub struct Solution {}
|
||||
|
||||
// problem: https://leetcode.cn/problems/reverse-integer/
|
||||
// discuss: https://leetcode.cn/problems/reverse-integer/discuss/?currentPage=1&orderBy=most_votes&query=
|
||||
|
||||
// submission codes start here
|
||||
|
||||
impl Solution {
|
||||
pub fn reverse(x: i32) -> i32 {
|
||||
let (num, negative) = if x < 0 {
|
||||
(-x, true)
|
||||
} else {
|
||||
(x, false)
|
||||
};
|
||||
|
||||
let str = num.to_string();
|
||||
let mut str: String = str.chars().rev().collect();
|
||||
if negative {
|
||||
str.insert(0, '-');
|
||||
}
|
||||
|
||||
str.parse::<i32>().unwrap_or_else(|_| 0)
|
||||
}
|
||||
}
|
||||
|
||||
// submission codes end
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_7() {
|
||||
assert_eq!(321, Solution::reverse(123));
|
||||
assert_eq!(-321, Solution::reverse(-123));
|
||||
assert_eq!(21, Solution::reverse(120));
|
||||
assert_eq!(0, Solution::reverse(0));
|
||||
}
|
||||
}
|
74
src/problem/p0009_palindrome_number.rs
Normal file
74
src/problem/p0009_palindrome_number.rs
Normal file
@@ -0,0 +1,74 @@
|
||||
/**
|
||||
* [9] Palindrome Number
|
||||
*
|
||||
* Given an integer x, return true if x is a <span data-keyword="palindrome-integer">palindrome</span>, and false otherwise.
|
||||
*
|
||||
* <strong class="example">Example 1:
|
||||
*
|
||||
* Input: x = 121
|
||||
* Output: true
|
||||
* Explanation: 121 reads as 121 from left to right and from right to left.
|
||||
*
|
||||
* <strong class="example">Example 2:
|
||||
*
|
||||
* Input: x = -121
|
||||
* Output: false
|
||||
* Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
|
||||
*
|
||||
* <strong class="example">Example 3:
|
||||
*
|
||||
* Input: x = 10
|
||||
* Output: false
|
||||
* Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
|
||||
*
|
||||
*
|
||||
* Constraints:
|
||||
*
|
||||
* -2^31 <= x <= 2^31 - 1
|
||||
*
|
||||
*
|
||||
* Follow up: Could you solve it without converting the integer to a string?
|
||||
*/
|
||||
pub struct Solution {}
|
||||
|
||||
// problem: https://leetcode.cn/problems/palindrome-number/
|
||||
// discuss: https://leetcode.cn/problems/palindrome-number/discuss/?currentPage=1&orderBy=most_votes&query=
|
||||
|
||||
// submission codes start here
|
||||
|
||||
impl Solution {
|
||||
pub fn is_palindrome(x: i32) -> bool {
|
||||
if x == 0 {
|
||||
return true
|
||||
}
|
||||
|
||||
if x < 0 || x % 10 == 0 {
|
||||
return false;
|
||||
}
|
||||
|
||||
let mut reverse_half = x % 10;
|
||||
let mut x = x / 10;
|
||||
|
||||
while x > reverse_half {
|
||||
reverse_half = reverse_half * 10 + x % 10;
|
||||
x = x / 10;
|
||||
}
|
||||
|
||||
x == reverse_half || x == reverse_half / 10
|
||||
}
|
||||
}
|
||||
|
||||
// submission codes end
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_9() {
|
||||
assert_eq!(true, Solution::is_palindrome(121));
|
||||
assert_eq!(false, Solution::is_palindrome(123));
|
||||
assert_eq!(false, Solution::is_palindrome(10));
|
||||
assert_eq!(true, Solution::is_palindrome(0));
|
||||
}
|
||||
}
|
88
src/problem/p0020_valid_parentheses.rs
Normal file
88
src/problem/p0020_valid_parentheses.rs
Normal file
@@ -0,0 +1,88 @@
|
||||
/**
|
||||
* [20] Valid Parentheses
|
||||
*
|
||||
* Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
|
||||
* An input string is valid if:
|
||||
* <ol>
|
||||
* Open brackets must be closed by the same type of brackets.
|
||||
* Open brackets must be closed in the correct order.
|
||||
* Every close bracket has a corresponding open bracket of the same type.
|
||||
* </ol>
|
||||
*
|
||||
* <strong class="example">Example 1:
|
||||
*
|
||||
* Input: s = "()"
|
||||
* Output: true
|
||||
*
|
||||
* <strong class="example">Example 2:
|
||||
*
|
||||
* Input: s = "()[]{}"
|
||||
* Output: true
|
||||
*
|
||||
* <strong class="example">Example 3:
|
||||
*
|
||||
* Input: s = "(]"
|
||||
* Output: false
|
||||
*
|
||||
*
|
||||
* Constraints:
|
||||
*
|
||||
* 1 <= s.length <= 10^4
|
||||
* s consists of parentheses only '()[]{}'.
|
||||
*
|
||||
*/
|
||||
pub struct Solution {}
|
||||
|
||||
// problem: https://leetcode.cn/problems/valid-parentheses/
|
||||
// discuss: https://leetcode.cn/problems/valid-parentheses/discuss/?currentPage=1&orderBy=most_votes&query=
|
||||
|
||||
// submission codes start here
|
||||
|
||||
impl Solution {
|
||||
pub fn is_valid(s: String) -> bool {
|
||||
let left = vec!['(', '[', '{'];
|
||||
let right = vec![')', ']', '}'];
|
||||
|
||||
let mut stack = Vec::with_capacity(s.len());
|
||||
|
||||
for c in s.chars() {
|
||||
if left.contains(&c) {
|
||||
stack.push(c)
|
||||
}
|
||||
else if right.contains(&c) {
|
||||
let target = match c {
|
||||
')' => '(',
|
||||
']' => '[',
|
||||
'}' => '{',
|
||||
_ => return false
|
||||
};
|
||||
|
||||
if stack.ends_with(&[target]) {
|
||||
stack.pop();
|
||||
}
|
||||
else {
|
||||
return false
|
||||
}
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
stack.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
// submission codes end
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_20() {
|
||||
assert_eq!(true, Solution::is_valid(String::from("()")));
|
||||
assert_eq!(true, Solution::is_valid(String::from("()[]{}")));
|
||||
assert_eq!(false, Solution::is_valid(String::from("(]")));
|
||||
assert_eq!(false, Solution::is_valid(String::from("(")));
|
||||
}
|
||||
}
|
89
src/problem/p0052_n_queens_ii.rs
Normal file
89
src/problem/p0052_n_queens_ii.rs
Normal file
@@ -0,0 +1,89 @@
|
||||
/**
|
||||
* [52] N-Queens II
|
||||
*
|
||||
* The n-queens puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other.
|
||||
* Given an integer n, return the number of distinct solutions to the n-queens puzzle.
|
||||
*
|
||||
* <strong class="example">Example 1:
|
||||
* <img alt="" src="https://assets.leetcode.com/uploads/2020/11/13/queens.jpg" style="width: 600px; height: 268px;" />
|
||||
* Input: n = 4
|
||||
* Output: 2
|
||||
* Explanation: There are two distinct solutions to the 4-queens puzzle as shown.
|
||||
*
|
||||
* <strong class="example">Example 2:
|
||||
*
|
||||
* Input: n = 1
|
||||
* Output: 1
|
||||
*
|
||||
*
|
||||
* Constraints:
|
||||
*
|
||||
* 1 <= n <= 9
|
||||
*
|
||||
*/
|
||||
pub struct Solution {}
|
||||
|
||||
// problem: https://leetcode.cn/problems/n-queens-ii/
|
||||
// discuss: https://leetcode.cn/problems/n-queens-ii/discuss/?currentPage=1&orderBy=most_votes&query=
|
||||
|
||||
// submission codes start here
|
||||
|
||||
use std::collections::VecDeque;
|
||||
|
||||
impl Solution {
|
||||
pub fn total_n_queens(n: i32) -> i32 {
|
||||
let n = n as usize;
|
||||
let mut result = 0;
|
||||
let mut stack = VecDeque::new();
|
||||
|
||||
for i in 0..n {
|
||||
stack.push_back((0usize, i, false));
|
||||
}
|
||||
|
||||
let mut x_occupied = vec![false; n];
|
||||
let mut y_occupied = vec![false; 2 * n - 1];
|
||||
let mut z_occupied = vec![false; 2 * n - 1];
|
||||
|
||||
while let Some((x, y, flag)) = stack.pop_back() {
|
||||
if flag {
|
||||
x_occupied[y] = false;
|
||||
y_occupied[n - 1 + x - y] = false;
|
||||
z_occupied[x + y] = false;
|
||||
} else {
|
||||
x_occupied[y] = true;
|
||||
y_occupied[n - 1 + x - y] = true;
|
||||
z_occupied[x + y] = true;
|
||||
|
||||
stack.push_back((x, y, true));
|
||||
|
||||
if x == n - 1 {
|
||||
result += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
for j in 0..n {
|
||||
// 注意这里在判断的点是(x + 1, j)
|
||||
if !x_occupied[j] && !y_occupied[n + x - j] && !z_occupied[x + 1 + j] {
|
||||
stack.push_back((x + 1, j, false));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
// submission codes end
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_52() {
|
||||
assert_eq!(1, Solution::total_n_queens(1));
|
||||
assert_eq!(2, Solution::total_n_queens(4));
|
||||
assert_eq!(14200, Solution::total_n_queens(12));
|
||||
}
|
||||
}
|
82
src/problem/p0162_find_peak_element.rs
Normal file
82
src/problem/p0162_find_peak_element.rs
Normal file
@@ -0,0 +1,82 @@
|
||||
/**
|
||||
* [162] Find Peak Element
|
||||
*
|
||||
* A peak element is an element that is strictly greater than its neighbors.
|
||||
* Given a 0-indexed integer array nums, find a peak element, and return its index. If the array contains multiple peaks, return the index to any of the peaks.
|
||||
* You may imagine that nums[-1] = nums[n] = -∞. In other words, an element is always considered to be strictly greater than a neighbor that is outside the array.
|
||||
* You must write an algorithm that runs in O(log n) time.
|
||||
*
|
||||
* <strong class="example">Example 1:
|
||||
*
|
||||
* Input: nums = [1,2,3,1]
|
||||
* Output: 2
|
||||
* Explanation: 3 is a peak element and your function should return the index number 2.
|
||||
* <strong class="example">Example 2:
|
||||
*
|
||||
* Input: nums = [1,2,1,3,5,6,4]
|
||||
* Output: 5
|
||||
* Explanation: Your function can return either index number 1 where the peak element is 2, or index number 5 where the peak element is 6.
|
||||
*
|
||||
* Constraints:
|
||||
*
|
||||
* 1 <= nums.length <= 1000
|
||||
* -2^31 <= nums[i] <= 2^31 - 1
|
||||
* nums[i] != nums[i + 1] for all valid i.
|
||||
*
|
||||
*/
|
||||
pub struct Solution {}
|
||||
|
||||
// problem: https://leetcode.cn/problems/find-peak-element/
|
||||
// discuss: https://leetcode.cn/problems/find-peak-element/discuss/?currentPage=1&orderBy=most_votes&query=
|
||||
|
||||
// submission codes start here
|
||||
|
||||
impl Solution {
|
||||
pub fn find_peak_element(nums: Vec<i32>) -> i32 {
|
||||
if nums.len() == 1 {
|
||||
return 0;
|
||||
}
|
||||
|
||||
let (mut left, mut right) = (0, nums.len() - 1);
|
||||
|
||||
let compare = |x| {
|
||||
if x == 0 {
|
||||
nums[0] > nums[1]
|
||||
} else if x == nums.len() - 1 {
|
||||
nums[x] > nums[x - 1]
|
||||
} else {
|
||||
nums[x - 1] < nums[x] && nums[x] > nums[x + 1]
|
||||
}
|
||||
};
|
||||
|
||||
while left <= right {
|
||||
let mid = (left + right) / 2;
|
||||
|
||||
if compare(mid) {
|
||||
return mid as i32
|
||||
}
|
||||
|
||||
if nums[mid] < nums[mid + 1] {
|
||||
left += 1;
|
||||
} else {
|
||||
right -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
-1
|
||||
}
|
||||
}
|
||||
|
||||
// submission codes end
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_162() {
|
||||
assert_eq!(2, Solution::find_peak_element(vec![1,2,3,1]));
|
||||
assert_eq!(5, Solution::find_peak_element(vec![1,2,1,3,5,6,4]));
|
||||
assert_eq!(1, Solution::find_peak_element(vec![1,2]));
|
||||
}
|
||||
}
|
78
src/problem/p0447_number_of_boomerangs.rs
Normal file
78
src/problem/p0447_number_of_boomerangs.rs
Normal file
@@ -0,0 +1,78 @@
|
||||
/**
|
||||
* [447] Number of Boomerangs
|
||||
*
|
||||
* You are given n points in the plane that are all distinct, where points[i] = [xi, yi]. A boomerang is a tuple of points (i, j, k) such that the distance between i and j equals the distance between i and k (the order of the tuple matters).
|
||||
* Return the number of boomerangs.
|
||||
*
|
||||
* <strong class="example">Example 1:
|
||||
*
|
||||
* Input: points = [[0,0],[1,0],[2,0]]
|
||||
* Output: 2
|
||||
* Explanation: The two boomerangs are [[1,0],[0,0],[2,0]] and [[1,0],[2,0],[0,0]].
|
||||
*
|
||||
* <strong class="example">Example 2:
|
||||
*
|
||||
* Input: points = [[1,1],[2,2],[3,3]]
|
||||
* Output: 2
|
||||
*
|
||||
* <strong class="example">Example 3:
|
||||
*
|
||||
* Input: points = [[1,1]]
|
||||
* Output: 0
|
||||
*
|
||||
*
|
||||
* Constraints:
|
||||
*
|
||||
* n == points.length
|
||||
* 1 <= n <= 500
|
||||
* points[i].length == 2
|
||||
* -10^4 <= xi, yi <= 10^4
|
||||
* All the points are unique.
|
||||
*
|
||||
*/
|
||||
pub struct Solution {}
|
||||
|
||||
// problem: https://leetcode.cn/problems/number-of-boomerangs/
|
||||
// discuss: https://leetcode.cn/problems/number-of-boomerangs/discuss/?currentPage=1&orderBy=most_votes&query=
|
||||
|
||||
// submission codes start here
|
||||
|
||||
use std::collections::HashMap;
|
||||
impl Solution {
|
||||
pub fn number_of_boomerangs(points: Vec<Vec<i32>>) -> i32 {
|
||||
let mut result = 0;
|
||||
|
||||
for p in &points {
|
||||
let mut values = HashMap::new();
|
||||
|
||||
for q in &points {
|
||||
let dis = (p[0] - q[0]).pow(2) + (p[1] - q[1]).pow(2);
|
||||
|
||||
let c = values.entry(dis).or_insert(0);
|
||||
*c += 1;
|
||||
}
|
||||
|
||||
for (_, v) in &values {
|
||||
// 实际上并不需要,因为最小值为1
|
||||
// 计算的结果为0
|
||||
if *v >= 2 {
|
||||
// result += A_m^2
|
||||
result += (*v) * (*v - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
// submission codes end
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_447() {
|
||||
}
|
||||
}
|
129
src/problem/p0743_network_delay_time.rs
Normal file
129
src/problem/p0743_network_delay_time.rs
Normal file
@@ -0,0 +1,129 @@
|
||||
/**
|
||||
* [743] Network Delay Time
|
||||
*
|
||||
* You are given a network of n nodes, labeled from 1 to n. You are also given times, a list of travel times as directed edges times[i] = (ui, vi, wi), where ui is the source node, vi is the target node, and wi is the time it takes for a signal to travel from source to target.
|
||||
* We will send a signal from a given node k. Return the minimum time it takes for all the n nodes to receive the signal. If it is impossible for all the n nodes to receive the signal, return -1.
|
||||
*
|
||||
* <strong class="example">Example 1:
|
||||
* <img alt="" src="https://assets.leetcode.com/uploads/2019/05/23/931_example_1.png" style="width: 217px; height: 239px;" />
|
||||
* Input: times = [[2,1,1],[2,3,1],[3,4,1]], n = 4, k = 2
|
||||
* Output: 2
|
||||
*
|
||||
* <strong class="example">Example 2:
|
||||
*
|
||||
* Input: times = [[1,2,1]], n = 2, k = 1
|
||||
* Output: 1
|
||||
*
|
||||
* <strong class="example">Example 3:
|
||||
*
|
||||
* Input: times = [[1,2,1]], n = 2, k = 2
|
||||
* Output: -1
|
||||
*
|
||||
*
|
||||
* Constraints:
|
||||
*
|
||||
* 1 <= k <= n <= 100
|
||||
* 1 <= times.length <= 6000
|
||||
* times[i].length == 3
|
||||
* 1 <= ui, vi <= n
|
||||
* ui != vi
|
||||
* 0 <= wi <= 100
|
||||
* All the pairs (ui, vi) are unique. (i.e., no multiple edges.)
|
||||
*
|
||||
*/
|
||||
pub struct Solution {}
|
||||
|
||||
// problem: https://leetcode.cn/problems/network-delay-time/
|
||||
// discuss: https://leetcode.cn/problems/network-delay-time/discuss/?currentPage=1&orderBy=most_votes&query=
|
||||
|
||||
// submission codes start here
|
||||
|
||||
use std::cmp::{max, min};
|
||||
use std::collections::HashSet;
|
||||
|
||||
impl Solution {
|
||||
pub fn network_delay_time(times: Vec<Vec<i32>>, n: i32, k: i32) -> i32 {
|
||||
let n = n as usize;
|
||||
let k = k as usize;
|
||||
|
||||
let mut matrix = Vec::with_capacity(n + 1);
|
||||
|
||||
for _i in 0..=n {
|
||||
let mut line = Vec::with_capacity(n + 1);
|
||||
line.resize(n + 1, i32::MAX);
|
||||
matrix.push(line);
|
||||
}
|
||||
|
||||
for line in × {
|
||||
matrix[line[0] as usize][line[1] as usize] = line[2];
|
||||
}
|
||||
|
||||
let mut path = HashSet::with_capacity(n + 1);
|
||||
path.insert(k);
|
||||
let mut distances = matrix[k].clone();
|
||||
|
||||
for _ in 2..=n {
|
||||
let (mut index, mut value) = (0usize, i32::MAX);
|
||||
|
||||
for (i, d) in distances.iter().enumerate() {
|
||||
if path.contains(&i) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if value > *d {
|
||||
value = *d;
|
||||
index = i;
|
||||
}
|
||||
}
|
||||
|
||||
if value == i32::MAX {
|
||||
break;
|
||||
}
|
||||
|
||||
path.insert(index);
|
||||
|
||||
for i in 1..=n {
|
||||
if path.contains(&i) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if matrix[index][i] == i32::MAX {
|
||||
continue;
|
||||
}
|
||||
|
||||
distances[i] = min(distances[i], value + matrix[index][i]);
|
||||
}
|
||||
}
|
||||
|
||||
let mut result = i32::MIN;
|
||||
|
||||
for i in 1..distances.len() {
|
||||
if i == k {
|
||||
continue
|
||||
}
|
||||
|
||||
result = max(result, distances[i]);
|
||||
}
|
||||
|
||||
return if result == i32::MAX {
|
||||
-1
|
||||
} else {
|
||||
result
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// submission codes end
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_743() {
|
||||
assert_eq!(Solution::network_delay_time(
|
||||
vec![vec![2,1,1], vec![2,3,1], vec![3,4,1]], 4, 2), 2);
|
||||
assert_eq!(Solution::network_delay_time(vec![vec![1,2,1]], 2, 1), 1);
|
||||
assert_eq!(Solution::network_delay_time(vec![vec![1,2,1]], 2, 2), -1);
|
||||
}
|
||||
}
|
95
src/problem/p0912_sort_an_array.rs
Normal file
95
src/problem/p0912_sort_an_array.rs
Normal file
@@ -0,0 +1,95 @@
|
||||
/**
|
||||
* [912] Sort an Array
|
||||
*
|
||||
* Given an array of integers nums, sort the array in ascending order and return it.
|
||||
* You must solve the problem without using any built-in functions in O(nlog(n)) time complexity and with the smallest space complexity possible.
|
||||
*
|
||||
* <strong class="example">Example 1:
|
||||
*
|
||||
* Input: nums = [5,2,3,1]
|
||||
* Output: [1,2,3,5]
|
||||
* Explanation: After sorting the array, the positions of some numbers are not changed (for example, 2 and 3), while the positions of other numbers are changed (for example, 1 and 5).
|
||||
*
|
||||
* <strong class="example">Example 2:
|
||||
*
|
||||
* Input: nums = [5,1,1,2,0,0]
|
||||
* Output: [0,0,1,1,2,5]
|
||||
* Explanation: Note that the values of nums are not necessairly unique.
|
||||
*
|
||||
*
|
||||
* Constraints:
|
||||
*
|
||||
* 1 <= nums.length <= 5 * 10^4
|
||||
* -5 * 10^4 <= nums[i] <= 5 * 10^4
|
||||
*
|
||||
*/
|
||||
pub struct Solution {}
|
||||
|
||||
// problem: https://leetcode.cn/problems/sort-an-array/
|
||||
// discuss: https://leetcode.cn/problems/sort-an-array/discuss/?currentPage=1&orderBy=most_votes&query=
|
||||
|
||||
// submission codes start here
|
||||
|
||||
impl Solution {
|
||||
pub fn sort_array(nums: Vec<i32>) -> Vec<i32> {
|
||||
let mut nums = nums;
|
||||
let mut temp = Vec::with_capacity(nums.len());
|
||||
temp.resize(nums.len(), 0);
|
||||
let (left, right) = (0, nums.len() - 1);
|
||||
|
||||
Solution::merge_sort(&mut nums, &mut temp, left, right);
|
||||
nums
|
||||
}
|
||||
|
||||
fn merge_sort(nums: &mut Vec<i32>, temp: &mut Vec<i32>, left: usize, right: usize) {
|
||||
if left >= right {
|
||||
return;
|
||||
}
|
||||
|
||||
let mid = (left + right) / 2;
|
||||
Solution::merge_sort(nums, temp, left, mid);
|
||||
Solution::merge_sort(nums, temp, mid + 1, right);
|
||||
|
||||
let (mut l, mut r, mut pos) = (left, mid + 1, left);
|
||||
|
||||
while l <= mid && r <= right {
|
||||
if nums[l] <= nums[r] {
|
||||
temp[pos] = nums[l];
|
||||
l += 1;
|
||||
} else {
|
||||
temp[pos] = nums[r];
|
||||
r += 1;
|
||||
}
|
||||
pos += 1;
|
||||
}
|
||||
|
||||
while l <= mid {
|
||||
temp[pos] = nums[l];
|
||||
pos += 1;
|
||||
l += 1;
|
||||
}
|
||||
|
||||
while r <= right {
|
||||
temp[pos] = nums[r];
|
||||
pos += 1;
|
||||
r += 1;
|
||||
}
|
||||
|
||||
for i in left..=right {
|
||||
nums[i] = temp[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// submission codes end
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_912() {
|
||||
assert_eq!(vec![1, 2, 3, 5], Solution::sort_array(vec![5, 2, 3, 1]));
|
||||
assert_eq!(vec![0, 0, 1, 1, 2, 5], Solution::sort_array(vec![5, 1, 1, 2, 0, 0]));
|
||||
}
|
||||
}
|
@@ -0,0 +1,91 @@
|
||||
/**
|
||||
* [1276] Number of Burgers with No Waste of Ingredients
|
||||
*
|
||||
* Given two integers tomatoSlices and cheeseSlices. The ingredients of different burgers are as follows:
|
||||
*
|
||||
* Jumbo Burger: 4 tomato slices and 1 cheese slice.
|
||||
* Small Burger: 2 Tomato slices and 1 cheese slice.
|
||||
*
|
||||
* Return [total_jumbo, total_small] so that the number of remaining tomatoSlices equal to 0 and the number of remaining cheeseSlices equal to 0. If it is not possible to make the remaining tomatoSlices and cheeseSlices equal to 0 return [].
|
||||
*
|
||||
* <strong class="example">Example 1:
|
||||
*
|
||||
* Input: tomatoSlices = 16, cheeseSlices = 7
|
||||
* Output: [1,6]
|
||||
* Explantion: To make one jumbo burger and 6 small burgers we need 4*1 + 2*6 = 16 tomato and 1 + 6 = 7 cheese.
|
||||
* There will be no remaining ingredients.
|
||||
*
|
||||
* <strong class="example">Example 2:
|
||||
*
|
||||
* Input: tomatoSlices = 17, cheeseSlices = 4
|
||||
* Output: []
|
||||
* Explantion: There will be no way to use all ingredients to make small and jumbo burgers.
|
||||
*
|
||||
* <strong class="example">Example 3:
|
||||
*
|
||||
* Input: tomatoSlices = 4, cheeseSlices = 17
|
||||
* Output: []
|
||||
* Explantion: Making 1 jumbo burger there will be 16 cheese remaining and making 2 small burgers there will be 15 cheese remaining.
|
||||
*
|
||||
*
|
||||
* Constraints:
|
||||
*
|
||||
* 0 <= tomatoSlices, cheeseSlices <= 10^7
|
||||
*
|
||||
*/
|
||||
pub struct Solution {}
|
||||
|
||||
// problem: https://leetcode.cn/problems/number-of-burgers-with-no-waste-of-ingredients/
|
||||
// discuss: https://leetcode.cn/problems/number-of-burgers-with-no-waste-of-ingredients/discuss/?currentPage=1&orderBy=most_votes&query=
|
||||
|
||||
// submission codes start here
|
||||
|
||||
impl Solution {
|
||||
pub fn num_of_burgers(tomato_slices: i32, cheese_slices: i32) -> Vec<i32> {
|
||||
let (mut tomato, mut cheese) = (tomato_slices as f32,
|
||||
cheese_slices as f32);
|
||||
if tomato == 0f32 && cheese == 0f32 {
|
||||
return vec![0, 0]
|
||||
}
|
||||
|
||||
if tomato == 0f32 ||
|
||||
tomato / cheese < 2f32 || tomato / cheese > 4f32 {
|
||||
return vec![]
|
||||
}
|
||||
|
||||
let mut big_count = 0;
|
||||
|
||||
while tomato / cheese != 2f32 {
|
||||
tomato -= 4f32;
|
||||
cheese -= 1f32;
|
||||
big_count += 1;
|
||||
|
||||
if tomato == 0f32 && cheese == 0f32 {
|
||||
break;
|
||||
}
|
||||
|
||||
if tomato * cheese == 0f32 {
|
||||
return vec![]
|
||||
}
|
||||
}
|
||||
|
||||
vec![big_count, cheese as i32]
|
||||
}
|
||||
}
|
||||
|
||||
// submission codes end
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_1276() {
|
||||
let empty_array : Vec<i32> = Vec::new();
|
||||
assert_eq!(vec![1, 6], Solution::num_of_burgers(16, 7));
|
||||
assert_eq!(empty_array, Solution::num_of_burgers(17, 4));
|
||||
assert_eq!(empty_array, Solution::num_of_burgers(4, 17));
|
||||
assert_eq!(vec![0, 0], Solution::num_of_burgers(0, 0));
|
||||
assert_eq!(vec![0, 1], Solution::num_of_burgers(2, 1));
|
||||
}
|
||||
}
|
86
src/problem/p2085_count_common_words_with_one_occurrence.rs
Normal file
86
src/problem/p2085_count_common_words_with_one_occurrence.rs
Normal file
@@ -0,0 +1,86 @@
|
||||
/**
|
||||
* [2085] Count Common Words With One Occurrence
|
||||
*
|
||||
* Given two string arrays words1 and words2, return the number of strings that appear exactly once in each of the two arrays.
|
||||
*
|
||||
* <strong class="example">Example 1:
|
||||
*
|
||||
* Input: words1 = ["leetcode","is","amazing","as","is"], words2 = ["amazing","leetcode","is"]
|
||||
* Output: 2
|
||||
* Explanation:
|
||||
* - "leetcode" appears exactly once in each of the two arrays. We count this string.
|
||||
* - "amazing" appears exactly once in each of the two arrays. We count this string.
|
||||
* - "is" appears in each of the two arrays, but there are 2 occurrences of it in words1. We do not count this string.
|
||||
* - "as" appears once in words1, but does not appear in words2. We do not count this string.
|
||||
* Thus, there are 2 strings that appear exactly once in each of the two arrays.
|
||||
*
|
||||
* <strong class="example">Example 2:
|
||||
*
|
||||
* Input: words1 = ["b","bb","bbb"], words2 = ["a","aa","aaa"]
|
||||
* Output: 0
|
||||
* Explanation: There are no strings that appear in each of the two arrays.
|
||||
*
|
||||
* <strong class="example">Example 3:
|
||||
*
|
||||
* Input: words1 = ["a","ab"], words2 = ["a","a","a","ab"]
|
||||
* Output: 1
|
||||
* Explanation: The only string that appears exactly once in each of the two arrays is "ab".
|
||||
*
|
||||
*
|
||||
* Constraints:
|
||||
*
|
||||
* 1 <= words1.length, words2.length <= 1000
|
||||
* 1 <= words1[i].length, words2[j].length <= 30
|
||||
* words1[i] and words2[j] consists only of lowercase English letters.
|
||||
*
|
||||
*/
|
||||
pub struct Solution {}
|
||||
|
||||
// problem: https://leetcode.cn/problems/count-common-words-with-one-occurrence/
|
||||
// discuss: https://leetcode.cn/problems/count-common-words-with-one-occurrence/discuss/?currentPage=1&orderBy=most_votes&query=
|
||||
|
||||
// submission codes start here
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
impl Solution {
|
||||
pub fn count_words(words1: Vec<String>, words2: Vec<String>) -> i32 {
|
||||
let mut dict = HashMap::new();
|
||||
|
||||
for i in &words1 {
|
||||
match dict.get(i) {
|
||||
None => {
|
||||
dict.insert(i, 0);}
|
||||
Some(_) => {
|
||||
dict.insert(i ,1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for i in &words2 {
|
||||
match dict.get(i) {
|
||||
None => {}
|
||||
Some(value) => {
|
||||
if *value == 0 {
|
||||
dict.insert(i, 2);
|
||||
} else if *value == 2{
|
||||
dict.insert(i, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dict.values().filter(|x| {**x == 2}).count() as i32
|
||||
}
|
||||
}
|
||||
|
||||
// submission codes end
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_2085() {
|
||||
}
|
||||
}
|
79
src/problem/p2697_lexicographically_smallest_palindrome.rs
Normal file
79
src/problem/p2697_lexicographically_smallest_palindrome.rs
Normal file
@@ -0,0 +1,79 @@
|
||||
/**
|
||||
* [2697] Lexicographically Smallest Palindrome
|
||||
*
|
||||
* You are given a string <code node="[object Object]">s consisting of lowercase English letters, and you are allowed to perform operations on it. In one operation, you can replace a character in <code node="[object Object]">s with another lowercase English letter.
|
||||
* Your task is to make <code node="[object Object]">s a palindrome with the minimum number of operations possible. If there are multiple palindromes that can be <meta charset="utf-8" />made using the minimum number of operations, <meta charset="utf-8" />make the lexicographically smallest one.
|
||||
* A string a is lexicographically smaller than a string b (of the same length) if in the first position where a and b differ, string a has a letter that appears earlier in the alphabet than the corresponding letter in b.
|
||||
* Return the resulting palindrome string.
|
||||
*
|
||||
* <strong class="example">Example 1:
|
||||
*
|
||||
* Input: s = "egcfe"
|
||||
* Output: "efcfe"
|
||||
* Explanation: The minimum number of operations to make "egcfe" a palindrome is 1, and the lexicographically smallest palindrome string we can get by modifying one character is "efcfe", by changing 'g'.
|
||||
*
|
||||
* <strong class="example">Example 2:
|
||||
*
|
||||
* Input: s = "abcd"
|
||||
* Output: "abba"
|
||||
* Explanation: The minimum number of operations to make "abcd" a palindrome is 2, and the lexicographically smallest palindrome string we can get by modifying two characters is "abba".
|
||||
*
|
||||
* <strong class="example">Example 3:
|
||||
*
|
||||
* Input: s = "seven"
|
||||
* Output: "neven"
|
||||
* Explanation: The minimum number of operations to make "seven" a palindrome is 1, and the lexicographically smallest palindrome string we can get by modifying one character is "neven".
|
||||
*
|
||||
*
|
||||
* Constraints:
|
||||
*
|
||||
* 1 <= s.length <= 1000
|
||||
* s consists of only lowercase English letters.
|
||||
*
|
||||
*/
|
||||
pub struct Solution {}
|
||||
|
||||
// problem: https://leetcode.cn/problems/lexicographically-smallest-palindrome/
|
||||
// discuss: https://leetcode.cn/problems/lexicographically-smallest-palindrome/discuss/?currentPage=1&orderBy=most_votes&query=
|
||||
|
||||
// submission codes start here
|
||||
|
||||
use std::cmp::min;
|
||||
impl Solution {
|
||||
pub fn make_smallest_palindrome(s: String) -> String {
|
||||
let mut result : Vec<u8> = s.bytes().collect();
|
||||
let mut left = 0;
|
||||
let mut right = result.len() - 1;
|
||||
|
||||
while left < right {
|
||||
if result[left] != result[right] {
|
||||
let value = min(result[left], result[right]);
|
||||
|
||||
result[left] = value;
|
||||
result[right] = value;
|
||||
}
|
||||
|
||||
left = left + 1;
|
||||
right = right - 1;
|
||||
}
|
||||
|
||||
String::from_utf8(result).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
// submission codes end
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_2697() {
|
||||
assert_eq!(String::from("efcfe"),
|
||||
Solution::make_smallest_palindrome(String::from("egcfe")));
|
||||
assert_eq!(String::from("abba"),
|
||||
Solution::make_smallest_palindrome(String::from("abcd")));
|
||||
assert_eq!(String::from("neven"),
|
||||
Solution::make_smallest_palindrome(String::from("seven")));
|
||||
}
|
||||
}
|
@@ -0,0 +1,78 @@
|
||||
/**
|
||||
* [2828] Check if a String Is an Acronym of Words
|
||||
*
|
||||
* Given an array of strings words and a string s, determine if s is an acronym of words.
|
||||
* The string s is considered an acronym of words if it can be formed by concatenating the first character of each string in words in order. For example, "ab" can be formed from ["apple", "banana"], but it can't be formed from ["bear", "aardvark"].
|
||||
* Return true if s is an acronym of words, and false otherwise.
|
||||
*
|
||||
* <strong class="example">Example 1:
|
||||
*
|
||||
* Input: words = ["alice","bob","charlie"], s = "abc"
|
||||
* Output: true
|
||||
* Explanation: The first character in the words "alice", "bob", and "charlie" are 'a', 'b', and 'c', respectively. Hence, s = "abc" is the acronym.
|
||||
*
|
||||
* <strong class="example">Example 2:
|
||||
*
|
||||
* Input: words = ["an","apple"], s = "a"
|
||||
* Output: false
|
||||
* Explanation: The first character in the words "an" and "apple" are 'a' and 'a', respectively.
|
||||
* The acronym formed by concatenating these characters is "aa".
|
||||
* Hence, s = "a" is not the acronym.
|
||||
*
|
||||
* <strong class="example">Example 3:
|
||||
*
|
||||
* Input: words = ["never","gonna","give","up","on","you"], s = "ngguoy"
|
||||
* Output: true
|
||||
* Explanation: By concatenating the first character of the words in the array, we get the string "ngguoy".
|
||||
* Hence, s = "ngguoy" is the acronym.
|
||||
*
|
||||
*
|
||||
* Constraints:
|
||||
*
|
||||
* 1 <= words.length <= 100
|
||||
* 1 <= words[i].length <= 10
|
||||
* 1 <= s.length <= 100
|
||||
* words[i] and s consist of lowercase English letters.
|
||||
*
|
||||
*/
|
||||
pub struct Solution {}
|
||||
|
||||
// problem: https://leetcode.cn/problems/check-if-a-string-is-an-acronym-of-words/
|
||||
// discuss: https://leetcode.cn/problems/check-if-a-string-is-an-acronym-of-words/discuss/?currentPage=1&orderBy=most_votes&query=
|
||||
|
||||
// submission codes start here
|
||||
|
||||
impl Solution {
|
||||
pub fn is_acronym(words: Vec<String>, s: String) -> bool {
|
||||
if words.len() != s.len() {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (index, value) in s.chars().enumerate() {
|
||||
if !words[index].starts_with(value) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
// submission codes end
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_2828() {
|
||||
assert_eq!(true, Solution::is_acronym(vec![
|
||||
String::from("alice"),
|
||||
String::from("bob"),
|
||||
String::from("charlie")], String::from("abc")));
|
||||
|
||||
assert_eq!(false, Solution::is_acronym(vec![
|
||||
String::from("an"),
|
||||
String::from("apple")], String::from("a")));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user