This commit is contained in:
youngyangyang04
2021-12-20 22:44:15 +08:00
parent 0df748cd9a
commit 1c6ad04346
10 changed files with 100 additions and 111 deletions

View File

@ -22,7 +22,7 @@
<a href="https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ"><img src="https://img.shields.io/badge/知识星球-代码随想录-blue" alt=""></a>
</p>
<p align="center"><strong>《代码随想录》正式出版啦!!录友专属福利,点击下方可以享五折优惠!详细可以<a href="programmercarl.com/other/publish.html">点击这里</a></strong></p>
<p align="center"><strong>《代码随想录》正式出版啦!!录友专属福利,点击下方可以享五折优惠!详细可以<a href="https://programmercarl.com/other/publish.html">点击这里</a></strong></p>
<p align="center">
<a href="https://union-click.jd.com/jdc?e=&p=JF8BAMQJK1olXg8EUVhVCkkWAV8IGV8WVAICU24ZVxNJXF9RXh5UHw0cSgYYXBcIWDoXSQVJQwYAUF1UDEsQHDZNRwYlX0B9A1cfakpyYBkSRj4QKFBUEEAfaEcbM244GFIXWQYAUV5VOHsXBF9edVsUXAcDVVtdDUgnAl8IHFkdXw8KUl5fDkgRM2gIEmtIFVpKAxVtOHsUM184G2sWbURsVApfAR8XA2sLSw8cWA8LUw1ZCElHAmhdTAxGW1YBUlxtCkoWB2Y4" target="_blank">

View File

