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:
@ -263,21 +263,38 @@ public:
|
|||||||
Java:
|
Java:
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public int[] twoSum(int[] nums, int target) {
|
// 双指针 动态规划
|
||||||
int[] res = new int[2];
|
class Solution {
|
||||||
if(nums == null || nums.length == 0){
|
public String longestPalindrome(String s) {
|
||||||
return res;
|
if (s.length() == 0 || s.length() == 1) return s;
|
||||||
}
|
int length = 1;
|
||||||
Map<Integer, Integer> map = new HashMap<>();
|
int index = 0;
|
||||||
for(int i = 0; i < nums.length; i++){
|
boolean[][] palindrome = new boolean[s.length()][s.length()];
|
||||||
int temp = target - nums[i];
|
for (int i = 0; i < s.length(); i++) {
|
||||||
if(map.containsKey(temp)){
|
palindrome[i][i] = true;
|
||||||
res[1] = i;
|
|
||||||
res[0] = map.get(temp);
|
|
||||||
}
|
}
|
||||||
map.put(nums[i], i);
|
|
||||||
|
for (int L = 2; L <= s.length(); L++) {
|
||||||
|
for (int i = 0; i < s.length(); i++) {
|
||||||
|
int j = i + L - 1;
|
||||||
|
if (j >= s.length()) break;
|
||||||
|
if (s.charAt(i) != s.charAt(j)) {
|
||||||
|
palindrome[i][j] = false;
|
||||||
|
} else {
|
||||||
|
if (j - i < 3) {
|
||||||
|
palindrome[i][j] = true;
|
||||||
|
} else {
|
||||||
|
palindrome[i][j] = palindrome[i + 1][j - 1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (palindrome[i][j] && j - i + 1 > length) {
|
||||||
|
length = j - i + 1;
|
||||||
|
index = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return s.substring(index, index + length);
|
||||||
}
|
}
|
||||||
return res;
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -222,56 +222,6 @@ public:
|
|||||||
|
|
||||||
## 其他语言补充
|
## 其他语言补充
|
||||||
|
|
||||||
|
|
||||||
### Python
|
|
||||||
|
|
||||||
```python
|
|
||||||
class Solution:
|
|
||||||
def solveNQueens(self, n: int) -> List[List[str]]:
|
|
||||||
if not n: return []
|
|
||||||
board = [['.'] * n for _ in range(n)]
|
|
||||||
res = []
|
|
||||||
def isVaild(board,row, col):
|
|
||||||
#判断同一列是否冲突
|
|
||||||
for i in range(len(board)):
|
|
||||||
if board[i][col] == 'Q':
|
|
||||||
return False
|
|
||||||
# 判断左上角是否冲突
|
|
||||||
i = row -1
|
|
||||||
j = col -1
|
|
||||||
while i>=0 and j>=0:
|
|
||||||
if board[i][j] == 'Q':
|
|
||||||
return False
|
|
||||||
i -= 1
|
|
||||||
j -= 1
|
|
||||||
# 判断右上角是否冲突
|
|
||||||
i = row - 1
|
|
||||||
j = col + 1
|
|
||||||
while i>=0 and j < len(board):
|
|
||||||
if board[i][j] == 'Q':
|
|
||||||
return False
|
|
||||||
i -= 1
|
|
||||||
j += 1
|
|
||||||
return True
|
|
||||||
|
|
||||||
def backtracking(board, row, n):
|
|
||||||
# 如果走到最后一行,说明已经找到一个解
|
|
||||||
if row == n:
|
|
||||||
temp_res = []
|
|
||||||
for temp in board:
|
|
||||||
temp_str = "".join(temp)
|
|
||||||
temp_res.append(temp_str)
|
|
||||||
res.append(temp_res)
|
|
||||||
for col in range(n):
|
|
||||||
if not isVaild(board, row, col):
|
|
||||||
continue
|
|
||||||
board[row][col] = 'Q'
|
|
||||||
backtracking(board, row+1, n)
|
|
||||||
board[row][col] = '.'
|
|
||||||
backtracking(board, 0, n)
|
|
||||||
return res
|
|
||||||
```
|
|
||||||
|
|
||||||
### Java
|
### Java
|
||||||
|
|
||||||
```java
|
```java
|
||||||
@ -341,6 +291,55 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Python
|
||||||
|
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def solveNQueens(self, n: int) -> List[List[str]]:
|
||||||
|
if not n: return []
|
||||||
|
board = [['.'] * n for _ in range(n)]
|
||||||
|
res = []
|
||||||
|
def isVaild(board,row, col):
|
||||||
|
#判断同一列是否冲突
|
||||||
|
for i in range(len(board)):
|
||||||
|
if board[i][col] == 'Q':
|
||||||
|
return False
|
||||||
|
# 判断左上角是否冲突
|
||||||
|
i = row -1
|
||||||
|
j = col -1
|
||||||
|
while i>=0 and j>=0:
|
||||||
|
if board[i][j] == 'Q':
|
||||||
|
return False
|
||||||
|
i -= 1
|
||||||
|
j -= 1
|
||||||
|
# 判断右上角是否冲突
|
||||||
|
i = row - 1
|
||||||
|
j = col + 1
|
||||||
|
while i>=0 and j < len(board):
|
||||||
|
if board[i][j] == 'Q':
|
||||||
|
return False
|
||||||
|
i -= 1
|
||||||
|
j += 1
|
||||||
|
return True
|
||||||
|
|
||||||
|
def backtracking(board, row, n):
|
||||||
|
# 如果走到最后一行,说明已经找到一个解
|
||||||
|
if row == n:
|
||||||
|
temp_res = []
|
||||||
|
for temp in board:
|
||||||
|
temp_str = "".join(temp)
|
||||||
|
temp_res.append(temp_str)
|
||||||
|
res.append(temp_res)
|
||||||
|
for col in range(n):
|
||||||
|
if not isVaild(board, row, col):
|
||||||
|
continue
|
||||||
|
board[row][col] = 'Q'
|
||||||
|
backtracking(board, row+1, n)
|
||||||
|
board[row][col] = '.'
|
||||||
|
backtracking(board, 0, n)
|
||||||
|
return res
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
### Go
|
### Go
|
||||||
```Go
|
```Go
|
||||||
@ -396,6 +395,8 @@ func isValid(n, row, col int, chessboard [][]string) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### Javascript
|
### Javascript
|
||||||
```Javascript
|
```Javascript
|
||||||
var solveNQueens = function(n) {
|
var solveNQueens = function(n) {
|
||||||
|
@ -187,12 +187,14 @@ func climbStairs(n int) int {
|
|||||||
JavaScript:
|
JavaScript:
|
||||||
```javascript
|
```javascript
|
||||||
var climbStairs = function(n) {
|
var climbStairs = function(n) {
|
||||||
const dp = new Array(n+1).fill(0);
|
const dp = new Array(n + 1).fill(0);
|
||||||
const weight = [1,2];
|
const m = 2;
|
||||||
dp[0] = 1;
|
dp[0] = 1;
|
||||||
for(let i = 0; i <= n; i++){ //先遍历背包
|
for(let i = 1; i <= n; i++){
|
||||||
for(let j = 0; j < weight.length; j++){ // 再遍历物品
|
for(let j = 1; j <= m; j++){
|
||||||
if(i >= weight[j]) dp[i] += dp[i-weight[j]];
|
if(i >= j) {
|
||||||
|
dp[i] += dp[i - j];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return dp[n];
|
return dp[n];
|
||||||
|
@ -20,7 +20,7 @@
|
|||||||
|
|
||||||
大家先回忆一下[77. 组合]给出的回溯法的代码:
|
大家先回忆一下[77. 组合]给出的回溯法的代码:
|
||||||
|
|
||||||
```c++
|
```CPP
|
||||||
class Solution {
|
class Solution {
|
||||||
private:
|
private:
|
||||||
vector<vector<int>> result; // 存放符合条件结果的集合
|
vector<vector<int>> result; // 存放符合条件结果的集合
|
||||||
@ -52,7 +52,7 @@ public:
|
|||||||
|
|
||||||
在遍历的过程中有如下代码:
|
在遍历的过程中有如下代码:
|
||||||
|
|
||||||
```c++
|
```CPP
|
||||||
for (int i = startIndex; i <= n; i++) {
|
for (int i = startIndex; i <= n; i++) {
|
||||||
path.push_back(i);
|
path.push_back(i);
|
||||||
backtracking(n, k, i + 1);
|
backtracking(n, k, i + 1);
|
||||||
@ -76,7 +76,7 @@ for (int i = startIndex; i <= n; i++) {
|
|||||||
**如果for循环选择的起始位置之后的元素个数 已经不足 我们需要的元素个数了,那么就没有必要搜索了**。
|
**如果for循环选择的起始位置之后的元素个数 已经不足 我们需要的元素个数了,那么就没有必要搜索了**。
|
||||||
|
|
||||||
注意代码中i,就是for循环里选择的起始位置。
|
注意代码中i,就是for循环里选择的起始位置。
|
||||||
```c++
|
```CPP
|
||||||
for (int i = startIndex; i <= n; i++) {
|
for (int i = startIndex; i <= n; i++) {
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -98,13 +98,13 @@ for (int i = startIndex; i <= n; i++) {
|
|||||||
|
|
||||||
所以优化之后的for循环是:
|
所以优化之后的for循环是:
|
||||||
|
|
||||||
```c++
|
```CPP
|
||||||
for (int i = startIndex; i <= n - (k - path.size()) + 1; i++) // i为本次搜索的起始位置
|
for (int i = startIndex; i <= n - (k - path.size()) + 1; i++) // i为本次搜索的起始位置
|
||||||
```
|
```
|
||||||
|
|
||||||
优化后整体代码如下:
|
优化后整体代码如下:
|
||||||
|
|
||||||
```c++
|
```CPP
|
||||||
class Solution {
|
class Solution {
|
||||||
private:
|
private:
|
||||||
vector<vector<int>> result;
|
vector<vector<int>> result;
|
||||||
|
@ -262,6 +262,43 @@ for (pair<string, int>target : targets[result[result.size() - 1]])
|
|||||||
|
|
||||||
### java
|
### java
|
||||||
|
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
private LinkedList<String> res;
|
||||||
|
private LinkedList<String> path = new LinkedList<>();
|
||||||
|
|
||||||
|
public List<String> findItinerary(List<List<String>> tickets) {
|
||||||
|
Collections.sort(tickets, (a, b) -> a.get(1).compareTo(b.get(1)));
|
||||||
|
path.add("JFK");
|
||||||
|
boolean[] used = new boolean[tickets.size()];
|
||||||
|
backTracking((ArrayList) tickets, used);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean backTracking(ArrayList<List<String>> tickets, boolean[] used) {
|
||||||
|
if (path.size() == tickets.size() + 1) {
|
||||||
|
res = new LinkedList(path);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < tickets.size(); i++) {
|
||||||
|
if (!used[i] && tickets.get(i).get(0).equals(path.getLast())) {
|
||||||
|
path.add(tickets.get(i).get(1));
|
||||||
|
used[i] = true;
|
||||||
|
|
||||||
|
if (backTracking(tickets, used)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
used[i] = false;
|
||||||
|
path.removeLast();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
```java
|
```java
|
||||||
class Solution {
|
class Solution {
|
||||||
private Deque<String> res;
|
private Deque<String> res;
|
||||||
|
@ -268,99 +268,102 @@ func topKFrequent(nums []int, k int) []int {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
javaScript:
|
JavaScript:
|
||||||
```js
|
```js
|
||||||
/**
|
// js 没有堆 需要自己构造
|
||||||
* @param {number[]} nums
|
class Heap {
|
||||||
* @param {number} k
|
constructor(compareFn) {
|
||||||
* @return {number[]}
|
this.compareFn = compareFn;
|
||||||
*/
|
this.queue = [];
|
||||||
var topKFrequent = function(nums, k) {
|
|
||||||
const map = new Map();
|
|
||||||
|
|
||||||
for(const num of nums) {
|
|
||||||
map.set(num, (map.get(num) || 0) + 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 创建小顶堆
|
|
||||||
const priorityQueue = new PriorityQueue((a, b) => a[1] - b[1]);
|
|
||||||
|
|
||||||
// entry 是一个长度为2的数组,0位置存储key,1位置存储value
|
|
||||||
for (const entry of map.entries()) {
|
|
||||||
priorityQueue.push(entry);
|
|
||||||
if (priorityQueue.size() > k) {
|
|
||||||
priorityQueue.pop();
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
const ret = [];
|
// 添加
|
||||||
|
push(item) {
|
||||||
|
// 推入元素
|
||||||
|
this.queue.push(item);
|
||||||
|
|
||||||
for(let i = priorityQueue.size() - 1; i >= 0; i--) {
|
// 上浮
|
||||||
ret[i] = priorityQueue.pop()[0];
|
let index = this.size() - 1; // 记录推入元素下标
|
||||||
}
|
let parent = Math.floor((index - 1) / 2); // 记录父节点下标
|
||||||
|
|
||||||
return ret;
|
while (parent >= 0 && this.compare(parent, index) > 0) { // 注意compare参数顺序
|
||||||
|
[this.queue[index], this.queue[parent]] = [this.queue[parent], this.queue[index]];
|
||||||
|
|
||||||
|
// 更新下标
|
||||||
|
index = parent;
|
||||||
|
parent = Math.floor((index - 1) / 2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取堆顶元素并移除
|
||||||
|
pop() {
|
||||||
|
// 堆顶元素
|
||||||
|
const out = this.queue[0];
|
||||||
|
|
||||||
|
// 移除堆顶元素 填入最后一个元素
|
||||||
|
this.queue[0] = this.queue.pop();
|
||||||
|
|
||||||
|
// 下沉
|
||||||
|
let index = 0; // 记录下沉元素下标
|
||||||
|
let left = 1; // left 是左子节点下标 left + 1 则是右子节点下标
|
||||||
|
let searchChild = this.compare(left, left + 1) > 0 ? left + 1 : left;
|
||||||
|
|
||||||
|
while (searchChild !== undefined && this.compare(index, searchChild) > 0) { // 注意compare参数顺序
|
||||||
|
[this.queue[index], this.queue[searchChild]] = [this.queue[searchChild], this.queue[index]];
|
||||||
|
|
||||||
|
// 更新下标
|
||||||
|
index = searchChild;
|
||||||
|
left = 2 * index + 1;
|
||||||
|
searchChild = this.compare(left, left + 1) > 0 ? left + 1 : left;
|
||||||
|
}
|
||||||
|
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
size() {
|
||||||
|
return this.queue.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 使用传入的 compareFn 比较两个位置的元素
|
||||||
|
compare(index1, index2) {
|
||||||
|
// 处理下标越界问题
|
||||||
|
if (this.queue[index1] === undefined) return 1;
|
||||||
|
if (this.queue[index2] === undefined) return -1;
|
||||||
|
|
||||||
|
return this.compareFn(this.queue[index1], this.queue[index2]);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
const topKFrequent = function (nums, k) {
|
||||||
|
const map = new Map();
|
||||||
|
|
||||||
|
for (const num of nums) {
|
||||||
|
map.set(num, (map.get(num) || 0) + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建小顶堆
|
||||||
|
const heap= new Heap((a, b) => a[1] - b[1]);
|
||||||
|
|
||||||
|
// entry 是一个长度为2的数组,0位置存储key,1位置存储value
|
||||||
|
for (const entry of map.entries()) {
|
||||||
|
heap.push(entry);
|
||||||
|
|
||||||
|
if (heap.size() > k) {
|
||||||
|
heap.pop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// return heap.queue.map(e => e[0]);
|
||||||
|
|
||||||
|
const res = [];
|
||||||
|
|
||||||
|
for (let i = heap.size() - 1; i >= 0; i--) {
|
||||||
|
res[i] = heap.pop()[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
function PriorityQueue(compareFn) {
|
|
||||||
this.compareFn = compareFn;
|
|
||||||
this.queue = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
// 添加
|
|
||||||
PriorityQueue.prototype.push = function(item) {
|
|
||||||
this.queue.push(item);
|
|
||||||
let index = this.queue.length - 1;
|
|
||||||
let parent = Math.floor((index - 1) / 2);
|
|
||||||
// 上浮
|
|
||||||
while(parent >= 0 && this.compare(parent, index) > 0) {
|
|
||||||
// 交换
|
|
||||||
[this.queue[index], this.queue[parent]] = [this.queue[parent], this.queue[index]];
|
|
||||||
index = parent;
|
|
||||||
parent = Math.floor((index - 1) / 2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取堆顶元素并移除
|
|
||||||
PriorityQueue.prototype.pop = function() {
|
|
||||||
const ret = this.queue[0];
|
|
||||||
|
|
||||||
// 把最后一个节点移到堆顶
|
|
||||||
this.queue[0] = this.queue.pop();
|
|
||||||
|
|
||||||
let index = 0;
|
|
||||||
// 左子节点下标,left + 1 就是右子节点下标
|
|
||||||
let left = 1;
|
|
||||||
let selectedChild = this.compare(left, left + 1) > 0 ? left + 1 : left;
|
|
||||||
|
|
||||||
// 下沉
|
|
||||||
while(selectedChild !== undefined && this.compare(index, selectedChild) > 0) {
|
|
||||||
// 交换
|
|
||||||
[this.queue[index], this.queue[selectedChild]] = [this.queue[selectedChild], this.queue[index]];
|
|
||||||
index = selectedChild;
|
|
||||||
left = 2 * index + 1;
|
|
||||||
selectedChild = this.compare(left, left + 1) > 0 ? left + 1 : left;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
PriorityQueue.prototype.size = function() {
|
|
||||||
return this.queue.length;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 使用传入的 compareFn 比较两个位置的元素
|
|
||||||
PriorityQueue.prototype.compare = function(index1, index2) {
|
|
||||||
if (this.queue[index1] === undefined) {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
if (this.queue[index2] === undefined) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.compareFn(this.queue[index1], this.queue[index2]);
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
TypeScript:
|
TypeScript:
|
||||||
|
@ -118,6 +118,27 @@ class Solution {
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 解法二
|
||||||
|
class Solution {
|
||||||
|
public int islandPerimeter(int[][] grid) {
|
||||||
|
// 计算岛屿的周长
|
||||||
|
// 方法二 : 遇到相邻的陆地总周长就-2
|
||||||
|
int landSum = 0; // 陆地数量
|
||||||
|
int cover = 0; // 相邻陆地数量
|
||||||
|
for (int i = 0; i < grid.length; i++) {
|
||||||
|
for (int j = 0; j < grid[0].length; j++) {
|
||||||
|
if (grid[i][j] == 1) {
|
||||||
|
landSum++;
|
||||||
|
// 统计上面和左边的相邻陆地
|
||||||
|
if(i - 1 >= 0 && grid[i-1][j] == 1) cover++;
|
||||||
|
if(j - 1 >= 0 && grid[i][j-1] == 1) cover++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return landSum * 4 - cover * 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
[力扣题目链接](https://leetcode.cn/problems/reverse-string-ii/)
|
[力扣题目链接](https://leetcode.cn/problems/reverse-string-ii/)
|
||||||
|
|
||||||
给定一个字符串 s 和一个整数 k,你需要对从字符串开头算起的每隔 2k 个字符的前 k 个字符进行反转。
|
给定一个字符串 s 和一个整数 k,从字符串开头算起, 每计数至 2k 个字符,就反转这 2k 个字符中的前 k 个字符。
|
||||||
|
|
||||||
如果剩余字符少于 k 个,则将剩余字符全部反转。
|
如果剩余字符少于 k 个,则将剩余字符全部反转。
|
||||||
|
|
||||||
|
@ -353,8 +353,12 @@ class MyLinkedList {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
size--;
|
size--;
|
||||||
|
if (index == 0) {
|
||||||
|
head = head.next;
|
||||||
|
return;
|
||||||
|
}
|
||||||
ListNode pred = head;
|
ListNode pred = head;
|
||||||
for (int i = 0; i < index; i++) {
|
for (int i = 0; i < index - 1; i++) {
|
||||||
pred = pred.next;
|
pred = pred.next;
|
||||||
}
|
}
|
||||||
pred.next = pred.next.next;
|
pred.next = pred.next.next;
|
||||||
|
Reference in New Issue
Block a user