mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
更新 图论 并查集 模拟 位运算 额外题目 排版格式修复
This commit is contained in:
@ -34,7 +34,7 @@
|
||||
* 输出:[1]
|
||||
|
||||
|
||||
# 思路
|
||||
## 思路
|
||||
|
||||
一些同学可能手动写排列的顺序,都没有写对,那么写程序的话思路一定是有问题的了,我这里以1234为例子,把全排列都列出来。可以参考一下规律所在:
|
||||
|
||||
@ -92,9 +92,9 @@ public:
|
||||
};
|
||||
```
|
||||
|
||||
# 其他语言版本
|
||||
## 其他语言版本
|
||||
|
||||
## Java
|
||||
### Java
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
@ -159,7 +159,7 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
## Python
|
||||
### Python
|
||||
>直接使用sorted()会开辟新的空间并返回一个新的list,故补充一个原地反转函数
|
||||
```python
|
||||
class Solution:
|
||||
@ -191,7 +191,7 @@ class Solution:
|
||||
"""
|
||||
```
|
||||
|
||||
## Go
|
||||
### Go
|
||||
|
||||
```go
|
||||
//卡尔的解法
|
||||
@ -216,7 +216,7 @@ func reverse(a []int,begin,end int){
|
||||
}
|
||||
```
|
||||
|
||||
## JavaScript
|
||||
### JavaScript
|
||||
|
||||
```js
|
||||
//卡尔的解法(吐槽一下JavaScript的sort和其他语言的不太一样,只想到了拷贝数组去排序再替换原数组来实现nums的[i + 1, nums.length)升序排序)
|
||||
@ -272,3 +272,4 @@ var nextPermutation = function(nums) {
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -29,7 +29,7 @@
|
||||
* 解释:endWord "cog" 不在字典中,所以无法进行转换。
|
||||
|
||||
|
||||
# 思路
|
||||
## 思路
|
||||
|
||||
以示例1为例,从这个图中可以看出 hit 到 cog的路线,不止一条,有三条,一条是最短的长度为5,两条长度为6。
|
||||
|
||||
@ -97,9 +97,9 @@ public:
|
||||
|
||||
当然本题也可以用双向BFS,就是从头尾两端进行搜索,大家感兴趣,可以自己去实现,这里就不再做详细讲解了。
|
||||
|
||||
# 其他语言版本
|
||||
## 其他语言版本
|
||||
|
||||
## Java
|
||||
### Java
|
||||
|
||||
```java
|
||||
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
|
||||
@ -196,7 +196,7 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
## Python
|
||||
### Python
|
||||
|
||||
```
|
||||
class Solution:
|
||||
@ -221,7 +221,7 @@ class Solution:
|
||||
queue.append(newWord)
|
||||
return 0
|
||||
```
|
||||
## Go
|
||||
### Go
|
||||
```go
|
||||
func ladderLength(beginWord string, endWord string, wordList []string) int {
|
||||
wordMap, que, depth := getWordMap(wordList, beginWord), []string{beginWord}, 0
|
||||
@ -274,7 +274,7 @@ func getCandidates(word string) []string {
|
||||
}
|
||||
```
|
||||
|
||||
## JavaScript
|
||||
### JavaScript
|
||||
```javascript
|
||||
var ladderLength = function(beginWord, endWord, wordList) {
|
||||
// 将wordList转成Set,提高查询速度
|
||||
@ -310,7 +310,7 @@ var ladderLength = function(beginWord, endWord, wordList) {
|
||||
};
|
||||
```
|
||||
|
||||
## TypeScript
|
||||
### TypeScript
|
||||
```typescript
|
||||
function ladderLength(
|
||||
beginWord: string,
|
||||
@ -364,4 +364,3 @@ function diffonechar(word1: string, word2: string): boolean {
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -90,7 +90,7 @@ public:
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
Java:
|
||||
### Java:
|
||||
|
||||
```java
|
||||
// 解法一
|
||||
@ -191,8 +191,8 @@ class Solution {
|
||||
|
||||
```
|
||||
|
||||
Python:
|
||||
### 解法1:
|
||||
### Python:
|
||||
|
||||
扫描每个cell,如果当前位置为岛屿 grid[i][j] == 1, 从当前位置判断四边方向,如果边界或者是水域,证明有边界存在,res矩阵的对应cell加一。
|
||||
|
||||
```python
|
||||
@ -228,7 +228,8 @@ class Solution:
|
||||
|
||||
```
|
||||
|
||||
Go:
|
||||
### Go:
|
||||
|
||||
```go
|
||||
func islandPerimeter(grid [][]int) int {
|
||||
m, n := len(grid), len(grid[0])
|
||||
@ -249,7 +250,8 @@ func islandPerimeter(grid [][]int) int {
|
||||
}
|
||||
```
|
||||
|
||||
JavaScript:
|
||||
### JavaScript:
|
||||
|
||||
```javascript
|
||||
//解法一
|
||||
var islandPerimeter = function(grid) {
|
||||
@ -305,3 +307,4 @@ var islandPerimeter = function(grid) {
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -29,7 +29,7 @@
|
||||
|
||||
|
||||
|
||||
# 思路
|
||||
## 思路
|
||||
|
||||
这道题目还是挺简单的,大家不要想复杂了,一波哈希法又一波图论算法啥的,哈哈。
|
||||
|
||||
@ -64,9 +64,9 @@ public:
|
||||
```
|
||||
|
||||
|
||||
# 其他语言版本
|
||||
## 其他语言版本
|
||||
|
||||
## Java
|
||||
### Java
|
||||
|
||||
```java
|
||||
// 时间复杂度:O(n)
|
||||
@ -86,7 +86,7 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
## Python
|
||||
### Python
|
||||
|
||||
```python
|
||||
# 时间复杂度:O(n)
|
||||
@ -107,7 +107,7 @@ class Solution:
|
||||
return x == 0 and y == 0
|
||||
```
|
||||
|
||||
## Go
|
||||
### Go
|
||||
|
||||
```go
|
||||
func judgeCircle(moves string) bool {
|
||||
@ -131,7 +131,7 @@ func judgeCircle(moves string) bool {
|
||||
}
|
||||
```
|
||||
|
||||
## JavaScript
|
||||
### JavaScript
|
||||
|
||||
```js
|
||||
// 时间复杂度:O(n)
|
||||
@ -150,7 +150,7 @@ var judgeCircle = function(moves) {
|
||||
```
|
||||
|
||||
|
||||
## TypeScript
|
||||
### TypeScript
|
||||
|
||||
```ts
|
||||
var judgeCircle = function (moves) {
|
||||
@ -185,3 +185,4 @@ var judgeCircle = function (moves) {
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -25,7 +25,7 @@
|
||||
* edges 中无重复元素
|
||||
* 给定的图是连通的
|
||||
|
||||
# 思路
|
||||
## 思路
|
||||
|
||||
这道题目也是并查集基础题目。
|
||||
|
||||
@ -150,9 +150,9 @@ public:
|
||||
可以看出,主函数的代码很少,就判断一下边的两个节点在不在同一个集合就可以了。
|
||||
|
||||
|
||||
# 其他语言版本
|
||||
## 其他语言版本
|
||||
|
||||
## Java
|
||||
### Java
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
@ -205,7 +205,7 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
## Python
|
||||
### Python
|
||||
|
||||
```python
|
||||
|
||||
@ -256,7 +256,7 @@ class Solution:
|
||||
return []
|
||||
```
|
||||
|
||||
## Go
|
||||
### Go
|
||||
|
||||
```go
|
||||
|
||||
@ -312,7 +312,7 @@ func findRedundantConnection(edges [][]int) []int {
|
||||
}
|
||||
```
|
||||
|
||||
## JavaScript
|
||||
### JavaScript
|
||||
|
||||
```js
|
||||
const n = 1005;
|
||||
@ -365,3 +365,4 @@ var findRedundantConnection = function(edges) {
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -213,9 +213,9 @@ public:
|
||||
```
|
||||
|
||||
|
||||
# 其他语言版本
|
||||
## 其他语言版本
|
||||
|
||||
## Java
|
||||
### Java
|
||||
|
||||
```java
|
||||
|
||||
@ -335,7 +335,7 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
## Python
|
||||
### Python
|
||||
|
||||
```python
|
||||
|
||||
@ -426,7 +426,7 @@ class Solution:
|
||||
return self.getRemoveEdge(edges)
|
||||
```
|
||||
|
||||
## Go
|
||||
### Go
|
||||
|
||||
```go
|
||||
|
||||
@ -527,7 +527,7 @@ func findRedundantDirectedConnection(edges [][]int) []int {
|
||||
|
||||
```
|
||||
|
||||
## JavaScript
|
||||
### JavaScript
|
||||
|
||||
```js
|
||||
const N = 1010; // 如题:二维数组大小的在3到1000范围内
|
||||
@ -623,3 +623,4 @@ var findRedundantDirectedConnection = function(edges) {
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -46,7 +46,7 @@
|
||||
|
||||
|
||||
|
||||
# 思路
|
||||
## 思路
|
||||
|
||||
这道题其实是考察如何计算一个数的二进制中1的数量。
|
||||
|
||||
@ -87,7 +87,7 @@ int bitCount(int n) {
|
||||
|
||||
下面我就使用方法二,来做这道题目:
|
||||
|
||||
## C++代码
|
||||
|
||||
|
||||
```C++
|
||||
class Solution {
|
||||
@ -116,9 +116,9 @@ public:
|
||||
|
||||
|
||||
|
||||
# 其他语言版本
|
||||
## 其他语言版本
|
||||
|
||||
## Java
|
||||
### Java
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
@ -151,7 +151,7 @@ class Solution {
|
||||
|
||||
|
||||
|
||||
## Python
|
||||
### Python
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
@ -167,7 +167,7 @@ class Solution:
|
||||
return count
|
||||
```
|
||||
|
||||
## Go
|
||||
### Go
|
||||
|
||||
```go
|
||||
func sortByBits(arr []int) []int {
|
||||
@ -205,7 +205,7 @@ func bitCount(n int) int {
|
||||
}
|
||||
```
|
||||
|
||||
## JavaScript
|
||||
### JavaScript
|
||||
|
||||
```js
|
||||
var sortByBits = function(arr) {
|
||||
@ -227,3 +227,4 @@ var sortByBits = function(arr) {
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
Reference in New Issue
Block a user