mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Merge branch 'master' of github.com:youngyangyang04/leetcode-master
This commit is contained in:
@ -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)
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -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)
|
||||
|
@ -186,20 +186,26 @@ class Solution:
|
||||
|
||||
```go
|
||||
func fourSumCount(nums1 []int, nums2 []int, nums3 []int, nums4 []int) int {
|
||||
m := make(map[int]int) //key:a+b的数值,value:a+b数值出现的次数
|
||||
m := make(map[int]int)
|
||||
count := 0
|
||||
// 遍历nums1和nums2数组,统计两个数组元素之和,和出现的次数,放到map中
|
||||
|
||||
// 构建nums1和nums2的和的map
|
||||
for _, v1 := range nums1 {
|
||||
for _, v2 := range nums2 {
|
||||
m[v1+v2]++
|
||||
}
|
||||
}
|
||||
// 遍历nums3和nums4数组,找到如果 0-(c+d) 在map中出现过的话,就把map中key对应的value也就是出现次数统计出来
|
||||
|
||||
// 遍历nums3和nums4,检查-c-d是否在map中
|
||||
for _, v3 := range nums3 {
|
||||
for _, v4 := range nums4 {
|
||||
count += m[-v3-v4]
|
||||
sum := -v3 - v4
|
||||
if countVal, ok := m[sum]; ok {
|
||||
count += countVal
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return count
|
||||
}
|
||||
```
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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 {
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -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<int>& 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;//之前未出现过,标记为出现
|
||||
|
@ -671,6 +671,62 @@ public void Traversal(TreeNode cur, IList<int> 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;
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
|
Reference in New Issue
Block a user