@ -55,7 +55,7 @@ public:
for (int i = 0; i < nums.size(); i++) {
// 排序之后如果第一个元素已经大于零,那么不可能凑成三元组
if (nums[i] > 0) {
continue;
break;
}
if (i > 0 && nums[i] == nums[i - 1]) { //三元组元素a去重
continue;

View File

@ -91,7 +91,8 @@ public:
// nums[k] + nums[i] + nums[left] + nums[right] > target 会溢出
if (nums[k] + nums[i] > target - (nums[left] + nums[right])) {
right--;
} else if (nums[k] + nums[i] + nums[left] + nums[right] < target) {
// nums[k] + nums[i] + nums[left] + nums[right] < target 会溢出
} else if (nums[k] + nums[i] < target - (nums[left] + nums[right])) {
left++;
} else {
result.push_back(vector<int>{nums[k], nums[i], nums[left], nums[right]});
@ -111,6 +112,7 @@ public:
}
};
```

View File

@ -5,22 +5,22 @@
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
## 56. 合并区间
# 56. 合并区间
[力扣题目链接](https://leetcode-cn.com/problems/merge-intervals/)
给出一个区间的集合,请合并所有重叠的区间。
示例 1:
输入: intervals = [[1,3],[2,6],[8,10],[15,18]]
输出: [[1,6],[8,10],[15,18]]
解释: 区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6].
* 输入: intervals = [[1,3],[2,6],[8,10],[15,18]]
* 输出: [[1,6],[8,10],[15,18]]
* 解释: 区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6].
示例 2:
输入: intervals = [[1,4],[4,5]]
输出: [[1,5]]
解释: 区间 [1,4] 和 [4,5] 可被视为重叠区间。
注意输入类型已于2019年4月15日更改。 请重置默认代码定义以获取新方法签名。
* 输入: intervals = [[1,4],[4,5]]
* 输出: [[1,5]]
* 解释: 区间 [1,4] 和 [4,5] 可被视为重叠区间。
* 注意输入类型已于2019年4月15日更改。 请重置默认代码定义以获取新方法签名。
提示:
@ -134,7 +134,7 @@ public:
## 其他语言版本
Java
### Java
```java
class Solution {
public int[][] merge(int[][] intervals) {
@ -178,7 +178,7 @@ class Solution {
}
```
Python
### Python
```python
class Solution:
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
@ -195,7 +195,7 @@ class Solution:
return result
```
Go
### Go
```golang
func merge(intervals [][]int) [][]int {
//先从小到大排序
@ -220,7 +220,7 @@ func max(a,b int)int{
}
```
Javascript
### Javascript
```javascript
var merge = function (intervals) {
intervals.sort((a, b) => a[0] - b[0]);

View File

@ -5,7 +5,7 @@
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
## 435. 无重叠区间
# 435. 无重叠区间
[力扣题目链接](https://leetcode-cn.com/problems/non-overlapping-intervals/)
@ -16,19 +16,19 @@
区间 [1,2] 和 [2,3] 的边界相互“接触”,但没有相互重叠。
示例 1:
输入: [ [1,2], [2,3], [3,4], [1,3] ]
输出: 1
解释: 移除 [1,3] 后,剩下的区间没有重叠。
* 输入: [ [1,2], [2,3], [3,4], [1,3] ]
* 输出: 1
* 解释: 移除 [1,3] 后,剩下的区间没有重叠。
示例 2:
输入: [ [1,2], [1,2], [1,2] ]
输出: 2
解释: 你需要移除两个 [1,2] 来使剩下的区间没有重叠。
* 输入: [ [1,2], [1,2], [1,2] ]
* 输出: 2
* 解释: 你需要移除两个 [1,2] 来使剩下的区间没有重叠。
示例 3:
输入: [ [1,2], [2,3] ]
输出: 0
解释: 你不需要移除任何区间,因为它们已经是无重叠的了。
* 输入: [ [1,2], [2,3] ]
* 输出: 0
* 解释: 你不需要移除任何区间,因为它们已经是无重叠的了。
## 思路
@ -179,7 +179,7 @@ public:
## 其他语言版本
Java
### Java
```java
class Solution {
public int eraseOverlapIntervals(int[][] intervals) {
@ -209,7 +209,6 @@ class Solution {
}
```
Java:
按左边排序,不管右边顺序。相交的时候取最小的右边。
```java
class Solution {
@ -232,7 +231,7 @@ class Solution {
}
```
Python
### Python
```python
class Solution:
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
@ -247,7 +246,7 @@ class Solution:
return len(intervals) - count
```
Go
### Go
```golang
func eraseOverlapIntervals(intervals [][]int) int {
var flag int
@ -271,7 +270,8 @@ func min(a,b int)int{
return a
}
```
Javascript:
### Javascript:
- 按右边界排序
```Javascript
var eraseOverlapIntervals = function(intervals) {

View File

@ -5,7 +5,7 @@
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
## 714. 买卖股票的最佳时机含手续费
# 714. 买卖股票的最佳时机含手续费
[力扣题目链接](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/)

View File

@ -5,7 +5,7 @@
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
## 738.单调递增的数字
# 738.单调递增的数字
[力扣题目链接](https://leetcode-cn.com/problems/monotone-increasing-digits/)
给定一个非负整数 N找出小于或等于 N 的最大的整数同时这个整数需要满足其各个位数上的数字是单调递增。
@ -13,16 +13,16 @@
当且仅当每个相邻位数上的数字 x  y 满足 x <= y 我们称这个整数是单调递增的。
示例 1:
输入: N = 10
输出: 9
* 输入: N = 10
* 输出: 9
示例 2:
输入: N = 1234
输出: 1234
* 输入: N = 1234
* 输出: 1234
示例 3:
输入: N = 332
输出: 299
* 输入: N = 332
* 输出: 299
说明: N 是在 [0, 10^9] 范围内的一个整数。
@ -123,7 +123,7 @@ public:
## 其他语言版本
Java
### Java
```java
版本1
class Solution {
@ -170,8 +170,8 @@ class Solution {
```
Python
```python3
### Python
```python
class Solution:
def monotoneIncreasingDigits(self, n: int) -> int:
a = list(str(n))
@ -182,7 +182,7 @@ class Solution:
return int("".join(a))
```
Go
### Go
```golang
func monotoneIncreasingDigits(N int) int {
s := strconv.Itoa(N)//将数字转为字符串,方便使用下标
@ -203,7 +203,8 @@ func monotoneIncreasingDigits(N int) int {
return res
}
```
Javascript:
### Javascript
```Javascript
var monotoneIncreasingDigits = function(n) {
n = n.toString()

View File

@ -5,15 +5,15 @@
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
## 763.划分字母区间
# 763.划分字母区间
[力扣题目链接](https://leetcode-cn.com/problems/partition-labels/)
字符串 S 由小写字母组成。我们要把这个字符串划分为尽可能多的片段,同一字母最多出现在一个片段中。返回一个表示每个字符串片段的长度的列表。
示例:
输入S = "ababcbacadefegdehijhklij"
输出:[9,7,8]
* 输入S = "ababcbacadefegdehijhklij"
* 输出:[9,7,8]
解释:
划分结果为 "ababcbaca", "defegde", "hijhklij"。
每个字母最多出现在一个片段中。
@ -81,7 +81,7 @@ public:
## 其他语言版本
Java
### Java
```java
class Solution {
public List<Integer> partitionLabels(String S) {
@ -105,7 +105,7 @@ class Solution {
}
```
Python
### Python
```python
class Solution:
def partitionLabels(self, s: str) -> List[int]:
@ -124,7 +124,7 @@ class Solution:
```
Go
### Go
```go
@ -153,7 +153,7 @@ func max(a, b int) int {
}
```
Javascript:
### Javascript
```Javascript
var partitionLabels = function(s) {
let hash = {}

View File

@ -5,7 +5,7 @@
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
## 968.监控二叉树
# 968.监控二叉树
[力扣题目链接](https://leetcode-cn.com/problems/binary-tree-cameras/)
@ -19,17 +19,17 @@
![](https://img-blog.csdnimg.cn/20201229175736596.png)
输入:[0,0,null,0,0]
输出1
解释:如图所示,一台摄像头足以监控所有节点。
* 输入:[0,0,null,0,0]
* 输出1
* 解释:如图所示,一台摄像头足以监控所有节点。
示例 2
![](https://img-blog.csdnimg.cn/2020122917584449.png)
输入:[0,0,null,0,null,0,null,null,0]
输出2
解释:需要至少两个摄像头来监视树的所有节点。 上图显示了摄像头放置的有效位置之一。
* 输入:[0,0,null,0,null,0,null,null,0]
* 输出2
* 解释:需要至少两个摄像头来监视树的所有节点。 上图显示了摄像头放置的有效位置之一。
提示:
@ -212,7 +212,7 @@ int minCameraCover(TreeNode* root) {
**以下我的代码注释很详细,为了把情况说清楚,特别把每种情况列出来。**
## C++代码
C++代码如下:
```CPP
// 版本一
@ -313,7 +313,7 @@ public:
## 其他语言版本
Java
### Java
```java
class Solution {
private int count = 0;
@ -343,14 +343,8 @@ class Solution {
```
Python
### Python
```python
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def minCameraCover(self, root: TreeNode) -> int:
# Greedy Algo:
@ -398,7 +392,8 @@ class Solution:
return result
```
Go
### Go
```go
const inf = math.MaxInt64 / 2
@ -427,7 +422,8 @@ func min(a, b int) int {
}
```
Javascript:
### Javascript
```Javascript
var minCameraCover = function(root) {
let result = 0
@ -464,7 +460,7 @@ var minCameraCover = function(root) {
};
```
C:
### C
```c
/*
**函数后序遍历二叉树。判断一个结点状态时,根据其左右孩子结点的状态进行判断

View File

@ -5,24 +5,35 @@
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
## 面试题 02.07. 链表相交
# 面试题 02.07. 链表相交
[力扣题目链接](https://leetcode-cn.com/problems/intersection-of-two-linked-lists-lcci/)
两个(单向)链表,判定它们是否相交并返回交点。请注意相交的定义基于节点的引用,而不是基于节点的值。换句话说,如果个链表的第k个节点与另一个链表的第j个节点是同一节点引用完全相同则这两个链表相交
两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果个链表没有交点,返回 null
图示两个链表在节点 c1 开始相交:
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221657.png)
题目数据 保证 整个链式结构中不存在环。
注意,函数返回结果后,链表必须 保持其原始结构 。
示例 1
输入listA = [4,1,8,4,5], listB = [5,0,1,8,4,5]
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221723.png)
输出Reference of the node with value = 8
示例 2
输入解释:相交节点的值为 8 (注意,如果两个列表相交则不能为 0。从各自的表头开始算起链表 A 为 [4,1,8,4,5],链表 B 为 [5,0,1,8,4,5]。在 A 中,相交节点前有 2 个节点;在 B 中,相交节点前有 3 个节点。
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221749.png)
示例 3
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221812.png)![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221812.png)
## 思路
本来很简洁明了的一道题,让题目描述搞的云里雾里的。
简单来说,就是求两个链表交点节点的**指针**。 这里同学们要注意,交点不是数值相等,而是指针相等。
@ -89,19 +100,8 @@ public:
## 其他语言版本
Java
### Java
```Java
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode curA = headA;
@ -148,13 +148,8 @@ public class Solution {
}
```
Python
### Python
```python
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
@ -175,7 +170,7 @@ class Solution:
return cur_a
```
Go
### Go
```go
func getIntersectionNode(headA, headB *ListNode) *ListNode {
@ -213,14 +208,9 @@ func getIntersectionNode(headA, headB *ListNode) *ListNode {
}
```
javaScript:
### javaScript
```js
/**
* @param {ListNode} headA
* @param {ListNode} headB
* @return {ListNode}
*/
var getListLen = function(head) {
let len = 0, cur = head;
while(cur) {