mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Merge branch 'master' of https://github.com/youngyangyang04/leetcode-master
This commit is contained in:
@ -176,29 +176,26 @@ class Solution:
|
||||
```
|
||||
|
||||
Go:
|
||||
```Go
|
||||
```golang
|
||||
func merge(intervals [][]int) [][]int {
|
||||
sort.Slice(intervals, func(i, j int) bool {
|
||||
//先从小到大排序
|
||||
sort.Slice(intervals,func(i,j int)bool{
|
||||
return intervals[i][0]<intervals[j][0]
|
||||
})
|
||||
|
||||
res:=[][]int{}
|
||||
prev:=intervals[0]
|
||||
|
||||
for i:=1;i<len(intervals);i++{
|
||||
cur :=intervals[i]
|
||||
if prev[1]<cur[0]{
|
||||
res=append(res,prev)
|
||||
prev=cur
|
||||
}else {
|
||||
prev[1]=max(prev[1],cur[1])
|
||||
//再弄重复的
|
||||
for i:=0;i<len(intervals)-1;i++{
|
||||
if intervals[i][1]>=intervals[i+1][0]{
|
||||
intervals[i][1]=max(intervals[i][1],intervals[i+1][1])//赋值最大值
|
||||
intervals=append(intervals[:i+1],intervals[i+2:]...)
|
||||
i--
|
||||
}
|
||||
}
|
||||
res=append(res,prev)
|
||||
return res
|
||||
return intervals
|
||||
}
|
||||
func max(a, b int) int {
|
||||
if a > b { return a }
|
||||
func max(a,b int)int{
|
||||
if a>b{
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
```
|
||||
|
@ -103,7 +103,7 @@ if (root->val > root->left->val && root->val < root->right->val) {
|
||||
|
||||

|
||||
|
||||
节点10小于左节点5,大于右节点15,但右子树里出现了一个6 这就不符合了!
|
||||
节点10大于左节点5,小于右节点15,但右子树里出现了一个6 这就不符合了!
|
||||
|
||||
* 陷阱2
|
||||
|
||||
|
@ -1046,6 +1046,31 @@ class Solution:
|
||||
out_list.append(max(in_list))
|
||||
return out_list
|
||||
```
|
||||
java代码:
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
public List<Integer> largestValues(TreeNode root) {
|
||||
List<Integer> retVal = new ArrayList<Integer>();
|
||||
Queue<TreeNode> tmpQueue = new LinkedList<TreeNode>();
|
||||
if (root != null) tmpQueue.add(root);
|
||||
|
||||
while (tmpQueue.size() != 0){
|
||||
int size = tmpQueue.size();
|
||||
List<Integer> lvlVals = new ArrayList<Integer>();
|
||||
for (int index = 0; index < size; index++){
|
||||
TreeNode node = tmpQueue.poll();
|
||||
lvlVals.add(node.val);
|
||||
if (node.left != null) tmpQueue.add(node.left);
|
||||
if (node.right != null) tmpQueue.add(node.right);
|
||||
}
|
||||
retVal.add(Collections.max(lvlVals));
|
||||
}
|
||||
|
||||
return retVal;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
go:
|
||||
|
||||
|
@ -130,15 +130,97 @@ public:
|
||||
## Java
|
||||
|
||||
```java
|
||||
|
||||
// 递归法
|
||||
class Solution {
|
||||
public void traversal(Node cur) {
|
||||
if (cur == null) return;
|
||||
if (cur.left != null) cur.left.next = cur.right; // 操作1
|
||||
if (cur.right != null) {
|
||||
if(cur.next != null) cur.right.next = cur.next.left; //操作2
|
||||
else cur.right.next = null;
|
||||
}
|
||||
traversal(cur.left); // 左
|
||||
traversal(cur.right); //右
|
||||
}
|
||||
public Node connect(Node root) {
|
||||
traversal(root);
|
||||
return root;
|
||||
}
|
||||
}
|
||||
```
|
||||
```java
|
||||
// 迭代法
|
||||
class Solution {
|
||||
public Node connect(Node root) {
|
||||
if (root == null) return root;
|
||||
Queue<Node> que = new LinkedList<Node>();
|
||||
que.offer(root);
|
||||
Node nodePre = null;
|
||||
Node node = null;
|
||||
while (!que.isEmpty()) {
|
||||
int size = que.size();
|
||||
for (int i=0; i<size; i++) { // 开始每一层的遍历
|
||||
if (i == 0) {
|
||||
nodePre = que.peek(); // 记录一层的头结点
|
||||
que.poll();
|
||||
node = nodePre;
|
||||
} else {
|
||||
node = que.peek();
|
||||
que.poll();
|
||||
nodePre.next = node; // 本层前一个节点next指向本节点
|
||||
nodePre = nodePre.next;
|
||||
}
|
||||
if (node.left != null) que.offer(node.left);
|
||||
if (node.right != null) que.offer(node.right);
|
||||
}
|
||||
nodePre.next = null; // 本层最后一个节点指向null
|
||||
}
|
||||
return root;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Python
|
||||
|
||||
```python
|
||||
|
||||
# 递归法
|
||||
class Solution:
|
||||
def connect(self, root: 'Node') -> 'Node':
|
||||
def traversal(cur: 'Node') -> 'Node':
|
||||
if not cur: return []
|
||||
if cur.left: cur.left.next = cur.right # 操作1
|
||||
if cur.right:
|
||||
if cur.next:
|
||||
cur.right.next = cur.next.left # 操作2
|
||||
else:
|
||||
cur.right.next = None
|
||||
traversal(cur.left) # 左
|
||||
traversal(cur.right) # 右
|
||||
traversal(root)
|
||||
return root
|
||||
```
|
||||
```python
|
||||
# 迭代法
|
||||
class Solution:
|
||||
def connect(self, root: 'Node') -> 'Node':
|
||||
if not root: return
|
||||
res = []
|
||||
queue = [root]
|
||||
while queue:
|
||||
size = len(queue)
|
||||
for i in range(size): # 开始每一层的遍历
|
||||
if i==0:
|
||||
nodePre = queue.pop(0) # 记录一层的头结点
|
||||
node = nodePre
|
||||
else:
|
||||
node = queue.pop(0)
|
||||
nodePre.next = node # 本层前一个节点next指向本节点
|
||||
nodePre = nodePre.next
|
||||
if node.left: queue.append(node.left)
|
||||
if node.right: queue.append(node.right)
|
||||
nodePre.next = None # 本层最后一个节点指向None
|
||||
return root
|
||||
```
|
||||
|
||||
## Go
|
||||
|
||||
```go
|
||||
|
@ -216,7 +216,30 @@ class Solution: # 贪心思路
|
||||
```
|
||||
|
||||
Go:
|
||||
|
||||
```golang
|
||||
func maxProfit(prices []int, fee int) int {
|
||||
var minBuy int = prices[0] //第一天买入
|
||||
var res int
|
||||
for i:=0;i<len(prices);i++{
|
||||
//如果当前价格小于最低价,则在此处买入
|
||||
if prices[i]<minBuy{
|
||||
minBuy=prices[i]
|
||||
}
|
||||
//如果以当前价格卖出亏本,则不卖,继续找下一个可卖点
|
||||
if prices[i]>=minBuy&&prices[i]-fee-minBuy<=0{
|
||||
continue
|
||||
}
|
||||
//可以售卖了
|
||||
if prices[i]>minBuy+fee{
|
||||
//累加每天的收益
|
||||
res+=prices[i]-minBuy-fee
|
||||
//更新最小值(如果还在收获利润的区间里,表示并不是真正的卖出,而计算利润每次都要减去手续费,所以要让minBuy = prices[i] - fee;,这样在明天收获利润的时候,才不会多减一次手续费!)
|
||||
minBuy=prices[i]-fee
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
```
|
||||
Javascript:
|
||||
```Javascript
|
||||
// 贪心思路
|
||||
|
@ -159,7 +159,26 @@ class Solution:
|
||||
```
|
||||
|
||||
Go:
|
||||
|
||||
```golang
|
||||
func monotoneIncreasingDigits(N int) int {
|
||||
s := strconv.Itoa(N)//将数字转为字符串,方便使用下标
|
||||
ss := []byte(s)//将字符串转为byte数组,方便更改。
|
||||
n := len(ss)
|
||||
if n <= 1 {
|
||||
return N
|
||||
}
|
||||
for i:=n-1 ; i>0; i-- {
|
||||
if ss[i-1] > ss[i] {//前一个大于后一位,前一位减1,后面的全部置为9
|
||||
ss[i-1] -= 1
|
||||
for j := i ; j < n; j++ {//后面的全部置为9
|
||||
ss[j] = '9'
|
||||
}
|
||||
}
|
||||
}
|
||||
res, _ := strconv.Atoi(string(ss))
|
||||
return res
|
||||
}
|
||||
```
|
||||
Javascript:
|
||||
```Javascript
|
||||
var monotoneIncreasingDigits = function(n) {
|
||||
|
@ -8,7 +8,7 @@
|
||||
<p align="center"><strong>欢迎大家<a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
|
||||
|
||||
# 925.长按键入
|
||||
|
||||
题目链接:https://leetcode-cn.com/problems/long-pressed-name/
|
||||
你的朋友正在使用键盘输入他的名字 name。偶尔,在键入字符 c 时,按键可能会被长按,而字符可能被输入 1 次或多次。
|
||||
|
||||
你将会检查键盘输入的字符 typed。如果它对应的可能是你的朋友的名字(其中一些字符可能被长按),那么就返回 True。
|
||||
@ -100,7 +100,31 @@ public:
|
||||
Java:
|
||||
|
||||
Python:
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
def isLongPressedName(self, name: str, typed: str) -> bool:
|
||||
i, j = 0, 0
|
||||
m, n = len(name) , len(typed)
|
||||
while i< m and j < n:
|
||||
if name[i] == typed[j]: # 相同时向后匹配
|
||||
i += 1
|
||||
j += 1
|
||||
else: # 不相同
|
||||
if j == 0: return False # 如果第一位不相同,直接返回false
|
||||
# 判断边界为n-1,若为n会越界,例如name:"kikcxmvzi" typed:"kiikcxxmmvvzzz"
|
||||
while j < n - 1 and typed[j] == typed[j-1]: j += 1
|
||||
if name[i] == typed[j]:
|
||||
i += 1
|
||||
j += 1
|
||||
else: return False
|
||||
# 说明name没有匹配完
|
||||
if i < m: return False
|
||||
# 说明type没有匹配完
|
||||
while j < n:
|
||||
if typed[j] == typed[j-1]: j += 1
|
||||
else: return False
|
||||
return True
|
||||
```
|
||||
Go:
|
||||
|
||||
JavaScript:
|
||||
|
@ -55,7 +55,7 @@ private:
|
||||
vec.push_back(cur->val);
|
||||
traversal(cur->right);
|
||||
}
|
||||
有序数组转平衡二叉树
|
||||
// 有序数组转平衡二叉树
|
||||
TreeNode* getTree(vector<int>& nums, int left, int right) {
|
||||
if (left > right) return nullptr;
|
||||
int mid = left + ((right - left) / 2);
|
||||
@ -76,9 +76,53 @@ public:
|
||||
# 其他语言版本
|
||||
|
||||
Java:
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
ArrayList <Integer> res = new ArrayList<Integer>();
|
||||
// 有序树转成有序数组
|
||||
private void travesal(TreeNode cur) {
|
||||
if (cur == null) return;
|
||||
travesal(cur.left);
|
||||
res.add(cur.val);
|
||||
travesal(cur.right);
|
||||
}
|
||||
// 有序数组转成平衡二叉树
|
||||
private TreeNode getTree(ArrayList <Integer> nums, int left, int right) {
|
||||
if (left > right) return null;
|
||||
int mid = left + (right - left) / 2;
|
||||
TreeNode root = new TreeNode(nums.get(mid));
|
||||
root.left = getTree(nums, left, mid - 1);
|
||||
root.right = getTree(nums, mid + 1, right);
|
||||
return root;
|
||||
}
|
||||
public TreeNode balanceBST(TreeNode root) {
|
||||
travesal(root);
|
||||
return getTree(res, 0, res.size() - 1);
|
||||
}
|
||||
}
|
||||
```
|
||||
Python:
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
def balanceBST(self, root: TreeNode) -> TreeNode:
|
||||
res = []
|
||||
# 有序树转成有序数组
|
||||
def traversal(cur: TreeNode):
|
||||
if not cur: return
|
||||
traversal(cur.left)
|
||||
res.append(cur.val)
|
||||
traversal(cur.right)
|
||||
# 有序数组转成平衡二叉树
|
||||
def getTree(nums: List, left, right):
|
||||
if left > right: return
|
||||
mid = left + (right -left) // 2
|
||||
root = TreeNode(nums[mid])
|
||||
root.left = getTree(nums, left, mid - 1)
|
||||
root.right = getTree(nums, mid + 1, right)
|
||||
return root
|
||||
traversal(root)
|
||||
return getTree(res, 0, len(res) - 1)
|
||||
```
|
||||
Go:
|
||||
|
||||
JavaScript:
|
||||
|
@ -223,6 +223,14 @@ type TreeNode struct {
|
||||
}
|
||||
```
|
||||
|
||||
JavaScript:
|
||||
```
|
||||
function TreeNode(val, left, right) {
|
||||
this.val = (val===undefined ? 0 : val)
|
||||
this.left = (left===undefined ? null : left)
|
||||
this.right = (right===undefined ? null : right)
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
-----------------------
|
||||
|
Reference in New Issue
Block a user