Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from youngyangyang04:master #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 18 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Jun 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
abe0023
添加 0001.两数之和.md Scala版本
wzqwtt May 13, 2022
53379c0
添加 0454.四数相加II.md Scala版本
wzqwtt May 13, 2022
16c6abf
添加 0383.赎金信.md Scala版本
wzqwtt May 13, 2022
ca27111
添加(0714.买卖股票的最佳时机含手续费动态规划.md):增加typescript版本
xiaofei-2020 May 13, 2022
a53da7b
添加 0015.三数之和.md Scala版本
wzqwtt May 14, 2022
68eed4a
添加 0018.四数之和.md Scala版本
wzqwtt May 14, 2022
e4da60a
添加 0344.反转字符串.md Scala版本
wzqwtt May 14, 2022
f2dcdbe
添加 0541.反转字符串II.md Scala版本
wzqwtt May 14, 2022
037bebb
添加 剑指Offer05.替换空格.md Scala版本
wzqwtt May 14, 2022
1c369bb
102 in rust
3Xpl0it3r May 14, 2022
b8b62ff
树深度 rust实现
3Xpl0it3r May 18, 2022
9611896
Merge branch 'youngyangyang04:master' into master
3Xpl0it3r May 18, 2022
8c2737d
Merge branch 'master' into patch08
youngyangyang04 Jun 4, 2022
71a9111
Merge pull request #1326 from wzqwtt/patch08
youngyangyang04 Jun 4, 2022
b91d3c2
Merge pull request #1327 from xiaofei-2020/dp39
youngyangyang04 Jun 4, 2022
83726ac
Merge pull request #1328 from wzqwtt/patch09
youngyangyang04 Jun 4, 2022
dab44f5
Merge pull request #1329 from wzqwtt/patch10
youngyangyang04 Jun 4, 2022
9c32528
Merge pull request #1330 from 3Xpl0it3r/master
youngyangyang04 Jun 4, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions problems/0001.两数之和.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,30 @@ class Solution {
}
}
```

Scala:
```scala
object Solution {
// 导入包
import scala.collection.mutable
def twoSum(nums: Array[Int], target: Int): Array[Int] = {
// key存储值,value存储下标
val map = new mutable.HashMap[Int, Int]()
for (i <- nums.indices) {
val tmp = target - nums(i) // 计算差值
// 如果这个差值存在于map,则说明找到了结果
if (map.contains(tmp)) {
return Array(map.get(tmp).get, i)
}
// 如果不包含把当前值与其下标放到map
map.put(nums(i), i)
}
// 如果没有找到直接返回一个空的数组,return关键字可以省略
new Array[Int](2)
}
}
```

C#:
```csharp
public class Solution {
Expand All @@ -293,5 +317,6 @@ public class Solution {
}
```


-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
45 changes: 44 additions & 1 deletion problems/0015.三数之和.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,49 @@ public class Solution
}
}
```

Scala:
```scala
object Solution {
// 导包
import scala.collection.mutable.ListBuffer
import scala.util.control.Breaks.{break, breakable}

def threeSum(nums: Array[Int]): List[List[Int]] = {
// 定义结果集,最后需要转换为List
val res = ListBuffer[List[Int]]()
val nums_tmp = nums.sorted // 对nums进行排序
for (i <- nums_tmp.indices) {
// 如果要排的第一个数字大于0,直接返回结果
if (nums_tmp(i) > 0) {
return res.toList
}
// 如果i大于0并且和前一个数字重复,则跳过本次循环,相当于continue
breakable {
if (i > 0 && nums_tmp(i) == nums_tmp(i - 1)) {
break
} else {
var left = i + 1
var right = nums_tmp.length - 1
while (left < right) {
var sum = nums_tmp(i) + nums_tmp(left) + nums_tmp(right) // 求三数之和
if (sum < 0) left += 1
else if (sum > 0) right -= 1
else {
res += List(nums_tmp(i), nums_tmp(left), nums_tmp(right)) // 如果等于0 添加进结果集
// 为了避免重复,对left和right进行移动
while (left < right && nums_tmp(left) == nums_tmp(left + 1)) left += 1
while (left < right && nums_tmp(right) == nums_tmp(right - 1)) right -= 1
left += 1
right -= 1
}
}
}
}
}
// 最终返回需要转换为List,return关键字可以省略
res.toList
}
}
```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
45 changes: 44 additions & 1 deletion problems/0018.四数之和.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,49 @@ public class Solution
}
}
```

Scala:
```scala
object Solution {
// 导包
import scala.collection.mutable.ListBuffer
import scala.util.control.Breaks.{break, breakable}
def fourSum(nums: Array[Int], target: Int): List[List[Int]] = {
val res = ListBuffer[List[Int]]()
val nums_tmp = nums.sorted // 先排序
for (i <- nums_tmp.indices) {
breakable {
if (i > 0 && nums_tmp(i) == nums_tmp(i - 1)) {
break // 如果该值和上次的值相同,跳过本次循环,相当于continue
} else {
for (j <- i + 1 until nums_tmp.length) {
breakable {
if (j > i + 1 && nums_tmp(j) == nums_tmp(j - 1)) {
break // 同上
} else {
// 双指针
var (left, right) = (j + 1, nums_tmp.length - 1)
while (left < right) {
var sum = nums_tmp(i) + nums_tmp(j) + nums_tmp(left) + nums_tmp(right)
if (sum == target) {
// 满足要求,直接加入到集合里面去
res += List(nums_tmp(i), nums_tmp(j), nums_tmp(left), nums_tmp(right))
while (left < right && nums_tmp(left) == nums_tmp(left + 1)) left += 1
while (left < right && nums_tmp(right) == nums_tmp(right - 1)) right -= 1
left += 1
right -= 1
} else if (sum < target) left += 1
else right -= 1
}
}
}
}
}
}
}
// 最终返回的res要转换为List,return关键字可以省略
res.toList
}
}
```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
59 changes: 59 additions & 0 deletions problems/0102.二叉树的层序遍历.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,36 @@ func levelOrder(_ root: TreeNode?) -> [[Int]] {
}
```

