From a8e19d60bb371a44296134932b7470825c1fda6a Mon Sep 17 00:00:00 2001 From: cezarbbb <105843128+cezarbbb@users.noreply.github.com> Date: Tue, 12 Jul 2022 14:08:50 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200202.=E5=BF=AB?= =?UTF-8?q?=E4=B9=90=E6=95=B0=20Rust=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0202.快乐数 Rust版本 --- problems/0202.快乐数.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/problems/0202.快乐数.md b/problems/0202.快乐数.md index 0bea0c72..2303765c 100644 --- a/problems/0202.快乐数.md +++ b/problems/0202.快乐数.md @@ -315,6 +315,36 @@ class Solution { } ``` +Rust: +```Rust +use std::collections::HashSet; +impl Solution { + pub fn get_sum(mut n: i32) -> i32 { + let mut sum = 0; + while n > 0 { + sum += (n % 10) * (n % 10); + n /= 10; + } + sum + } + + pub fn is_happy(n: i32) -> bool { + let mut n = n; + let mut set = HashSet::new(); + loop { + let sum = Self::get_sum(n); + if sum == 1 { + return true; + } + if set.contains(&sum) { + return false; + } else { set.insert(sum); } + n = sum; + } + } +} +``` + C: ```C typedef struct HashNodeTag { From 07ab44ede3fa043184754a638f59e6b76a7c3e0a Mon Sep 17 00:00:00 2001 From: cezarbbb <105843128+cezarbbb@users.noreply.github.com> Date: Tue, 12 Jul 2022 15:26:07 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200015.=E4=B8=89?= =?UTF-8?q?=E6=95=B0=E4=B9=8B=E5=92=8C=200018.=E5=9B=9B=E6=95=B0=E4=B9=8B?= =?UTF-8?q?=E5=92=8C=20Rust=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0015.三数之和 0018.四数之和 Rust版本 --- problems/0015.三数之和.md | 65 +++++++++++++++++++++++++++++++++++ problems/0018.四数之和.md | 45 ++++++++++++++++++++++++ 2 files changed, 110 insertions(+) diff --git a/problems/0015.三数之和.md b/problems/0015.三数之和.md index 4f1d711a..94885b0c 100644 --- a/problems/0015.三数之和.md +++ b/problems/0015.三数之和.md @@ -554,6 +554,71 @@ func threeSum(_ nums: [Int]) -> [[Int]] { } ``` +Rust: +```Rust +// 哈希解法 +use std::collections::HashSet; +impl Solution { + pub fn three_sum(nums: Vec) -> Vec> { + let mut result: Vec> = Vec::new(); + let mut nums = nums; + nums.sort(); + let len = nums.len(); + for i in 0..len { + if nums[i] > 0 { break; } + if i > 0 && nums[i] == nums[i - 1] { continue; } + let mut set = HashSet::new(); + for j in (i + 1)..len { + if j > i + 2 && nums[j] == nums[j - 1] && nums[j] == nums[j - 2] { continue; } + let c = 0 - (nums[i] + nums[j]); + if set.contains(&c) { + result.push(vec![nums[i], nums[j], c]); + set.remove(&c); + } else { set.insert(nums[j]); } + } + } + result + } +} +``` + +```Rust +// 双指针法 +use std::collections::HashSet; +impl Solution { + pub fn three_sum(nums: Vec) -> Vec> { + let mut result: Vec> = Vec::new(); + let mut nums = nums; + nums.sort(); + let len = nums.len(); + for i in 0..len { + if nums[i] > 0 { return result; } + if i > 0 && nums[i] == nums[i - 1] { continue; } + let (mut left, mut right) = (i + 1, len - 1); + while left < right { + if nums[i] + nums[left] + nums[right] > 0 { + right -= 1; + // 去重 + while left < right && nums[right] == nums[right + 1] { right -= 1; } + } else if nums[i] + nums[left] + nums[right] < 0 { + left += 1; + // 去重 + while left < right && nums[left] == nums[left - 1] { left += 1; } + } else { + result.push(vec![nums[i], nums[left], nums[right]]); + // 去重 + right -= 1; + left += 1; + while left < right && nums[right] == nums[right + 1] { right -= 1; } + while left < right && nums[left] == nums[left - 1] { left += 1; } + } + } + } + result + } +} +``` + C: ```C //qsort辅助cmp函数 diff --git a/problems/0018.四数之和.md b/problems/0018.四数之和.md index 2146a114..f6cfad29 100644 --- a/problems/0018.四数之和.md +++ b/problems/0018.四数之和.md @@ -522,6 +522,51 @@ public class Solution } } ``` + +Rust: +```Rust +impl Solution { + pub fn four_sum(nums: Vec, target: i32) -> Vec> { + let mut result: Vec> = Vec::new(); + let mut nums = nums; + nums.sort(); + let len = nums.len(); + for k in 0..len { + // 剪枝 + if nums[k] > target && (nums[k] > 0 || target > 0) { break; } + // 去重 + if k > 0 && nums[k] == nums[k - 1] { continue; } + for i in (k + 1)..len { + // 剪枝 + if nums[k] + nums[i] > target && (nums[k] + nums[i] >= 0 || target >= 0) { break; } + // 去重 + if i > k + 1 && nums[i] == nums[i - 1] { continue; } + let (mut left, mut right) = (i + 1, len - 1); + while left < right { + if nums[k] + nums[i] > target - (nums[left] + nums[right]) { + right -= 1; + // 去重 + while left < right && nums[right] == nums[right + 1] { right -= 1; } + } else if nums[k] + nums[i] < target - (nums[left] + nums[right]) { + left += 1; + // 去重 + while left < right && nums[left] == nums[left - 1] { left += 1; } + } else { + result.push(vec![nums[k], nums[i], nums[left], nums[right]]); + // 去重 + while left < right && nums[right] == nums[right - 1] { right -= 1; } + while left < right && nums[left] == nums[left + 1] { left += 1; } + left += 1; + right -= 1; + } + } + } + } + result + } +} +``` + Scala: ```scala object Solution {