1447.最简分数 删除todo

This commit is contained in:
游由 2022-02-10 17:12:04 +08:00
parent 5df97bae16
commit 0d485bb0e9
3 changed files with 67 additions and 31 deletions

View File

@ -105,6 +105,7 @@ mod q1190;
mod q1218;
mod q1269;
mod q1310;
mod q1447;
mod q1473;
mod q1482;
mod q1486;

View File

@ -0,0 +1,66 @@
use crate::Solution;
impl Solution {
/// [1447.最简分数](https://leetcode-cn.com/problems/simplified-fractions/)
///
/// 2022-02-10 14:35:49
///
/// 给你一个整数 `n` ,请你返回所有 0 到 1 之间(不包括 0 和 1满足分母小于等于 `n` 的 **最简** 分数 。分数可以以 **任意** 顺序返回。
///
/// + **示例 1**
/// + **输入:** n = 2
/// + **输出:** \["1/2"\]
/// + **解释:** "1/2" 是唯一一个分母小于等于 2 的最简分数。
/// + **示例 2**
/// + **输入:** n = 3
/// + **输出:** \["1/2","1/3","2/3"\]
/// + **示例 3**
/// + **输入:** n = 4
/// + **输出:** \["1/2","1/3","1/4","2/3","3/4"\]
/// + **解释:** "2/4" 不是最简分数,因为它可以化简为 "1/2" 。
/// + **示例 4**
/// + **输入:** n = 1
/// + **输出:** \[\]
/// + **提示:**
/// * `1 <= n <= 100`
/// + Related Topics
/// * 数学
/// * 字符串
/// * 数论
/// * 👍 60
/// * 👎 0
pub fn simplified_fractions(n: i32) -> Vec<String> {
fn gcd(a: i32, b: i32) -> i32 {
if b == 0 { a } else { gcd(b, a % b) }
}
/*fn gcd(mut a:i32, mut b: i32) -> i32 {
while b != 0 {
(a, b) = (b, a % b);
}
a
}*/
(2..=n).map(|i| (1..i).filter(move |&j| gcd(j, i) == 1).map(move |j| format!("{}/{}", j, i))).flatten().collect()
/*let mut ans = Vec::<String>::new();
for i in 2..=n {
for j in 1..i {
if gcd(i, j) == 1 {
ans.push(format!("{}/{}", j, i));
}
}
}
ans*/
}
}
#[cfg(test)]
mod test {
use crate::Solution;
#[test]
fn test_q1447() {
let ans = vec!["1/2".to_string(), "1/3".to_string(), "2/3".to_string()];
assert_eq!(Solution::simplified_fractions(3), ans);
}
}

View File

