diff --git a/problems/0101.对称二叉树.md b/problems/0101.对称二叉树.md
index 063b5429..31c24fc5 100644
--- a/problems/0101.对称二叉树.md
+++ b/problems/0101.对称二叉树.md
@@ -224,8 +224,8 @@ public:
st.push(root->left);
st.push(root->right);
while (!st.empty()) {
- TreeNode* leftNode = st.top(); st.pop();
TreeNode* rightNode = st.top(); st.pop();
+ TreeNode* leftNode = st.top(); st.pop();
if (!leftNode && !rightNode) {
continue;
}
@@ -950,3 +950,4 @@ public bool IsSymmetric(TreeNode root)
+
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)
diff --git a/problems/0454.四数相加II.md b/problems/0454.四数相加II.md
index 83dea97e..6231c22b 100644
--- a/problems/0454.四数相加II.md
+++ b/problems/0454.四数相加II.md
@@ -185,22 +185,28 @@ class Solution:
### Go:
```go
-func fourSumCount(nums1 []int, nums2 []int, nums3 []int, nums4 []int) int {
- m := make(map[int]int) //key:a+b的数值,value:a+b数值出现的次数
- count := 0
- // 遍历nums1和nums2数组,统计两个数组元素之和,和出现的次数,放到map中
+func fourSumCount(nums1 []int, nums2 []int, nums3 []int, nums4 []int) int {
+ m := make(map[int]int)
+ count := 0
+
+ // 构建nums1和nums2的和的map
for _, v1 := range nums1 {
- for _, v2 := range nums2 {
- m[v1+v2]++
- }
- }
- // 遍历nums3和nums4数组,找到如果 0-(c+d) 在map中出现过的话,就把map中key对应的value也就是出现次数统计出来
- for _, v3 := range nums3 {
- for _, v4 := range nums4 {
- count += m[-v3-v4]
- }
- }
- return count
+ for _, v2 := range nums2 {
+ m[v1+v2]++
+ }
+ }
+
+ // 遍历nums3和nums4,检查-c-d是否在map中
+ for _, v3 := range nums3 {
+ for _, v4 := range nums4 {
+ sum := -v3 - v4
+ if countVal, ok := m[sum]; ok {
+ count += countVal
+ }
+ }
+ }
+
+ return count
}
```
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;
}
}
diff --git a/problems/1020.飞地的数量.md b/problems/1020.飞地的数量.md
index ff59411e..f708e4a3 100644
--- a/problems/1020.飞地的数量.md
+++ b/problems/1020.飞地的数量.md
@@ -605,6 +605,63 @@ func bfs(grid [][]int, i, j int) {
}
```
+### JavaScript
+
+```js
+/**
+ * @param {number[][]} grid
+ * @return {number}
+ */
+var numEnclaves = function (grid) {
+ let row = grid.length;
+ let col = grid[0].length;
+ let count = 0;
+
+ // Check the first and last row, if there is a 1, then change all the connected 1s to 0 and don't count them.
+ for (let j = 0; j < col; j++) {
+ if (grid[0][j] === 1) {
+ dfs(0, j, false);
+ }
+ if (grid[row - 1][j] === 1) {
+ dfs(row - 1, j, false);
+ }
+ }
+
+ // Check the first and last column, if there is a 1, then change all the connected 1s to 0 and don't count them.
+ for (let i = 0; i < row; i++) {
+ if (grid[i][0] === 1) {
+ dfs(i, 0, false);
+ }
+ if (grid[i][col - 1] === 1) {
+ dfs(i, col - 1, false);
+ }
+ }
+
+ // Check the rest of the grid, if there is a 1, then change all the connected 1s to 0 and count them.
+ for (let i = 1; i < row - 1; i++) {
+ for (let j = 1; j < col - 1; j++) {
+ dfs(i, j, true);
+ }
+ }
+
+ function dfs(i, j, isCounting) {
+ let condition = i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] === 0;
+
+ if (condition) return;
+ if (isCounting) count++;
+
+ grid[i][j] = 0;
+
+ dfs(i - 1, j, isCounting);
+ dfs(i + 1, j, isCounting);
+ dfs(i, j - 1, isCounting);
+ dfs(i, j + 1, isCounting);
+ }
+
+ return count;
+};
+```
+
### Rust
dfs:
@@ -700,3 +757,4 @@ impl Solution {
+
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;//之前未出现过,标记为出现
diff --git a/problems/二叉树的递归遍历.md b/problems/二叉树的递归遍历.md
index a1d49e98..f2a97f4d 100644
--- a/problems/二叉树的递归遍历.md
+++ b/problems/二叉树的递归遍历.md
@@ -671,6 +671,62 @@ public void Traversal(TreeNode cur, IList 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;
+}
+```
+