mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Merge branch 'master' of github.com:flames519/leetcode-master
This commit is contained in:
@ -218,8 +218,33 @@ class Solution {
|
||||
```
|
||||
|
||||
Python:
|
||||
|
||||
|
||||
```Python
|
||||
class Solution:
|
||||
def threeSum(self, nums):
|
||||
ans = []
|
||||
n = len(nums)
|
||||
nums.sort()
|
||||
for i in range(n):
|
||||
left = i + 1
|
||||
right = n - 1
|
||||
if nums[i] > 0:
|
||||
break
|
||||
if i >= 1 and nums[i] == nums[i - 1]:
|
||||
continue
|
||||
while left < right:
|
||||
total = nums[i] + nums[left] + nums[right]
|
||||
if total > 0:
|
||||
right -= 1
|
||||
elif total < 0:
|
||||
left += 1
|
||||
else:
|
||||
ans.append([nums[i], nums[left], nums[right]])
|
||||
while left != right and nums[left] == nums[left + 1]: left += 1
|
||||
while left != right and nums[right] == nums[right - 1]: right -= 1
|
||||
left += 1
|
||||
right -= 1
|
||||
return ans
|
||||
```
|
||||
Go:
|
||||
```Go
|
||||
func threeSum(nums []int)[][]int{
|
||||
|
@ -168,7 +168,21 @@ class Solution {
|
||||
```
|
||||
|
||||
Python:
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
|
||||
if len(intervals) == 0: return intervals
|
||||
intervals.sort(key=lambda x: x[0])
|
||||
result = []
|
||||
result.append(intervals[0])
|
||||
for i in range(1, len(intervals)):
|
||||
last = result[-1]
|
||||
if last[1] >= intervals[i][0]:
|
||||
result[-1] = [last[0], max(last[1], intervals[i][1])]
|
||||
else:
|
||||
result.append(intervals[i])
|
||||
return result
|
||||
```
|
||||
|
||||
Go:
|
||||
|
||||
@ -179,4 +193,4 @@ Go:
|
||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||
|
@ -249,7 +249,24 @@ Python:
|
||||
|
||||
|
||||
Go:
|
||||
|
||||
```Go
|
||||
func uniquePaths(m int, n int) int {
|
||||
dp := make([][]int, m)
|
||||
for i := range dp {
|
||||
dp[i] = make([]int, n)
|
||||
dp[i][0] = 1
|
||||
}
|
||||
for j := 0; j < n; j++ {
|
||||
dp[0][j] = 1
|
||||
}
|
||||
for i := 1; i < m; i++ {
|
||||
for j := 1; j < n; j++ {
|
||||
dp[i][j] = dp[i-1][j] + dp[i][j-1]
|
||||
}
|
||||
}
|
||||
return dp[m-1][n-1]
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
@ -79,6 +79,35 @@ public:
|
||||
return result;
|
||||
}
|
||||
};
|
||||
```
|
||||
javascript代码:
|
||||
|
||||
```javascript
|
||||
var levelOrder = function(root) {
|
||||
//二叉树的层序遍历
|
||||
let res=[],queue=[];
|
||||
queue.push(root);
|
||||
if(root===null){
|
||||
return res;
|
||||
}
|
||||
while(queue.length!==0){
|
||||
// 记录当前层级节点数
|
||||
let length=queue.length;
|
||||
//存放每一层的节点
|
||||
let curLevel=[];
|
||||
for(let i=0;i<length;i++){
|
||||
let node=queue.shift();
|
||||
curLevel.push(node.val);
|
||||
// 存放当前层下一层的节点
|
||||
node.left&&queue.push(node.left);
|
||||
node.right&&queue.push(node.right);
|
||||
}
|
||||
//把每一层的结果放到结果数组
|
||||
res.push(curLevel);
|
||||
}
|
||||
return res;
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
**此时我们就掌握了二叉树的层序遍历了,那么如下五道leetcode上的题目,只需要修改模板的一两行代码(不能再多了),便可打倒!**
|
||||
@ -122,6 +151,30 @@ public:
|
||||
}
|
||||
};
|
||||
```
|
||||
javascript代码
|
||||
|
||||
```javascript
|
||||
var levelOrderBottom = function(root) {
|
||||
let res=[],queue=[];
|
||||
queue.push(root);
|
||||
while(queue.length&&root!==null){
|
||||
// 存放当前层级节点数组
|
||||
let curLevel=[];
|
||||
// 计算当前层级节点数量
|
||||
let length=queue.length;
|
||||
while(length--){
|
||||
let node=queue.shift();
|
||||
// 把当前层节点存入curLevel数组
|
||||
curLevel.push(node.val);
|
||||
// 把下一层级的左右节点存入queue队列
|
||||
node.left&&queue.push(node.left);
|
||||
node.right&&queue.push(node.right);
|
||||
}
|
||||
res.push(curLevel);
|
||||
}
|
||||
return res.reverse();
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
## 199.二叉树的右视图
|
||||
@ -159,6 +212,29 @@ public:
|
||||
}
|
||||
};
|
||||
```
|
||||
javascript代码:
|
||||
|
||||
```javascript
|
||||
var rightSideView = function(root) {
|
||||
//二叉树右视图 只需要把每一层最后一个节点存储到res数组
|
||||
let res=[],queue=[];
|
||||
queue.push(root);
|
||||
while(queue.length&&root!==null){
|
||||
// 记录当前层级节点个数
|
||||
let length=queue.length;
|
||||
while(length--){
|
||||
let node=queue.shift();
|
||||
//length长度为0的时候表明到了层级最后一个节点
|
||||
if(!length){
|
||||
res.push(node.val);
|
||||
}
|
||||
node.left&&queue.push(node.left);
|
||||
node.right&&queue.push(node.right);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
};
|
||||
```
|
||||
|
||||
## 637.二叉树的层平均值
|
||||
|
||||
@ -199,6 +275,31 @@ public:
|
||||
|
||||
```
|
||||
|
||||
javascript代码:
|
||||
|
||||
```javascript
|
||||
var averageOfLevels = function(root) {
|
||||
//层级平均值
|
||||
let res=[],queue=[];
|
||||
queue.push(root);
|
||||
while(queue.length&&root!==null){
|
||||
//每一层节点个数
|
||||
let length=queue.length;
|
||||
//sum记录每一层的和
|
||||
let sum=0;
|
||||
for(let i=0;i<length;i++){
|
||||
let node=queue.shift();
|
||||
sum+=node.val;
|
||||
node.left&&queue.push(node.left);
|
||||
node.right&&queue.push(node.right);
|
||||
}
|
||||
//每一层的平均值存入数组res
|
||||
res.push(sum/length);
|
||||
}
|
||||
return res;
|
||||
};
|
||||
```
|
||||
|
||||
## 429.N叉树的层序遍历
|
||||
|
||||
题目链接:https://leetcode-cn.com/problems/n-ary-tree-level-order-traversal/
|
||||
@ -250,6 +351,31 @@ public:
|
||||
};
|
||||
```
|
||||
|
||||
JavaScript代码:
|
||||
```JavaScript
|
||||
var levelOrder = function(root) {
|
||||
//每一层可能有2个以上,所以不再使用node.left node.right
|
||||
let res=[],queue=[];
|
||||
queue.push(root);
|
||||
while(queue.length&&root!==null){
|
||||
//记录每一层节点个数还是和二叉树一致
|
||||
let length=queue.length;
|
||||
//存放每层节点 也和二叉树一致
|
||||
let curLevel=[];
|
||||
while(length--){
|
||||
let node = queue.shift();
|
||||
curLevel.push(node.val);
|
||||
//这里不再是 ndoe.left node.right 而是循坏node.children
|
||||
for(let item of node.children){
|
||||
item&&queue.push(item);
|
||||
}
|
||||
}
|
||||
res.push(curLevel);
|
||||
}
|
||||
return res;
|
||||
};
|
||||
```
|
||||
|
||||
## 515.在每个树行中找最大值
|
||||
|
||||
题目链接:https://leetcode-cn.com/problems/find-largest-value-in-each-tree-row/
|
||||
@ -287,6 +413,29 @@ public:
|
||||
}
|
||||
};
|
||||
```
|
||||
javascript代码:
|
||||
|
||||
```javascript
|
||||
var largestValues = function(root) {
|
||||
//使用层序遍历
|
||||
let res=[],queue=[];
|
||||
queue.push(root);
|
||||
while(root!==null&&queue.length){
|
||||
//设置max初始值就是队列的第一个元素
|
||||
let max=queue[0];
|
||||
let length=queue.length;
|
||||
while(length--){
|
||||
let node = queue.shift();
|
||||
max=max>node.val?max:node.val;
|
||||
node.left&&queue.push(node.left);
|
||||
node.right&&queue.push(node.right);
|
||||
}
|
||||
//把每一层的最大值放到res数组
|
||||
res.push(max);
|
||||
}
|
||||
return res;
|
||||
};
|
||||
```
|
||||
|
||||
## 116.填充每个节点的下一个右侧节点指针
|
||||
|
||||
|
@ -233,7 +233,27 @@ class Solution {
|
||||
```
|
||||
|
||||
Python:
|
||||
|
||||
```python3
|
||||
# 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 sortedArrayToBST(self, nums: List[int]) -> TreeNode:
|
||||
def buildaTree(left,right):
|
||||
if left > right: return None #左闭右闭的区间,当区间 left > right的时候,就是空节点,当left = right的时候,不为空
|
||||
mid = left + (right - left) // 2 #保证数据不会越界
|
||||
val = nums[mid]
|
||||
root = TreeNode(val)
|
||||
root.left = buildaTree(left,mid - 1)
|
||||
root.right = buildaTree(mid + 1,right)
|
||||
return root
|
||||
root = buildaTree(0,len(nums) - 1) #左闭右闭区间
|
||||
return root
|
||||
```
|
||||
|
||||
Go:
|
||||
|
||||
@ -244,4 +264,4 @@ Go:
|
||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||
|
@ -172,6 +172,30 @@ Python:
|
||||
|
||||
|
||||
Go:
|
||||
```go
|
||||
func minSubArrayLen(target int, nums []int) int {
|
||||
i := 0
|
||||
l := len(nums) // 数组长度
|
||||
sum := 0 // 子数组之和
|
||||
result := l + 1 // 初始化返回长度为l+1,目的是为了判断“不存在符合条件的子数组,返回0”的情况
|
||||
for j := 0; j < l; j++ {
|
||||
sum += nums[j]
|
||||
for sum >= target {
|
||||
subLength := j - i + 1
|
||||
if subLength < result {
|
||||
result = subLength
|
||||
}
|
||||
sum -= nums[i]
|
||||
i++
|
||||
}
|
||||
}
|
||||
if result == l+1 {
|
||||
return 0
|
||||
} else {
|
||||
return result
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
JavaScript:
|
||||
@ -200,4 +224,4 @@ var minSubArrayLen = function(target, nums) {
|
||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||
|
@ -196,8 +196,26 @@ class Solution {
|
||||
```
|
||||
|
||||
Python:
|
||||
|
||||
|
||||
```python3
|
||||
# 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 convertBST(self, root: TreeNode) -> TreeNode:
|
||||
def buildalist(root):
|
||||
if not root: return None
|
||||
buildalist(root.right) #右中左遍历
|
||||
root.val += self.pre
|
||||
self.pre = root.val
|
||||
buildalist(root.left)
|
||||
self.pre = 0 #记录前一个节点的数值
|
||||
buildalist(root)
|
||||
return root
|
||||
```
|
||||
Go:
|
||||
|
||||
|
||||
@ -207,4 +225,4 @@ Go:
|
||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||
|
@ -265,8 +265,24 @@ class Solution {
|
||||
```
|
||||
|
||||
Python:
|
||||
|
||||
|
||||
```python3
|
||||
# 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 trimBST(self, root: TreeNode, low: int, high: int) -> TreeNode:
|
||||
if not root: return root
|
||||
if root.val < low:
|
||||
return self.trimBST(root.right,low,high) // 寻找符合区间[low, high]的节点
|
||||
if root.val > high:
|
||||
return self.trimBST(root.left,low,high) // 寻找符合区间[low, high]的节点
|
||||
root.left = self.trimBST(root.left,low,high) // root->left接入符合条件的左孩子
|
||||
root.right = self.trimBST(root.right,low,high) // root->right接入符合条件的右孩子
|
||||
return root
|
||||
```
|
||||
Go:
|
||||
|
||||
|
||||
@ -276,4 +292,4 @@ Go:
|
||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||
|
@ -8,6 +8,7 @@
|
||||
|
||||
|
||||
## 738.单调递增的数字
|
||||
题目链接: https://leetcode-cn.com/problems/monotone-increasing-digits/
|
||||
|
||||
给定一个非负整数 N,找出小于或等于 N 的最大的整数,同时这个整数需要满足其各个位数上的数字是单调递增。
|
||||
|
||||
@ -30,7 +31,7 @@
|
||||
|
||||
## 暴力解法
|
||||
|
||||
题意很简单,那么首先想的就是暴力解法了,来我提大家暴力一波,结果自然是超时!
|
||||
题意很简单,那么首先想的就是暴力解法了,来我替大家暴力一波,结果自然是超时!
|
||||
|
||||
代码如下:
|
||||
```C++
|
||||
@ -146,7 +147,19 @@ class Solution {
|
||||
|
||||
|
||||
Python:
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
def monotoneIncreasingDigits(self, n: int) -> int:
|
||||
strNum = list(str(n))
|
||||
flag = len(strNum)
|
||||
for i in range(len(strNum) - 1, 0, -1):
|
||||
if int(strNum[i]) < int(strNum[i - 1]):
|
||||
strNum[i - 1] = str(int(strNum[i - 1]) - 1)
|
||||
flag = i
|
||||
for i in range(flag, len(strNum)):
|
||||
strNum[i] = '9'
|
||||
return int("".join(strNum))
|
||||
```
|
||||
|
||||
Go:
|
||||
|
||||
@ -157,4 +170,4 @@ Go:
|
||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||
|
@ -108,7 +108,23 @@ class Solution {
|
||||
```
|
||||
|
||||
Python:
|
||||
```python
|
||||
class Solution:
|
||||
def partitionLabels(self, s: str) -> List[int]:
|
||||
hash = [0] * 26
|
||||
for i in range(len(s)):
|
||||
hash[ord(s[i]) - ord('a')] = i
|
||||
result = []
|
||||
left = 0
|
||||
right = 0
|
||||
for i in range(len(s)):
|
||||
right = max(right, hash[ord(s[i]) - ord('a')])
|
||||
if i == right:
|
||||
result.append(right - left + 1)
|
||||
left = i + 1
|
||||
return result
|
||||
|
||||
```
|
||||
|
||||
Go:
|
||||
|
||||
|
@ -242,7 +242,137 @@ Python:
|
||||
|
||||
|
||||
Go:
|
||||
> 前序遍历统一迭代法
|
||||
|
||||
```GO
|
||||
/**
|
||||
type Element struct {
|
||||
// 元素保管的值
|
||||
Value interface{}
|
||||
// 内含隐藏或非导出字段
|
||||
}
|
||||
|
||||
func (l *List) Back() *Element
|
||||
前序遍历:中左右
|
||||
压栈顺序:右左中
|
||||
**/
|
||||
func preorderTraversal(root *TreeNode) []int {
|
||||
if root == nil {
|
||||
return nil
|
||||
}
|
||||
var stack = list.New()//栈
|
||||
res:=[]int{}//结果集
|
||||
stack.PushBack(root)
|
||||
var node *TreeNode
|
||||
for stack.Len()>0{
|
||||
e := stack.Back()
|
||||
stack.Remove(e)//弹出元素
|
||||
if e.Value==nil{// 如果为空,则表明是需要处理中间节点
|
||||
e=stack.Back()//弹出元素(即中间节点)
|
||||
stack.Remove(e)//删除中间节点
|
||||
node=e.Value.(*TreeNode)
|
||||
res=append(res,node.Val)//将中间节点加入到结果集中
|
||||
continue//继续弹出栈中下一个节点
|
||||
}
|
||||
node = e.Value.(*TreeNode)
|
||||
//压栈顺序:右左中
|
||||
if node.Right!=nil{
|
||||
stack.PushBack(node.Right)
|
||||
}
|
||||
if node.Left!=nil{
|
||||
stack.PushBack(node.Left)
|
||||
}
|
||||
stack.PushBack(node)//中间节点压栈后再压入nil作为中间节点的标志符
|
||||
stack.PushBack(nil)
|
||||
}
|
||||
return res
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
> 中序遍历统一迭代法
|
||||
|
||||
```go
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
* Val int
|
||||
* Left *TreeNode
|
||||
* Right *TreeNode
|
||||
* }
|
||||
*/
|
||||
//中序遍历:左中右
|
||||
//压栈顺序:右中左
|
||||
func inorderTraversal(root *TreeNode) []int {
|
||||
if root==nil{
|
||||
return nil
|
||||
}
|
||||
stack:=list.New()//栈
|
||||
res:=[]int{}//结果集
|
||||
stack.PushBack(root)
|
||||
var node *TreeNode
|
||||
for stack.Len()>0{
|
||||
e := stack.Back()
|
||||
stack.Remove(e)
|
||||
if e.Value==nil{// 如果为空,则表明是需要处理中间节点
|
||||
e=stack.Back()//弹出元素(即中间节点)
|
||||
stack.Remove(e)//删除中间节点
|
||||
node=e.Value.(*TreeNode)
|
||||
res=append(res,node.Val)//将中间节点加入到结果集中
|
||||
continue//继续弹出栈中下一个节点
|
||||
}
|
||||
node = e.Value.(*TreeNode)
|
||||
//压栈顺序:右中左
|
||||
if node.Right!=nil{
|
||||
stack.PushBack(node.Right)
|
||||
}
|
||||
stack.PushBack(node)//中间节点压栈后再压入nil作为中间节点的标志符
|
||||
stack.PushBack(nil)
|
||||
if node.Left!=nil{
|
||||
stack.PushBack(node.Left)
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
```
|
||||
|
||||
> 后序遍历统一迭代法
|
||||
|
||||
```go
|
||||
//后续遍历:左右中
|
||||
//压栈顺序:中右左
|
||||
func postorderTraversal(root *TreeNode) []int {
|
||||
if root == nil {
|
||||
return nil
|
||||
}
|
||||
var stack = list.New()//栈
|
||||
res:=[]int{}//结果集
|
||||
stack.PushBack(root)
|
||||
var node *TreeNode
|
||||
for stack.Len()>0{
|
||||
e := stack.Back()
|
||||
stack.Remove(e)
|
||||
if e.Value==nil{// 如果为空,则表明是需要处理中间节点
|
||||
e=stack.Back()//弹出元素(即中间节点)
|
||||
stack.Remove(e)//删除中间节点
|
||||
node=e.Value.(*TreeNode)
|
||||
res=append(res,node.Val)//将中间节点加入到结果集中
|
||||
continue//继续弹出栈中下一个节点
|
||||
}
|
||||
node = e.Value.(*TreeNode)
|
||||
//压栈顺序:中右左
|
||||
stack.PushBack(node)//中间节点压栈后再压入nil作为中间节点的标志符
|
||||
stack.PushBack(nil)
|
||||
if node.Right!=nil{
|
||||
stack.PushBack(node.Right)
|
||||
}
|
||||
if node.Left!=nil{
|
||||
stack.PushBack(node.Left)
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
@ -96,10 +96,27 @@ public:
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
|
||||
Java:
|
||||
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
public String reverseLeftWords(String s, int n) {
|
||||
int len=s.length();
|
||||
StringBuilder sb=new StringBuilder(s);
|
||||
reverseString(sb,0,n-1);
|
||||
reverseString(sb,n,len-1);
|
||||
return sb.reverse().toString();
|
||||
}
|
||||
public void reverseString(StringBuilder sb, int start, int end) {
|
||||
while (start < end) {
|
||||
char temp = sb.charAt(start);
|
||||
sb.setCharAt(start, sb.charAt(end));
|
||||
sb.setCharAt(end, temp);
|
||||
start++;
|
||||
end--;
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
Python:
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user