@ -205,7 +205,6 @@ impl Solution {
/// 224.基本计算器
///
/// [原题链接](https://leetcode-cn.com/problems/basic-calculator/)
/// todo
pub fn calculate(s: String) -> i32 {
let mut stack = Vec::<i32>::new();
let mut operand = 0;
@ -428,7 +427,6 @@ impl Solution {
/// [原题链接](https://leetcode-cn.com/problems/magic-index-lcci/)
///
/// 循环遍历
/// todo:二分查找加DFS
pub fn find_magic_index(nums: Vec<i32>) -> i32 {
let mut idx = -1;
for i in nums.iter().enumerate() {
@ -605,7 +603,6 @@ impl Solution {
/// 51.N皇后
///
/// [原题链接](https://leetcode-cn.com/problems/n-queens/)
/// todo
pub fn solve_n_queens(n: i32) -> Vec<Vec<String>> {
fn generate_board(queens: &Vec<usize>, n: usize) -> Vec<String> {
let mut board = Vec::<String>::with_capacity(n);
@ -639,7 +636,6 @@ impl Solution {
/// 6.Z 字形变换
///
/// [原题链接](https://leetcode-cn.com/problems/zigzag-conversion/)
/// todo
pub fn convert(s: String, num_rows: i32) -> String {
if num_rows <= 1 {
return s;
@ -689,7 +685,6 @@ impl Solution {
/// 188.买卖股票的最佳时机 IV
///
/// [原题链接](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iv/)
/// todo
pub fn max_profit_iv(mut k: i32, prices: Vec<i32>) -> i32 {
/*if prices.is_empty() || k < 1 {
return 0;
@ -726,7 +721,6 @@ impl Solution {
/// 123.买卖股票的最佳时机 III
///
/// [原题链接](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iii/)
/// todo
pub fn max_profit_iii(prices: Vec<i32>) -> i32 {
if prices.is_empty() {
return 0;
@ -1063,9 +1057,6 @@ impl Solution {
/// }
/// ```
/// # 方法2 弗洛伊德算法
/// todo 解析
/// ## 复杂度分析
/// todo
/// ## 源码
/// ```rust
/// pub fn calc_equation(equations: Vec<Vec<String>>, values: Vec<f64>, queries: Vec<Vec<String>>) -> Vec<f64> {
@ -1112,9 +1103,6 @@ impl Solution {
/// }
/// ```
/// # 方法3 加权并查集
/// todo
/// ## 复杂度分析
/// todo
/// ## 源码
/// 源码见实现
pub fn calc_equation(equations: Vec<Vec<String>>, values: Vec<f64>, queries: Vec<Vec<String>>) -> Vec<f64> {
@ -1297,7 +1285,6 @@ impl Solution {
/// 86.分隔链表
///
/// [原题链接](https://leetcode-cn.com/problems/partition-list/)
/// todo
pub fn partition(mut head: Option<Box<ListNode>>, x: i32) -> Option<Box<ListNode>> {
let mut less_than_x_head = None;
let mut less_than_x = &mut less_than_x_head;
@ -1349,7 +1336,6 @@ impl Solution {
/// ans
/// }
/// ```
/// todo
pub fn max_sliding_window(nums: Vec<i32>, k: i32) -> Vec<i32> {
unimplemented!()
}
@ -1357,7 +1343,6 @@ impl Solution {
/// 605.种花问题
///
/// [原题链接](https://leetcode-cn.com/problems/can-place-flowers/)
/// todo
pub fn can_place_flowers(flowerbed: Vec<i32>, n: i32) -> bool {
let mut count = 0i32;
let m = flowerbed.len();
@ -1411,12 +1396,10 @@ impl Solution {
///
/// [原题链接](https://leetcode-cn.com/problems/smallest-string-with-swaps/)
/// # 方法1 并查集 优先队列
/// todo
///
/// # 方法2 DFS
///
/// ## 源码
/// todo
pub fn smallest_string_with_swaps(s: String, pairs: Vec<Vec<i32>>) -> String {
if pairs.is_empty() {
return s;
@ -1483,7 +1466,6 @@ impl Solution {
///
/// [原题链接](https://leetcode-cn.com/problems/sort-items-by-groups-respecting-dependencies/)
/// # 方法1 拓扑排序
/// todo
pub fn sort_items(n: i32, m: i32, group: Vec<i32>, before_items: Vec<Vec<i32>>) -> Vec<i32> {
fn top_sort(deg: &mut Vec<usize>, graph: &mut Vec<Vec<usize>>, items: &mut Vec<usize>) -> Vec<usize> {
let mut q = std::collections::VecDeque::<usize>::new();
@ -1759,7 +1741,6 @@ impl Solution {
/// 172.阶乘后的零
///
/// [原题链接](https://leetcode-cn.com/problems/factorial-trailing-zeroes/)
/// todo
pub fn trailing_zeroes(mut n: i32) -> i32 {
let mut zero_count = 0;
while n > 0 {
@ -1774,7 +1755,6 @@ impl Solution {
/// [原题链接](https://leetcode-cn.com/problems/redundant-connection/)
///
/// # 方法1 并查集
/// todo
pub fn find_redundant_connection(edges: Vec<Vec<i32>>) -> Vec<i32> {
fn find(parent: &mut Vec<usize>, index: usize) -> usize {
if parent[index] != index {
@ -1887,7 +1867,6 @@ impl Solution {
/// }
/// ```
/// # 方法2 位运算除法
/// todo
pub fn divide(dividend: i32, divisor: i32) -> i32 {
if divisor == 0 {
return 0;
@ -1915,10 +1894,8 @@ impl Solution {
///
/// [原题链接](https://leetcode-cn.com/problems/most-stones-removed-with-same-row-or-column/)
/// # 方法1 并查集
/// todo
/// 实现见源码
/// # 方法2 dfs
/// todo
pub fn remove_stones(stones: Vec<Vec<i32>>) -> i32 {
use std::collections::HashMap;
let mut parent = HashMap::<usize, usize>::new();
@ -2044,7 +2021,6 @@ impl Solution {
/// # 方法3 二分查找
/// # 方法4 二进制
/// # 方法5 快慢指针
/// todo
pub fn find_duplicate(nums: Vec<i32>) -> i32 {
let mut num_look = vec![0; nums.len()];
let mut idx = 0usize;
@ -2062,7 +2038,6 @@ impl Solution {
///
/// [原题链接](https://leetcode-cn.com/problems/find-all-numbers-disappeared-in-an-array/)
/// # 方法1 哈希表
/// todo 略
/// # 方法2 原地修改
/// 遍历数组,将数组元素中对应的索引位置的元素取反,二次遍历时大于零的元素没有被修改过,即缺少索引对应的元素。实现见源码。
pub fn find_disappeared_numbers(mut nums: Vec<i32>) -> Vec<i32> {
@ -2086,7 +2061,6 @@ impl Solution {
///
/// [原题链接](https://leetcode-cn.com/problems/bricks-falling-when-hit/)
/// # 方法1 并查集
/// todo
pub fn hit_bricks(grid: Vec<Vec<i32>>, hits: Vec<Vec<i32>>) -> Vec<i32> {
const DIRECTIONS: [[i32; 2]; 4] = [[0, 1], [1, 0], [-1, 0], [0, -1]];
let rows = grid.len();
@ -2203,7 +2177,6 @@ impl Solution {
///
/// [原题链接](https://leetcode-cn.com/problems/accounts-merge/)
/// # 方法1 并查集
/// todo
pub fn accounts_merge(accounts: Vec<Vec<String>>) -> Vec<Vec<String>> {
fn find(parent: &mut Vec<usize>, index: usize) -> usize {
if parent[index] != index {
@ -2265,7 +2238,6 @@ impl Solution {
///
/// [原题链接](https://leetcode-cn.com/problems/min-cost-to-connect-all-points/)
/// # 方法1 Kruskal算法
/// todo
/// ## 源码
/// ```rust
/// pub fn min_cost_connect_points(points: Vec<Vec<i32>>) -> i32 {
@ -2316,7 +2288,6 @@ impl Solution {
/// # 方法2 建图优化Kruskal算法
/// 实现见源码
/// # 方法3 prim算法
/// todo
pub fn min_cost_connect_points(points: Vec<Vec<i32>>) -> i32 {
use std::collections::HashSet;
@ -2463,7 +2434,6 @@ impl Solution {
///
/// [原题链接](https://leetcode-cn.com/problems/find-critical-and-pseudo-critical-edges-in-minimum-spanning-tree/)
/// # 方法1 枚举+最小生成树判定
/// todo
/// ## 源码
/// ```rust
/// pub fn find_critical_and_pseudo_critical_edges(n: i32, mut edges: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
@ -3042,7 +3012,6 @@ impl Solution {
/// # 方法1 二分查找
/// # 方法2 并查集
/// # 方法3 Dijkstra 算法
/// todo
pub fn swim_in_water(grid: Vec<Vec<i32>>) -> i32 {
const DIRS: [(i32, i32); 4] = [(-1, 0), (1, 0), (0, -1), (0, 1)];
#[derive(PartialEq, Eq)]