mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 04:06:51 +08:00
Merge branch 'youngyangyang04:master' into master
This commit is contained in:
@ -234,6 +234,29 @@ class Solution:
|
||||
```
|
||||
|
||||
Go:
|
||||
```func detectCycle(head *ListNode) *ListNode {
|
||||
if head ==nil{
|
||||
return head
|
||||
}
|
||||
slow:=head
|
||||
fast:=head.Next
|
||||
|
||||
for fast!=nil&&fast.Next!=nil{
|
||||
if fast==slow{
|
||||
slow=head
|
||||
fast=fast.Next
|
||||
for fast!=slow {
|
||||
fast=fast.Next
|
||||
slow=slow.Next
|
||||
}
|
||||
return slow
|
||||
}
|
||||
fast=fast.Next.Next
|
||||
slow=slow.Next
|
||||
}
|
||||
return nil
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
-----------------------
|
||||
|
@ -94,7 +94,7 @@ void backtracking(int targetSum, int k, int sum, int startIndex)
|
||||
|
||||
所以 终止代码如下:
|
||||
|
||||
```
|
||||
```C++
|
||||
if (path.size() == k) {
|
||||
if (sum == targetSum) result.push_back(path);
|
||||
return; // 如果path.size() == k 但sum != targetSum 直接返回
|
||||
@ -112,7 +112,7 @@ if (path.size() == k) {
|
||||
|
||||
代码如下:
|
||||
|
||||
```
|
||||
```C++
|
||||
for (int i = startIndex; i <= 9; i++) {
|
||||
sum += i;
|
||||
path.push_back(i);
|
||||
@ -126,7 +126,7 @@ for (int i = startIndex; i <= 9; i++) {
|
||||
|
||||
参照[关于回溯算法,你该了解这些!](https://mp.weixin.qq.com/s/gjSgJbNbd1eAA5WkA-HeWw)中的模板,不难写出如下C++代码:
|
||||
|
||||
```
|
||||
```C++
|
||||
class Solution {
|
||||
private:
|
||||
vector<vector<int>> result; // 存放结果集
|
||||
|
@ -324,8 +324,31 @@ class Solution {
|
||||
```
|
||||
|
||||
Python:
|
||||
```Python
|
||||
# class TreeNode:
|
||||
# def __init__(self, val=0, left=None, right=None):
|
||||
# self.val = val
|
||||
# self.left = left
|
||||
# self.right = right
|
||||
class Solution:
|
||||
def binaryTreePaths(self, root: TreeNode) -> List[str]:
|
||||
path=[]
|
||||
res=[]
|
||||
def backtrace(root, path):
|
||||
if not root:return
|
||||
path.append(root.val)
|
||||
if (not root.left)and (not root.right):
|
||||
res.append(path[:])
|
||||
ways=[]
|
||||
if root.left:ways.append(root.left)
|
||||
if root.right:ways.append(root.right)
|
||||
for way in ways:
|
||||
backtrace(way,path)
|
||||
path.pop()
|
||||
backtrace(root,path)
|
||||
return ["->".join(list(map(str,i))) for i in res]
|
||||
|
||||
|
||||
```
|
||||
Go:
|
||||
|
||||
|
||||
|
@ -26,7 +26,7 @@ canConstruct("a", "b") -> false
|
||||
canConstruct("aa", "ab") -> false
|
||||
canConstruct("aa", "aab") -> true
|
||||
|
||||
# 思路
|
||||
## 思路
|
||||
|
||||
这道题目和[242.有效的字母异位词](https://mp.weixin.qq.com/s/vM6OszkM6L1Mx2Ralm9Dig)很像,[242.有效的字母异位词](https://mp.weixin.qq.com/s/vM6OszkM6L1Mx2Ralm9Dig)相当于求 字符串a 和 字符串b 是否可以相互组成 ,而这道题目是求 字符串a能否组成字符串b,而不用管字符串b 能不能组成字符串a。
|
||||
|
||||
@ -36,7 +36,7 @@ canConstruct("aa", "aab") -> true
|
||||
|
||||
* 第二点 “你可以假设两个字符串均只含有小写字母。” *说明只有小写字母*,这一点很重要
|
||||
|
||||
# 暴力解法
|
||||
## 暴力解法
|
||||
|
||||
那么第一个思路其实就是暴力枚举了,两层for循环,不断去寻找,代码如下:
|
||||
|
||||
@ -67,7 +67,7 @@ public:
|
||||
这里时间复杂度是比较高的,而且里面还有一个字符串删除也就是erase的操作,也是费时的,当然这段代码也可以过这道题。
|
||||
|
||||
|
||||
# 哈希解法
|
||||
## 哈希解法
|
||||
|
||||
因为题目所只有小写字母,那可以采用空间换取时间的哈希策略, 用一个长度为26的数组还记录magazine里字母出现的次数。
|
||||
|
||||
@ -105,8 +105,6 @@ public:
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
|
||||
|
@ -205,8 +205,25 @@ class Solution {
|
||||
|
||||
|
||||
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 sumOfLeftLeaves(self, root: TreeNode) -> int:
|
||||
self.res=0
|
||||
def areleftleaves(root):
|
||||
if not root:return
|
||||
if root.left and (not root.left.left) and (not root.left.right):self.res+=root.left.val
|
||||
areleftleaves(root.left)
|
||||
areleftleaves(root.right)
|
||||
areleftleaves(root)
|
||||
return self.res
|
||||
```
|
||||
Go:
|
||||
|
||||
|
||||
|
@ -211,7 +211,18 @@ class Solution {
|
||||
```
|
||||
|
||||
Python:
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
|
||||
people.sort(key=lambda x: (x[0], -x[1]), reverse=True)
|
||||
que = []
|
||||
for p in people:
|
||||
if p[1] > len(que):
|
||||
que.append(p)
|
||||
else:
|
||||
que.insert(p[1], p)
|
||||
return que
|
||||
```
|
||||
|
||||
Go:
|
||||
|
||||
|
@ -274,8 +274,28 @@ class Solution {
|
||||
|
||||
|
||||
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 findBottomLeftValue(self, root: TreeNode) -> int:
|
||||
depth=0
|
||||
self.res=[]
|
||||
def level(root,depth):
|
||||
if not root:return
|
||||
if depth==len(self.res):
|
||||
self.res.append([])
|
||||
self.res[depth].append(root.val)
|
||||
level(root.left,depth+1)
|
||||
level(root.right,depth+1)
|
||||
level(root,depth)
|
||||
return self.res[-1][0]
|
||||
```
|
||||
Go:
|
||||
|
||||
|
||||
|
@ -95,7 +95,48 @@ public:
|
||||
|
||||
|
||||
Java:
|
||||
```java
|
||||
/**
|
||||
* 卖出时支付手续费
|
||||
* @param prices
|
||||
* @param fee
|
||||
* @return
|
||||
*/
|
||||
public int maxProfit(int[] prices, int fee) {
|
||||
int len = prices.length;
|
||||
// 0 : 持股(买入)
|
||||
// 1 : 不持股(售出)
|
||||
// dp 定义第i天持股/不持股 所得最多现金
|
||||
int[][] dp = new int[len][2];
|
||||
dp[0][0] = -prices[0];
|
||||
for (int i = 1; i < len; i++) {
|
||||
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] - prices[i]);
|
||||
dp[i][1] = Math.max(dp[i - 1][0] + prices[i] - fee, dp[i - 1][1]);
|
||||
}
|
||||
return Math.max(dp[len - 1][0], dp[len - 1][1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 买入时支付手续费
|
||||
* @param prices
|
||||
* @param fee
|
||||
* @return
|
||||
*/
|
||||
public int maxProfit(int[] prices, int fee) {
|
||||
int len = prices.length;
|
||||
// 0 : 持股(买入)
|
||||
// 1 : 不持股(售出)
|
||||
// dp 定义第i天持股/不持股 所得最多现金
|
||||
int[][] dp = new int[len][2];
|
||||
// 考虑买入的时候就支付手续费
|
||||
dp[0][0] = -prices[0] - fee;
|
||||
for (int i = 1; i < len; i++) {
|
||||
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] - prices[i] - fee);
|
||||
dp[i][1] = Math.max(dp[i - 1][0] + prices[i], dp[i - 1][1]);
|
||||
}
|
||||
return Math.max(dp[len - 1][0], dp[len - 1][1]);
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
|
||||
|
@ -156,7 +156,30 @@ class Solution {
|
||||
```
|
||||
|
||||
Python:
|
||||
```python
|
||||
class Solution:
|
||||
def lemonadeChange(self, bills: List[int]) -> bool:
|
||||
five, ten, twenty = 0, 0, 0
|
||||
for bill in bills:
|
||||
if bill == 5:
|
||||
five += 1
|
||||
elif bill == 10:
|
||||
if five < 1: return False
|
||||
five -= 1
|
||||
ten += 1
|
||||
else:
|
||||
if ten > 0 and five > 0:
|
||||
ten -= 1
|
||||
five -= 1
|
||||
twenty += 1
|
||||
elif five > 2:
|
||||
five -= 3
|
||||
twenty += 1
|
||||
else:
|
||||
return False
|
||||
return True
|
||||
|
||||
```
|
||||
|
||||
Go:
|
||||
|
||||
|
@ -166,6 +166,34 @@ class Solution:
|
||||
|
||||
|
||||
Go:
|
||||
```Go
|
||||
func longestCommonSubsequence(text1 string, text2 string) int {
|
||||
t1 := len(text1)
|
||||
t2 := len(text2)
|
||||
dp:=make([][]int,t1+1)
|
||||
for i:=range dp{
|
||||
dp[i]=make([]int,t2+1)
|
||||
}
|
||||
|
||||
for i := 1; i <= t1; i++ {
|
||||
for j := 1; j <=t2; j++ {
|
||||
if text1[i-1]==text2[j-1]{
|
||||
dp[i][j]=dp[i-1][j-1]+1
|
||||
}else{
|
||||
dp[i][j]=max(dp[i-1][j],dp[i][j-1])
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[t1][t2]
|
||||
}
|
||||
|
||||
func max(a,b int)int {
|
||||
if a>b{
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
@ -92,7 +92,63 @@ public:
|
||||
|
||||
|
||||
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;
|
||||
ListNode curB = headB;
|
||||
int lenA = 0, lenB = 0;
|
||||
while (curA != null) { // 求链表A的长度
|
||||
lenA++;
|
||||
curA = curA.next;
|
||||
}
|
||||
while (curB != null) { // 求链表B的长度
|
||||
lenB++;
|
||||
curB = curB.next;
|
||||
}
|
||||
curA = headA;
|
||||
curB = headB;
|
||||
// 让curA为最长链表的头,lenA为其长度
|
||||
if (lenB > lenA) {
|
||||
//1. swap (lenA, lenB);
|
||||
int tmpLen = lenA;
|
||||
lenA = lenB;
|
||||
lenB = tmpLen;
|
||||
//2. swap (curA, curB);
|
||||
ListNode tmpNode = curA;
|
||||
curA = curB;
|
||||
curB = tmpNode;
|
||||
}
|
||||
// 求长度差
|
||||
int gap = lenA - lenB;
|
||||
// 让curA和curB在同一起点上(末尾位置对齐)
|
||||
while (gap-- > 0) {
|
||||
curA = curA.next;
|
||||
}
|
||||
// 遍历curA 和 curB,遇到相同则直接返回
|
||||
while (curA != null) {
|
||||
if (curA == curB) {
|
||||
return curA;
|
||||
}
|
||||
curA = curA.next;
|
||||
curB = curB.next;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
|
||||
|
Reference in New Issue
Block a user