mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 07:35:35 +08:00
更新 0383.赎金信 排版格式修复
This commit is contained in:
@ -34,7 +34,7 @@ canConstruct("aa", "aab") -> true
|
|||||||
|
|
||||||
* 第二点 “你可以假设两个字符串均只含有小写字母。” *说明只有小写字母*,这一点很重要
|
* 第二点 “你可以假设两个字符串均只含有小写字母。” *说明只有小写字母*,这一点很重要
|
||||||
|
|
||||||
## 暴力解法
|
### 暴力解法
|
||||||
|
|
||||||
那么第一个思路其实就是暴力枚举了,两层for循环,不断去寻找,代码如下:
|
那么第一个思路其实就是暴力枚举了,两层for循环,不断去寻找,代码如下:
|
||||||
|
|
||||||
@ -66,7 +66,7 @@ public:
|
|||||||
这里时间复杂度是比较高的,而且里面还有一个字符串删除也就是erase的操作,也是费时的,当然这段代码也可以过这道题。
|
这里时间复杂度是比较高的,而且里面还有一个字符串删除也就是erase的操作,也是费时的,当然这段代码也可以过这道题。
|
||||||
|
|
||||||
|
|
||||||
## 哈希解法
|
### 哈希解法
|
||||||
|
|
||||||
因为题目所只有小写字母,那可以采用空间换取时间的哈希策略, 用一个长度为26的数组还记录magazine里字母出现的次数。
|
因为题目所只有小写字母,那可以采用空间换取时间的哈希策略, 用一个长度为26的数组还记录magazine里字母出现的次数。
|
||||||
|
|
||||||
@ -112,8 +112,8 @@ public:
|
|||||||
|
|
||||||
## 其他语言版本
|
## 其他语言版本
|
||||||
|
|
||||||
|
### Java:
|
||||||
|
|
||||||
Java:
|
|
||||||
```Java
|
```Java
|
||||||
class Solution {
|
class Solution {
|
||||||
public boolean canConstruct(String ransomNote, String magazine) {
|
public boolean canConstruct(String ransomNote, String magazine) {
|
||||||
@ -146,8 +146,9 @@ class Solution {
|
|||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
### Python:
|
||||||
(版本一)使用数组
|
(版本一)使用数组
|
||||||
|
|
||||||
```python
|
```python
|
||||||
class Solution:
|
class Solution:
|
||||||
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
|
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))
|
return all(ransomNote.count(c) <= magazine.count(c) for c in set(ransomNote))
|
||||||
```
|
```
|
||||||
|
|
||||||
Go:
|
### Go:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func canConstruct(ransomNote string, magazine string) bool {
|
func canConstruct(ransomNote string, magazine string) bool {
|
||||||
@ -231,7 +232,7 @@ func canConstruct(ransomNote string, magazine string) bool {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
javaScript:
|
### JavaScript:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
/**
|
/**
|
||||||
@ -254,7 +255,7 @@ var canConstruct = function(ransomNote, magazine) {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
TypeScript:
|
### TypeScript:
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
function canConstruct(ransomNote: string, magazine: string): boolean {
|
function canConstruct(ransomNote: string, magazine: string): boolean {
|
||||||
@ -275,8 +276,8 @@ function canConstruct(ransomNote: string, magazine: string): boolean {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### PHP:
|
||||||
|
|
||||||
PHP:
|
|
||||||
```php
|
```php
|
||||||
class Solution {
|
class Solution {
|
||||||
/**
|
/**
|
||||||
@ -301,7 +302,8 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Swift:
|
### Swift:
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
func canConstruct(_ ransomNote: String, _ magazine: String) -> Bool {
|
func canConstruct(_ ransomNote: String, _ magazine: String) -> Bool {
|
||||||
var record = Array(repeating: 0, count: 26);
|
var record = Array(repeating: 0, count: 26);
|
||||||
@ -324,7 +326,8 @@ func canConstruct(_ ransomNote: String, _ magazine: String) -> Bool {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Rust:
|
### Rust:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
impl Solution {
|
impl Solution {
|
||||||
pub fn can_construct(ransom_note: String, magazine: String) -> bool {
|
pub fn can_construct(ransom_note: String, magazine: String) -> bool {
|
||||||
@ -347,7 +350,7 @@ impl Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Scala:
|
### Scala:
|
||||||
|
|
||||||
版本一: 使用数组作为哈希表
|
版本一: 使用数组作为哈希表
|
||||||
```scala
|
```scala
|
||||||
@ -411,8 +414,8 @@ object Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### C#:
|
||||||
|
|
||||||
C#:
|
|
||||||
```csharp
|
```csharp
|
||||||
public bool CanConstruct(string ransomNote, string magazine) {
|
public bool CanConstruct(string ransomNote, string magazine) {
|
||||||
if(ransomNote.Length > magazine.Length) return false;
|
if(ransomNote.Length > magazine.Length) return false;
|
||||||
@ -434,3 +437,4 @@ public bool CanConstruct(string ransomNote, string magazine) {
|
|||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user