Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from youngyangyang04:master #459

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 5 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions problems/1020.้ฃžๅœฐ็š„ๆ•ฐ้‡.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -700,3 +757,4 @@ impl Solution {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/็ฝ‘็ซ™ๆ˜Ÿ็ƒๅฎฃไผ ๆตทๆŠฅ.jpg" width="1000"/>
</a>

View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down

AltStyle ใซใ‚ˆใฃใฆๅค‰ๆ›ใ•ใ‚ŒใŸใƒšใƒผใ‚ธ (->ใ‚ชใƒชใ‚ธใƒŠใƒซ) /