mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 15:45:40 +08:00
更新 0242.有效的字母异位词 排版格式修复
This commit is contained in:
@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
> 数组就是简单的哈希表,但是数组的大小可不是无限开辟的
|
> 数组就是简单的哈希表,但是数组的大小可不是无限开辟的
|
||||||
|
|
||||||
## 242.有效的字母异位词
|
# 242.有效的字母异位词
|
||||||
|
|
||||||
[力扣题目链接](https://leetcode.cn/problems/valid-anagram/)
|
[力扣题目链接](https://leetcode.cn/problems/valid-anagram/)
|
||||||
|
|
||||||
@ -21,13 +21,14 @@
|
|||||||
输入: s = "rat", t = "car"
|
输入: s = "rat", t = "car"
|
||||||
输出: false
|
输出: false
|
||||||
|
|
||||||
|
|
||||||
**说明:**
|
**说明:**
|
||||||
你可以假设字符串只包含小写字母。
|
你可以假设字符串只包含小写字母。
|
||||||
|
|
||||||
## 思路
|
## 算法公开课
|
||||||
|
|
||||||
本题B站视频讲解版:[学透哈希表,数组使用有技巧!Leetcode:242.有效的字母异位词](https://www.bilibili.com/video/BV1YG411p7BA)
|
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[学透哈希表,数组使用有技巧!Leetcode:242.有效的字母异位词](https://www.bilibili.com/video/BV1YG411p7BA),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||||
|
|
||||||
|
## 思路
|
||||||
|
|
||||||
先看暴力的解法,两层for循环,同时还要记录字符是否重复出现,很明显时间复杂度是 O(n^2)。
|
先看暴力的解法,两层for循环,同时还要记录字符是否重复出现,很明显时间复杂度是 O(n^2)。
|
||||||
|
|
||||||
@ -88,12 +89,10 @@ public:
|
|||||||
* 时间复杂度: O(n)
|
* 时间复杂度: O(n)
|
||||||
* 空间复杂度: O(1)
|
* 空间复杂度: O(1)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## 其他语言版本
|
## 其他语言版本
|
||||||
|
|
||||||
|
### Java:
|
||||||
|
|
||||||
Java:
|
|
||||||
```java
|
```java
|
||||||
/**
|
/**
|
||||||
* 242. 有效的字母异位词 字典解法
|
* 242. 有效的字母异位词 字典解法
|
||||||
@ -121,7 +120,7 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
### Python:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
class Solution:
|
class Solution:
|
||||||
@ -165,7 +164,7 @@ class Solution(object):
|
|||||||
return a_count == b_count
|
return a_count == b_count
|
||||||
```
|
```
|
||||||
|
|
||||||
Go:
|
### Go:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func isAnagram(s string, t string) bool {
|
func isAnagram(s string, t string) bool {
|
||||||
@ -182,7 +181,7 @@ func isAnagram(s string, t string) bool {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
javaScript:
|
### JavaScript:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
/**
|
/**
|
||||||
@ -218,7 +217,7 @@ var isAnagram = function(s, t) {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
TypeScript:
|
### TypeScript:
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
function isAnagram(s: string, t: string): boolean {
|
function isAnagram(s: string, t: string): boolean {
|
||||||
@ -233,7 +232,7 @@ function isAnagram(s: string, t: string): boolean {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
Swift:
|
### Swift:
|
||||||
|
|
||||||
```Swift
|
```Swift
|
||||||
func isAnagram(_ s: String, _ t: String) -> Bool {
|
func isAnagram(_ s: String, _ t: String) -> Bool {
|
||||||
@ -257,7 +256,8 @@ func isAnagram(_ s: String, _ t: String) -> Bool {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
PHP:
|
### PHP:
|
||||||
|
|
||||||
```php
|
```php
|
||||||
class Solution {
|
class Solution {
|
||||||
/**
|
/**
|
||||||
@ -292,7 +292,8 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Rust:
|
### Rust:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
impl Solution {
|
impl Solution {
|
||||||
pub fn is_anagram(s: String, t: String) -> bool {
|
pub fn is_anagram(s: String, t: String) -> bool {
|
||||||
@ -312,8 +313,8 @@ impl Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Scala:
|
||||||
|
|
||||||
Scala:
|
|
||||||
```scala
|
```scala
|
||||||
object Solution {
|
object Solution {
|
||||||
def isAnagram(s: String, t: String): Boolean = {
|
def isAnagram(s: String, t: String): Boolean = {
|
||||||
@ -337,8 +338,8 @@ object Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### C#:
|
||||||
|
|
||||||
C#:
|
|
||||||
```csharp
|
```csharp
|
||||||
public bool IsAnagram(string s, string t) {
|
public bool IsAnagram(string s, string t) {
|
||||||
int sl=s.Length,tl=t.Length;
|
int sl=s.Length,tl=t.Length;
|
||||||
@ -360,11 +361,12 @@ C#:
|
|||||||
## 相关题目
|
## 相关题目
|
||||||
|
|
||||||
* [383.赎金信](https://programmercarl.com/0383.%E8%B5%8E%E9%87%91%E4%BF%A1.html)
|
* [383.赎金信](https://programmercarl.com/0383.%E8%B5%8E%E9%87%91%E4%BF%A1.html)
|
||||||
* 49.字母异位词分组
|
* [49.字母异位词分组](https://leetcode.cn/problems/group-anagrams/)
|
||||||
* 438.找到字符串中所有字母异位词
|
* [438.找到字符串中所有字母异位词](https://leetcode.cn/problems/find-all-anagrams-in-a-string/)
|
||||||
|
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<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