res)
}
```
+### PHP
+```php
+// 144.前序遍历
+function preorderTraversal($root) {
+ $output = [];
+ $this->traversal($root, $output);
+ return $output;
+}
+
+function traversal($root, array &$output) {
+ if ($root->val === null) {
+ return;
+ }
+
+ $output[] = $root->val;
+ $this->traversal($root->left, $output);
+ $this->traversal($root->right, $output);
+}
+```
+```php
+// 94.中序遍历
+function inorderTraversal($root) {
+ $output = [];
+ $this->traversal($root, $output);
+ return $output;
+}
+
+function traversal($root, array &$output) {
+ if ($root->val === null) {
+ return;
+ }
+
+ $this->traversal($root->left, $output);
+ $output[] = $root->val;
+ $this->traversal($root->right, $output);
+}
+```
+```php
+// 145.后序遍历
+function postorderTraversal($root) {
+ $output = [];
+ $this->traversal($root, $output);
+ return $output;
+}
+
+function traversal($root, array &$output) {
+ if ($root->val === null) {
+ return;
+ }
+
+ $this->traversal($root->left, $output);
+ $this->traversal($root->right, $output);
+ $output[] = $root->val;
+}
+```
+
From b5a5340d1a964254325dc4ed64507c3460a9d3b9 Mon Sep 17 00:00:00 2001
From: mengyi
Date: Wed, 5 Jun 2024 19:08:31 -0400
Subject: [PATCH 5/7] add curly braces and comments to advoid confusion
---
problems/0704.二分查找.md | 20 ++++++++++++++------
1 file changed, 14 insertions(+), 6 deletions(-)
diff --git a/problems/0704.二分查找.md b/problems/0704.二分查找.md
index 5604cd56..d86146d6 100644
--- a/problems/0704.二分查找.md
+++ b/problems/0704.二分查找.md
@@ -174,13 +174,17 @@ class Solution {
int left = 0, right = nums.length - 1;
while (left <= right) {
int mid = left + ((right - left) >> 1);
- if (nums[mid] == target)
+ if (nums[mid] == target) {
return mid;
- else if (nums[mid] < target)
+ }
+ else if (nums[mid] < target) {
left = mid + 1;
- else if (nums[mid] > target)
+ }
+ else { // nums[mid] > target
right = mid - 1;
+ }
}
+ // 未找到目标值
return -1;
}
}
@@ -194,13 +198,17 @@ class Solution {
int left = 0, right = nums.length;
while (left < right) {
int mid = left + ((right - left) >> 1);
- if (nums[mid] == target)
+ if (nums[mid] == target) {
return mid;
- else if (nums[mid] < target)
+ }
+ else if (nums[mid] < target) {
left = mid + 1;
- else if (nums[mid] > target)
+ }
+ else { // nums[mid] > target
right = mid;
+ }
}
+ // 未找到目标值
return -1;
}
}
From 0f6ea64f68857ea7c080b820f31b88f622cab276 Mon Sep 17 00:00:00 2001
From: Jack Lin
Date: Sat, 15 Jun 2024 15:11:24 +0800
Subject: [PATCH 6/7] Update 0242: Add C solution
---
problems/0242.有效的字母异位词.md | 25 +++++++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git a/problems/0242.有效的字母异位词.md b/problems/0242.有效的字母异位词.md
index ac03ddbb..61488f03 100644
--- a/problems/0242.有效的字母异位词.md
+++ b/problems/0242.有效的字母异位词.md
@@ -383,6 +383,31 @@ object Solution {
}
```
+### C
+
+```c
+bool isAnagram(char* s, char* t) {
+ int len1 = strlen(s), len2 = strlen(t);
+ if (len1 != len2) {
+ return false;
+ }
+
+ int map1[26] = {0}, map2[26] = {0};
+ for (int i = 0; i < len1; i++) {
+ map1[s[i] - 'a'] += 1;
+ map2[t[i] - 'a'] += 1;
+ }
+
+ for (int i = 0; i < 26; i++) {
+ if (map1[i] != map2[i]) {
+ return false;
+ }
+ }
+
+ return true;
+}
+```
+
## 相关题目
* [383.赎金信](https://programmercarl.com/0383.%E8%B5%8E%E9%87%91%E4%BF%A1.html)
From 9aca3b5ca46a3bd39bbf9e15568b5bc390d55466 Mon Sep 17 00:00:00 2001
From: jycathy
Date: Sun, 16 Jun 2024 17:32:38 +0800
Subject: [PATCH 7/7] fix prob.1207
---
problems/1207.独一无二的出现次数.md | 18 +++++++++---------
1 file changed, 9 insertions(+), 9 deletions(-)
diff --git a/problems/1207.独一无二的出现次数.md b/problems/1207.独一无二的出现次数.md
index 5c5f92c3..cd89522e 100644
--- a/problems/1207.独一无二的出现次数.md
+++ b/problems/1207.独一无二的出现次数.md
@@ -40,9 +40,9 @@
回归本题,**本题强调了-1000 <= arr[i] <= 1000**,那么就可以用数组来做哈希,arr[i]作为哈希表(数组)的下标,那么arr[i]可以是负数,怎么办?负数不能做数组下标。
-**此时可以定义一个2000大小的数组,例如int count[2002];**,统计的时候,将arr[i]统一加1000,这样就可以统计arr[i]的出现频率了。
+**此时可以定义一个2001大小的数组,例如int count[2001];**,统计的时候,将arr[i]统一加1000,这样就可以统计arr[i]的出现频率了。
-题目中要求的是是否有相同的频率出现,那么需要再定义一个哈希表(数组)用来记录频率是否重复出现过,bool fre[1002]; 定义布尔类型的就可以了,**因为题目中强调1 <= arr.length <= 1000,所以哈希表大小为1000就可以了**。
+题目中要求的是是否有相同的频率出现,那么需要再定义一个哈希表(数组)用来记录频率是否重复出现过,bool fre[1001]; 定义布尔类型的就可以了,**因为题目中强调1 <= arr.length <= 1000,所以哈希表大小为1000就可以了**。
如图所示:
@@ -55,11 +55,11 @@ C++代码如下:
class Solution {
public:
bool uniqueOccurrences(vector& arr) {
- int count[2002] = {0}; // 统计数字出现的频率
+ int count[2001] = {0}; // 统计数字出现的频率
for (int i = 0; i < arr.size(); i++) {
count[arr[i] + 1000]++;
}
- bool fre[1002] = {false}; // 看相同频率是否重复出现
+ bool fre[1001] = {false}; // 看相同频率是否重复出现
for (int i = 0; i <= 2000; i++) {
if (count[i]) {
if (fre[count[i]] == false) fre[count[i]] = true;
@@ -78,7 +78,7 @@ public:
```java
class Solution {
public boolean uniqueOccurrences(int[] arr) {
- int[] count = new int[2002];
+ int[] count = new int[2001];
for (int i = 0; i < arr.length; i++) {
count[arr[i] + 1000]++; // 防止负数作为下标
}
@@ -103,10 +103,10 @@ class Solution {
# 方法 1: 数组在哈西法的应用
class Solution:
def uniqueOccurrences(self, arr: List[int]) -> bool:
- count = [0] * 2002
+ count = [0] * 2001
for i in range(len(arr)):
count[arr[i] + 1000] += 1 # 防止负数作为下标
- freq = [False] * 1002 # 标记相同频率是否重复出现
+ freq = [False] * 1001 # 标记相同频率是否重复出现
for i in range(2001):
if count[i] > 0:
if freq[count[i]] == False:
@@ -139,12 +139,12 @@ class Solution:
``` javascript
// 方法一:使用数组记录元素出现次数
var uniqueOccurrences = function(arr) {
- const count = new Array(2002).fill(0);// -1000 <= arr[i] <= 1000
+ const count = new Array(2001).fill(0);// -1000 <= arr[i] <= 1000
for(let i = 0; i < arr.length; i++){
count[arr[i] + 1000]++;// 防止负数作为下标
}
// 标记相同频率是否重复出现
- const fre = new Array(1002).fill(false);// 1 <= arr.length <= 1000
+ const fre = new Array(1001).fill(false);// 1 <= arr.length <= 1000
for(let i = 0; i <= 2000; i++){
if(count[i] > 0){//有i出现过
if(fre[count[i]] === false) fre[count[i]] = true;//之前未出现过,标记为出现