Rust:

```rust
pub fn level_order(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<Vec<i32>> {
let mut ans = Vec::new();
let mut stack = Vec::new();
if root.is_none(){
return ans;
}
stack.push(root.unwrap());
while stack.is_empty()!= true{
let num = stack.len();
let mut level = Vec::new();
for _i in 0..num{
let tmp = stack.remove(0);
level.push(tmp.borrow_mut().val);
if tmp.borrow_mut().left.is_some(){
stack.push(tmp.borrow_mut().left.take().unwrap());
}
if tmp.borrow_mut().right.is_some(){
stack.push(tmp.borrow_mut().right.take().unwrap());
}
}
ans.push(level);
}
ans
}
```


**此时我们就掌握了二叉树的层序遍历了,那么如下九道力扣上的题目,只需要修改模板的两三行代码(不能再多了),便可打倒!**


Expand Down Expand Up @@ -548,6 +578,35 @@ func levelOrderBottom(_ root: TreeNode?) -> [[Int]] {
}
```

Rust:

```rust
pub fn level_order(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<Vec<i32>> {
let mut ans = Vec::new();
let mut stack = Vec::new();
if root.is_none(){
return ans;
}
stack.push(root.unwrap());
while stack.is_empty()!= true{
let num = stack.len();
let mut level = Vec::new();
for _i in 0..num{
let tmp = stack.remove(0);
level.push(tmp.borrow_mut().val);
if tmp.borrow_mut().left.is_some(){
stack.push(tmp.borrow_mut().left.take().unwrap());
}
if tmp.borrow_mut().right.is_some(){
stack.push(tmp.borrow_mut().right.take().unwrap());
}
}
ans.push(level);
}
ans
}
```

# 199.二叉树的右视图

[力扣题目链接](https://leetcode-cn.com/problems/binary-tree-right-side-view/)
Expand Down
27 changes: 27 additions & 0 deletions problems/0104.二叉树的最大深度.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,33 @@ public:
};
```

rust:
```rust
impl Solution {
pub fn max_depth(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
if root.is_none(){
return 0;
}
let mut max_depth: i32 = 0;
let mut stack = vec![root.unwrap()];
while !stack.is_empty() {
let num = stack.len();
for _i in 0..num{
let top = stack.remove(0);
if top.borrow_mut().left.is_some(){
stack.push(top.borrow_mut().left.take().unwrap());
}
if top.borrow_mut().right.is_some(){
stack.push(top.borrow_mut().right.take().unwrap());
}
}
max_depth+=1;
}
max_depth
}
```


那么我们可以顺便解决一下n叉树的最大深度问题

# 559.n叉树的最大深度
Expand Down
64 changes: 64 additions & 0 deletions problems/0111.二叉树的最小深度.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -488,5 +488,69 @@ func minDepth(_ root: TreeNode?) -> Int {
}
```

rust:
```rust
impl Solution {
pub fn min_depth(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
return Solution::bfs(root)
}

// 递归
pub fn dfs(node: Option<Rc<RefCell<TreeNode>>>) -> i32{
if node.is_none(){
return 0;
}
let parent = node.unwrap();
let left_child = parent.borrow_mut().left.take();
let right_child = parent.borrow_mut().right.take();
if left_child.is_none() && right_child.is_none(){
return 1;
}
let mut min_depth = i32::MAX;
if left_child.is_some(){
let left_depth = Solution::dfs(left_child);
if left_depth <= min_depth{
min_depth = left_depth
}
}
if right_child.is_some(){
let right_depth = Solution::dfs(right_child);
if right_depth <= min_depth{
min_depth = right_depth
}
}
min_depth + 1

}

// 迭代
pub fn bfs(node: Option<Rc<RefCell<TreeNode>>>) -> i32{
let mut min_depth = 0;
if node.is_none(){
return min_depth
}
let mut stack = vec![node.unwrap()];
while !stack.is_empty(){
min_depth += 1;
let num = stack.len();
for _i in 0..num{
let top = stack.remove(0);
let left_child = top.borrow_mut().left.take();
let right_child = top.borrow_mut().right.take();
if left_child.is_none() && right_child.is_none(){
return min_depth;
}
if left_child.is_some(){
stack.push(left_child.unwrap());
}
if right_child.is_some(){
stack.push(right_child.unwrap());
}
}
}
min_depth
}
```

-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
16 changes: 15 additions & 1 deletion problems/0344.反转字符串.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,20 @@ public class Solution
}
}
```

Scala:
```scala
object Solution {
def reverseString(s: Array[Char]): Unit = {
var (left, right) = (0, s.length - 1)
while (left < right) {
var tmp = s(left)
s(left) = s(right)
s(right) = tmp
left += 1
right -= 1
}
}
}
```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
Loading

AltStyle によって変換されたページ (->オリジナル) /