From 0922ede8c7ce3fedbd7494834beb224aa3da6311 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 19 Jul 2023 14:49:56 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200383.=E8=B5=8E=E9=87=91?= =?UTF-8?q?=E4=BF=A1=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0383.赎金信.md | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/problems/0383.赎金信.md b/problems/0383.赎金信.md index e74cdf71..8122240e 100644 --- a/problems/0383.赎金信.md +++ b/problems/0383.赎金信.md @@ -34,7 +34,7 @@ canConstruct("aa", "aab") -> true * 第二点 “你可以假设两个字符串均只含有小写字母。” *说明只有小写字母*,这一点很重要 -## 暴力解法 +### 暴力解法 那么第一个思路其实就是暴力枚举了,两层for循环,不断去寻找,代码如下: @@ -66,7 +66,7 @@ public: 这里时间复杂度是比较高的,而且里面还有一个字符串删除也就是erase的操作,也是费时的,当然这段代码也可以过这道题。 -## 哈希解法 +### 哈希解法 因为题目所只有小写字母,那可以采用空间换取时间的哈希策略, 用一个长度为26的数组还记录magazine里字母出现的次数。 @@ -112,8 +112,8 @@ public: ## 其他语言版本 +### Java: -Java: ```Java class Solution { public boolean canConstruct(String ransomNote, String magazine) { @@ -146,8 +146,9 @@ class Solution { ``` -Python: +### Python: (版本一)使用数组 + ```python class Solution: def canConstruct(self, ransomNote: str, magazine: str) -> bool: @@ -213,7 +214,7 @@ class Solution: return all(ransomNote.count(c) <= magazine.count(c) for c in set(ransomNote)) ``` -Go: +### Go: ```go func canConstruct(ransomNote string, magazine string) bool { @@ -231,7 +232,7 @@ func canConstruct(ransomNote string, magazine string) bool { } ``` -javaScript: +### JavaScript: ```js /** @@ -254,7 +255,7 @@ var canConstruct = function(ransomNote, magazine) { }; ``` -TypeScript: +### TypeScript: ```typescript function canConstruct(ransomNote: string, magazine: string): boolean { @@ -275,8 +276,8 @@ function canConstruct(ransomNote: string, magazine: string): boolean { }; ``` +### PHP: -PHP: ```php class Solution { /** @@ -301,7 +302,8 @@ class Solution { } ``` -Swift: +### Swift: + ```swift func canConstruct(_ ransomNote: String, _ magazine: String) -> Bool { var record = Array(repeating: 0, count: 26); @@ -324,7 +326,8 @@ func canConstruct(_ ransomNote: String, _ magazine: String) -> Bool { } ``` -Rust: +### Rust: + ```rust impl Solution { pub fn can_construct(ransom_note: String, magazine: String) -> bool { @@ -347,7 +350,7 @@ impl Solution { } ``` -Scala: +### Scala: 版本一: 使用数组作为哈希表 ```scala @@ -411,8 +414,8 @@ object Solution { } ``` +### C#: -C#: ```csharp public bool CanConstruct(string ransomNote, string magazine) { if(ransomNote.Length > magazine.Length) return false; @@ -434,3 +437,4 @@ public bool CanConstruct(string ransomNote, string magazine) { +