mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 11:34:46 +08:00
Merge branch 'youngyangyang04:master' into master
This commit is contained in:
@ -247,3 +247,35 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
## 其他语言版本
|
## 其他语言版本
|
||||||
|
|
||||||
|
### Java
|
||||||
|
|
||||||
|
下面的代码使用的是深度优先搜索 DFS 的做法。为了统计岛屿数量同时不重复记录,每当我们搜索到一个岛后,就将这个岛 “淹没” —— 将这个岛所占的地方从 “1” 改为 “0”,这样就不用担心后续会重复记录这个岛屿了。而 DFS 的过程就体现在 “淹没” 这一步中。详见代码:
|
||||||
|
|
||||||
|
```java
|
||||||
|
public int numIslands(char[][] grid) {
|
||||||
|
int res = 0; //记录找到的岛屿数量
|
||||||
|
for(int i = 0;i < grid.length;i++){
|
||||||
|
for(int j = 0;j < grid[0].length;j++){
|
||||||
|
//找到“1”,res加一,同时淹没这个岛
|
||||||
|
if(grid[i][j] == '1'){
|
||||||
|
res++;
|
||||||
|
dfs(grid,i,j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
//使用DFS“淹没”岛屿
|
||||||
|
public void dfs(char[][] grid, int i, int j){
|
||||||
|
//搜索边界:索引越界或遍历到了"0"
|
||||||
|
if(i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] == '0') return;
|
||||||
|
//将这块土地标记为"0"
|
||||||
|
grid[i][j] = '0';
|
||||||
|
//根据"每座岛屿只能由水平方向或竖直方向上相邻的陆地连接形成",对上下左右的相邻顶点进行dfs
|
||||||
|
dfs(grid,i - 1,j);
|
||||||
|
dfs(grid,i + 1,j);
|
||||||
|
dfs(grid,i,j + 1);
|
||||||
|
dfs(grid,i,j - 1);
|
||||||
|
}
|
||||||
|
```
|
@ -92,18 +92,24 @@ public:
|
|||||||
|
|
||||||
Java:
|
Java:
|
||||||
```java
|
```java
|
||||||
|
/**
|
||||||
|
* 242. 有效的字母异位词 字典解法
|
||||||
|
* 时间复杂度O(m+n) 空间复杂度O(1)
|
||||||
|
*/
|
||||||
class Solution {
|
class Solution {
|
||||||
public boolean isAnagram(String s, String t) {
|
public boolean isAnagram(String s, String t) {
|
||||||
|
|
||||||
int[] record = new int[26];
|
int[] record = new int[26];
|
||||||
for (char c : s.toCharArray()) {
|
|
||||||
record[c - 'a'] += 1;
|
for (int i = 0; i < s.length(); i++) {
|
||||||
|
record[s.charAt(i) - 'a']++;
|
||||||
}
|
}
|
||||||
for (char c : t.toCharArray()) {
|
|
||||||
record[c - 'a'] -= 1;
|
for (int i = 0; i < t.length(); i++) {
|
||||||
|
record[t.charAt(i) - 'a']--;
|
||||||
}
|
}
|
||||||
for (int i : record) {
|
|
||||||
if (i != 0) {
|
for (int count: record) {
|
||||||
|
if (count != 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -239,18 +239,24 @@ Typescript:
|
|||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
function sortedSquares(nums: number[]): number[] {
|
function sortedSquares(nums: number[]): number[] {
|
||||||
let left: number = 0, right: number = nums.length - 1;
|
const ans: number[] = [];
|
||||||
let resArr: number[] = new Array(nums.length);
|
let left = 0,
|
||||||
let resArrIndex: number = resArr.length - 1;
|
right = nums.length - 1;
|
||||||
|
|
||||||
while (left <= right) {
|
while (left <= right) {
|
||||||
if (Math.abs(nums[left]) < Math.abs(nums[right])) {
|
// 右侧的元素不需要取绝对值,nums 为非递减排序的整数数组
|
||||||
resArr[resArrIndex] = nums[right--] ** 2;
|
// 在同为负数的情况下,左侧的平方值一定大于右侧的平方值
|
||||||
|
if (Math.abs(nums[left]) > nums[right]) {
|
||||||
|
// 使用 Array.prototype.unshift() 直接在数组的首项插入当前最大值
|
||||||
|
ans.unshift(nums[left] ** 2);
|
||||||
|
left++;
|
||||||
} else {
|
} else {
|
||||||
resArr[resArrIndex] = nums[left++] ** 2;
|
ans.unshift(nums[right] ** 2);
|
||||||
|
right--;
|
||||||
}
|
}
|
||||||
resArrIndex--;
|
|
||||||
}
|
}
|
||||||
return resArr;
|
|
||||||
|
return ans;
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user