diff --git a/problems/0001.两数之和.md b/problems/0001.两数之和.md index 9571a773..6969c2e2 100644 --- a/problems/0001.两数之和.md +++ b/problems/0001.两数之和.md @@ -275,5 +275,48 @@ 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 { + public int[] TwoSum(int[] nums, int target) { + Dictionary dic= new Dictionary(); + for(int i=0;i diff --git a/problems/0015.三数之和.md b/problems/0015.三数之和.md index cc184c87..1764d244 100644 --- a/problems/0015.三数之和.md +++ b/problems/0015.三数之和.md @@ -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 + } +} +``` -----------------------
diff --git a/problems/0018.四数之和.md b/problems/0018.四数之和.md index ee70cb69..6cbd40c2 100644 --- a/problems/0018.四数之和.md +++ b/problems/0018.四数之和.md @@ -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 + } +} +``` -----------------------
diff --git a/problems/0019.删除链表的倒数第N个节点.md b/problems/0019.删除链表的倒数第N个节点.md index 813e9b02..c36900bc 100644 --- a/problems/0019.删除链表的倒数第N个节点.md +++ b/problems/0019.删除链表的倒数第N个节点.md @@ -39,7 +39,7 @@ 分为如下几步: -* 首先这里我推荐大家使用虚拟头结点,这样方面处理删除实际头结点的逻辑,如果虚拟头结点不清楚,可以看这篇: [链表:听说用虚拟头节点会方便很多?](https://programmercarl.com/0203.移除链表元素.html) +* 首先这里我推荐大家使用虚拟头结点,这样方便处理删除实际头结点的逻辑,如果虚拟头结点不清楚,可以看这篇: [链表:听说用虚拟头节点会方便很多?](https://programmercarl.com/0203.移除链表元素.html) * 定义fast指针和slow指针,初始值为虚拟头结点,如图: @@ -289,6 +289,28 @@ func removeNthFromEnd(_ head: ListNode?, _ n: Int) -> ListNode? { return dummyHead.next } ``` - +Scala: +```scala +object Solution { + def removeNthFromEnd(head: ListNode, n: Int): ListNode = { + val dummy = new ListNode(-1, head) // 定义虚拟头节点 + var fast = head // 快指针从头开始走 + var slow = dummy // 慢指针从虚拟头开始头 + // 因为参数 n 是不可变量,所以不能使用 while(n>0){n-=1}的方式 + for (i <- 0 until n) { + fast = fast.next + } + // 快指针和满指针一起走,直到fast走到null + while (fast != null) { + slow = slow.next + fast = fast.next + } + // 删除slow的下一个节点 + slow.next = slow.next.next + // 返回虚拟头节点的下一个 + dummy.next + } +} +``` -----------------------
diff --git a/problems/0020.有效的括号.md b/problems/0020.有效的括号.md index 7bb7f746..a0df0d07 100644 --- a/problems/0020.有效的括号.md +++ b/problems/0020.有效的括号.md @@ -400,6 +400,27 @@ bool isValid(char * s){ return !stackTop; } ``` - +Scala: +```scala +object Solution { + import scala.collection.mutable + def isValid(s: String): Boolean = { + if(s.length % 2 != 0) return false // 如果字符串长度是奇数直接返回false + val stack = mutable.Stack[Char]() + // 循环遍历字符串 + for (i <- s.indices) { + val c = s(i) + if (c == '(' || c == '[' || c == '{') stack.push(c) + else if(stack.isEmpty) return false // 如果没有(、[、{则直接返回false + // 以下三种情况,不满足则直接返回false + else if(c==')' && stack.pop() != '(') return false + else if(c==']' && stack.pop() != '[') return false + else if(c=='}' && stack.pop() != '{') return false + } + // 如果为空则正确匹配,否则还有余孽就不匹配 + stack.isEmpty + } +} +``` -----------------------
diff --git a/problems/0024.两两交换链表中的节点.md b/problems/0024.两两交换链表中的节点.md index ce75e0d7..2289c229 100644 --- a/problems/0024.两两交换链表中的节点.md +++ b/problems/0024.两两交换链表中的节点.md @@ -311,7 +311,29 @@ func swapPairs(_ head: ListNode?) -> ListNode? { return dummyHead.next } ``` - +Scala: +```scala +// 虚拟头节点 +object Solution { + def swapPairs(head: ListNode): ListNode = { + var dummy = new ListNode(0, head) // 虚拟头节点 + var pre = dummy + var cur = head + // 当pre的下一个和下下个都不为空,才进行两两转换 + while (pre.next != null && pre.next.next != null) { + var tmp: ListNode = cur.next.next // 缓存下一次要进行转换的第一个节点 + pre.next = cur.next // 步骤一 + cur.next.next = cur // 步骤二 + cur.next = tmp // 步骤三 + // 下面是准备下一轮的交换 + pre = cur + cur = tmp + } + // 最终返回dummy虚拟头节点的下一个,return可以省略 + dummy.next + } +} +``` -----------------------
diff --git a/problems/0027.移除元素.md b/problems/0027.移除元素.md index 72860ffd..cf343f66 100644 --- a/problems/0027.移除元素.md +++ b/problems/0027.移除元素.md @@ -93,7 +93,7 @@ public: **双指针法(快慢指针法)在数组和链表的操作中是非常常见的,很多考察数组、链表、字符串等操作的面试题,都使用双指针法。** -后序都会一一介绍到,本题代码如下: +后续都会一一介绍到,本题代码如下: ```CPP // 时间复杂度:O(n) @@ -338,6 +338,20 @@ int removeElement(int* nums, int numsSize, int val){ return slow; } ``` - +Scala: +```scala +object Solution { + def removeElement(nums: Array[Int], `val`: Int): Int = { + var slow = 0 + for (fast <- 0 until nums.length) { + if (`val` != nums(fast)) { + nums(slow) = nums(fast) + slow += 1 + } + } + slow + } +} +``` -----------------------
diff --git a/problems/0028.实现strStr.md b/problems/0028.实现strStr.md index d67e5f70..1cdd5292 100644 --- a/problems/0028.实现strStr.md +++ b/problems/0028.实现strStr.md @@ -1166,5 +1166,80 @@ func strStr(_ haystack: String, _ needle: String) -> Int { ``` +PHP: + +> 前缀表统一减一 +```php +function strStr($haystack, $needle) { + if (strlen($needle) == 0) return 0; + $next= []; + $this->getNext($next,$needle); + + $j = -1; + for ($i = 0;$i < strlen($haystack); $i++) { // 注意i就从0开始 + while($j >= 0 && $haystack[$i] != $needle[$j + 1]) { + $j = $next[$j]; + } + if ($haystack[$i] == $needle[$j + 1]) { + $j++; + } + if ($j == (strlen($needle) - 1) ) { + return ($i - strlen($needle) + 1); + } + } + return -1; +} + +function getNext(&$next, $s){ + $j = -1; + $next[0] = $j; + for($i = 1; $i < strlen($s); $i++) { // 注意i从1开始 + while ($j >= 0 && $s[$i] != $s[$j + 1]) { + $j = $next[$j]; + } + if ($s[$i] == $s[$j + 1]) { + $j++; + } + $next[$i] = $j; + } +} +``` + +> 前缀表统一不减一 +```php +function strStr($haystack, $needle) { + if (strlen($needle) == 0) return 0; + $next= []; + $this->getNext($next,$needle); + + $j = 0; + for ($i = 0;$i < strlen($haystack); $i++) { // 注意i就从0开始 + while($j > 0 && $haystack[$i] != $needle[$j]) { + $j = $next[$j-1]; + } + if ($haystack[$i] == $needle[$j]) { + $j++; + } + if ($j == strlen($needle)) { + return ($i - strlen($needle) + 1); + } + } + return -1; +} + +function getNext(&$next, $s){ + $j = 0; + $next[0] = $j; + for($i = 1; $i < strlen($s); $i++) { // 注意i从1开始 + while ($j > 0 && $s[$i] != $s[$j]) { + $j = $next[$j-1]; + } + if ($s[$i] == $s[$j]) { + $j++; + } + $next[$i] = $j; + } +} +``` -----------------------
diff --git a/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md b/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md index dfd90b82..260462c2 100644 --- a/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md +++ b/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md @@ -480,7 +480,52 @@ var searchRange = function(nums, target) { return [-1, -1]; }; ``` +### Scala +```scala +object Solution { + def searchRange(nums: Array[Int], target: Int): Array[Int] = { + var left = getLeftBorder(nums, target) + var right = getRightBorder(nums, target) + if (left == -2 || right == -2) return Array(-1, -1) + if (right - left > 1) return Array(left + 1, right - 1) + Array(-1, -1) + } + // 寻找左边界 + def getLeftBorder(nums: Array[Int], target: Int): Int = { + var leftBorder = -2 + var left = 0 + var right = nums.length - 1 + while (left <= right) { + var mid = left + (right - left) / 2 + if (nums(mid) >= target) { + right = mid - 1 + leftBorder = right + } else { + left = mid + 1 + } + } + leftBorder + } + + // 寻找右边界 + def getRightBorder(nums: Array[Int], target: Int): Int = { + var rightBorder = -2 + var left = 0 + var right = nums.length - 1 + while (left <= right) { + var mid = left + (right - left) / 2 + if (nums(mid) <= target) { + left = mid + 1 + rightBorder = left + } else { + right = mid - 1 + } + } + rightBorder + } +} +``` -----------------------
diff --git a/problems/0035.搜索插入位置.md b/problems/0035.搜索插入位置.md index ff1b0292..5cf44ded 100644 --- a/problems/0035.搜索插入位置.md +++ b/problems/0035.搜索插入位置.md @@ -316,7 +316,26 @@ func searchInsert(_ nums: [Int], _ target: Int) -> Int { return right + 1 } ``` - +### Scala +```scala +object Solution { + def searchInsert(nums: Array[Int], target: Int): Int = { + var left = 0 + var right = nums.length - 1 + while (left <= right) { + var mid = left + (right - left) / 2 + if (target == nums(mid)) { + return mid + } else if (target > nums(mid)) { + left = mid + 1 + } else { + right = mid - 1 + } + } + right + 1 + } +} +``` ### PHP diff --git a/problems/0056.合并区间.md b/problems/0056.合并区间.md index 92b66473..e444a221 100644 --- a/problems/0056.合并区间.md +++ b/problems/0056.合并区间.md @@ -96,7 +96,7 @@ public: vector> merge(vector>& intervals) { vector> result; if (intervals.size() == 0) return result; - // 排序的参数使用了lamda表达式 + // 排序的参数使用了lambda表达式 sort(intervals.begin(), intervals.end(), [](const vector& a, const vector& b){return a[0] < b[0];}); result.push_back(intervals[0]); diff --git a/problems/0059.螺旋矩阵II.md b/problems/0059.螺旋矩阵II.md index 93735895..22229302 100644 --- a/problems/0059.螺旋矩阵II.md +++ b/problems/0059.螺旋矩阵II.md @@ -130,57 +130,37 @@ Java: ```Java class Solution { public int[][] generateMatrix(int n) { + int loop = 0; // 控制循环次数 int[][] res = new int[n][n]; + int start = 0; // 每次循环的开始点(start, start) + int count = 1; // 定义填充数字 + int i, j; - // 循环次数 - int loop = n / 2; - - // 定义每次循环起始位置 - int startX = 0; - int startY = 0; - - // 定义偏移量 - int offset = 1; - - // 定义填充数字 - int count = 1; - - // 定义中间位置 - int mid = n / 2; - while (loop > 0) { - int i = startX; - int j = startY; - + while (loop++ < n / 2) { // 判断边界后,loop从1开始 // 模拟上侧从左到右 - for (; j startY; j--) { + for (; j >= loop; j--) { res[i][j] = count++; } // 模拟左侧从下到上 - for (; i > startX; i--) { + for (; i >= loop; i--) { res[i][j] = count++; } - - loop--; - - startX += 1; - startY += 1; - - offset += 2; + start++; } if (n % 2 == 1) { - res[mid][mid] = count; + res[start][start] = count; } return res; @@ -564,6 +544,57 @@ int** generateMatrix(int n, int* returnSize, int** returnColumnSizes){ return ans; } ``` +Scala: +```scala +object Solution { + def generateMatrix(n: Int): Array[Array[Int]] = { + var res = Array.ofDim[Int](n, n) // 定义一个n*n的二维矩阵 + var num = 1 // 标志当前到了哪个数字 + var i = 0 // 横坐标 + var j = 0 // 竖坐标 + while (num <= n * n) { + // 向右:当j不越界,并且下一个要填的数字是空白时 + while (j < n && res(i)(j) == 0) { + res(i)(j) = num // 当前坐标等于num + num += 1 // num++ + j += 1 // 竖坐标+1 + } + i += 1 // 下移一行 + j -= 1 // 左移一列 + + // 剩下的都同上 + + // 向下 + while (i < n && res(i)(j) == 0) { + res(i)(j) = num + num += 1 + i += 1 + } + i -= 1 + j -= 1 + + // 向左 + while (j >= 0 && res(i)(j) == 0) { + res(i)(j) = num + num += 1 + j -= 1 + } + i -= 1 + j += 1 + + // 向上 + while (i >= 0 && res(i)(j) == 0) { + res(i)(j) = num + num += 1 + i -= 1 + } + i += 1 + j += 1 + } + res + } +} +``` -----------------------
diff --git a/problems/0063.不同路径II.md b/problems/0063.不同路径II.md index d09ea0e6..59c60156 100644 --- a/problems/0063.不同路径II.md +++ b/problems/0063.不同路径II.md @@ -66,7 +66,7 @@ dp[i][j] :表示从(0 ,0)出发,到(i, j) 有dp[i][j]条不同的路 所以代码为: -``` +```cpp if (obstacleGrid[i][j] == 0) { // 当(i, j)没有障碍的时候,再推导dp[i][j] dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; } @@ -76,7 +76,7 @@ if (obstacleGrid[i][j] == 0) { // 当(i, j)没有障碍的时候,再推导dp[i 在[62.不同路径](https://programmercarl.com/0062.不同路径.html)不同路径中我们给出如下的初始化: -``` +```cpp vector> dp(m, vector(n, 0)); // 初始值为0 for (int i = 0; i < m; i++) dp[i][0] = 1; for (int j = 0; j < n; j++) dp[0][j] = 1; @@ -138,6 +138,8 @@ public: int uniquePathsWithObstacles(vector>& obstacleGrid) { int m = obstacleGrid.size(); int n = obstacleGrid[0].size(); + if (obstacleGrid[m - 1][n - 1] == 1 || obstacleGrid[0][0] == 1) //如果在起点或终点出现了障碍,直接返回0 + return 0; vector> dp(m, vector(n, 0)); for (int i = 0; i < m && obstacleGrid[i][0] == 0; i++) dp[i][0] = 1; for (int j = 0; j < n && obstacleGrid[0][j] == 0; j++) dp[0][j] = 1; diff --git a/problems/0070.爬楼梯完全背包版本.md b/problems/0070.爬楼梯完全背包版本.md index 2286de2d..0f482bb7 100644 --- a/problems/0070.爬楼梯完全背包版本.md +++ b/problems/0070.爬楼梯完全背包版本.md @@ -199,6 +199,28 @@ var climbStairs = function(n) { }; ``` +TypeScript: + +```typescript +function climbStairs(n: number): number { + const m: number = 2; // 本题m为2 + const dp: number[] = new Array(n + 1).fill(0); + dp[0] = 1; + // 遍历背包 + for (let i = 1; i <= n; i++) { + // 遍历物品 + for (let j = 1; j <= m; j++) { + if (j <= i) { + dp[i] += dp[i - j]; + } + } + } + return dp[n]; +}; +``` + + + -----------------------
diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index ab8f2e57..dcdff178 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -82,6 +82,26 @@ public: } }; ``` +```CPP +# 递归法 +class Solution { +public: + void order(TreeNode* cur, vector>& result, int depth) + { + if (cur == nullptr) return; + if (result.size() == depth) result.push_back(vector()); + result[depth].push_back(cur->val); + order(cur->left, result, depth + 1); + order(cur->right, result, depth + 1); + } + vector> levelOrder(TreeNode* root) { + vector> result; + int depth = 0; + order(root, result, depth); + return result; + } +}; +``` python3代码: @@ -298,6 +318,61 @@ func levelOrder(_ root: TreeNode?) -> [[Int]] { return result } ``` +Scala: +```scala +// 102.二叉树的层序遍历 +object Solution { + import scala.collection.mutable + def levelOrder(root: TreeNode): List[List[Int]] = { + val res = mutable.ListBuffer[List[Int]]() + if (root == null) return res.toList + val queue = mutable.Queue[TreeNode]() // 声明一个队列 + queue.enqueue(root) // 把根节点加入queue + while (!queue.isEmpty) { + val tmp = mutable.ListBuffer[Int]() + val len = queue.size // 求出len的长度 + for (i <- 0 until len) { // 从0到当前队列长度的所有节点都加入到结果集 + val curNode = queue.dequeue() + tmp.append(curNode.value) + if (curNode.left != null) queue.enqueue(curNode.left) + if (curNode.right != null) queue.enqueue(curNode.right) + } + res.append(tmp.toList) + } + res.toList + } +} +``` + +Rust: + +```rust +pub fn level_order(root: Option>>) -> Vec> { + 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 +} +``` + **此时我们就掌握了二叉树的层序遍历了,那么如下九道力扣上的题目,只需要修改模板的两三行代码(不能再多了),便可打倒!** @@ -528,6 +603,61 @@ func levelOrderBottom(_ root: TreeNode?) -> [[Int]] { } ``` + +Scala: +```scala +// 107.二叉树的层次遍历II +object Solution { + import scala.collection.mutable + def levelOrderBottom(root: TreeNode): List[List[Int]] = { + val res = mutable.ListBuffer[List[Int]]() + if (root == null) return res.toList + val queue = mutable.Queue[TreeNode]() + queue.enqueue(root) + while (!queue.isEmpty) { + val tmp = mutable.ListBuffer[Int]() + val len = queue.size + for (i <- 0 until len) { + val curNode = queue.dequeue() + tmp.append(curNode.value) + if (curNode.left != null) queue.enqueue(curNode.left) + if (curNode.right != null) queue.enqueue(curNode.right) + } + res.append(tmp.toList) + } + // 最后翻转一下 + res.reverse.toList + } + +Rust: + +```rust +pub fn level_order(root: Option>>) -> Vec> { + 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/) @@ -750,6 +880,31 @@ func rightSideView(_ root: TreeNode?) -> [Int] { } ``` +Scala: +```scala +// 199.二叉树的右视图 +object Solution { + import scala.collection.mutable + def rightSideView(root: TreeNode): List[Int] = { + val res = mutable.ListBuffer[Int]() + if (root == null) return res.toList + val queue = mutable.Queue[TreeNode]() + queue.enqueue(root) + while (!queue.isEmpty) { + val len = queue.size + var curNode: TreeNode = null + for (i <- 0 until len) { + curNode = queue.dequeue() + if (curNode.left != null) queue.enqueue(curNode.left) + if (curNode.right != null) queue.enqueue(curNode.right) + } + res.append(curNode.value) // 把最后一个节点的值加入解集 + } + res.toList // 最后需要把res转换为List,return关键字可以省略 + } +} +``` + # 637.二叉树的层平均值 [力扣题目链接](https://leetcode-cn.com/problems/average-of-levels-in-binary-tree/) @@ -981,6 +1136,30 @@ func averageOfLevels(_ root: TreeNode?) -> [Double] { return result } ``` +Scala: +```scala +// 637.二叉树的层平均值 +object Solution { + import scala.collection.mutable + def averageOfLevels(root: TreeNode): Array[Double] = { + val res = mutable.ArrayBuffer[Double]() + val queue = mutable.Queue[TreeNode]() + queue.enqueue(root) + while (!queue.isEmpty) { + var sum = 0.0 + var len = queue.size + for (i <- 0 until len) { + var curNode = queue.dequeue() + sum += curNode.value // 累加该层的值 + if (curNode.left != null) queue.enqueue(curNode.left) + if (curNode.right != null) queue.enqueue(curNode.right) + } + res.append(sum / len) // 平均值即为sum/len + } + res.toArray // 最后需要转换为Array,return关键字可以省略 + } +} +``` # 429.N叉树的层序遍历 @@ -1225,6 +1404,34 @@ func levelOrder(_ root: Node?) -> [[Int]] { } ``` +Scala: +```scala +// 429.N叉树的层序遍历 +object Solution { + import scala.collection.mutable + def levelOrder(root: Node): List[List[Int]] = { + val res = mutable.ListBuffer[List[Int]]() + if (root == null) return res.toList + val queue = mutable.Queue[Node]() + queue.enqueue(root) // 根节点入队 + while (!queue.isEmpty) { + val tmp = mutable.ListBuffer[Int]() // 存储每层节点 + val len = queue.size + for (i <- 0 until len) { + val curNode = queue.dequeue() + tmp.append(curNode.value) // 将该节点的值加入tmp + // 循环遍历该节点的子节点,加入队列 + for (child <- curNode.children) { + queue.enqueue(child) + } + } + res.append(tmp.toList) // 将该层的节点放到结果集 + } + res.toList + } +} +``` + # 515.在每个树行中找最大值 [力扣题目链接](https://leetcode-cn.com/problems/find-largest-value-in-each-tree-row/) diff --git a/problems/0104.二叉树的最大深度.md b/problems/0104.二叉树的最大深度.md index 2229a854..65a155fb 100644 --- a/problems/0104.二叉树的最大深度.md +++ b/problems/0104.二叉树的最大深度.md @@ -192,6 +192,33 @@ public: }; ``` +rust: +```rust +impl Solution { + pub fn max_depth(root: Option>>) -> 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叉树的最大深度 diff --git a/problems/0110.平衡二叉树.md b/problems/0110.平衡二叉树.md index d98ff8a9..b7598365 100644 --- a/problems/0110.平衡二叉树.md +++ b/problems/0110.平衡二叉树.md @@ -208,7 +208,7 @@ int getHeight(TreeNode* node) { ```CPP class Solution { public: - // 返回以该节点为根节点的二叉树的高度,如果不是二叉搜索树了则返回-1 + // 返回以该节点为根节点的二叉树的高度,如果不是平衡二叉树了则返回-1 int getHeight(TreeNode* node) { if (node == NULL) { return 0; diff --git a/problems/0111.二叉树的最小深度.md b/problems/0111.二叉树的最小深度.md index 224caa5e..b1331659 100644 --- a/problems/0111.二叉树的最小深度.md +++ b/problems/0111.二叉树的最小深度.md @@ -488,5 +488,69 @@ func minDepth(_ root: TreeNode?) -> Int { } ``` +rust: +```rust +impl Solution { + pub fn min_depth(root: Option>>) -> i32 { + return Solution::bfs(root) + } + + // 递归 + pub fn dfs(node: Option>>) -> 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>>) -> 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 + } +``` + -----------------------
diff --git a/problems/0112.路径总和.md b/problems/0112.路径总和.md index 41463ec1..d3eec16b 100644 --- a/problems/0112.路径总和.md +++ b/problems/0112.路径总和.md @@ -377,22 +377,22 @@ class solution { ```java class solution { - public list> pathsum(treenode root, int targetsum) { - list> res = new arraylist<>(); + public List> pathsum(TreeNode root, int targetsum) { + List> res = new ArrayList<>(); if (root == null) return res; // 非空判断 - - list path = new linkedlist<>(); + + List path = new LinkedList<>(); preorderdfs(root, targetsum, res, path); return res; } - public void preorderdfs(treenode root, int targetsum, list> res, list path) { + public void preorderdfs(TreeNode root, int targetsum, List> res, List path) { path.add(root.val); // 遇到了叶子节点 if (root.left == null && root.right == null) { // 找到了和为 targetsum 的路径 if (targetsum - root.val == 0) { - res.add(new arraylist<>(path)); + res.add(new ArrayList<>(path)); } return; // 如果和不为 targetsum,返回 } @@ -1006,6 +1006,126 @@ func traversal(_ cur: TreeNode?, count: Int) { } ``` +## C +> 0112.路径总和 +递归法: +```c +bool hasPathSum(struct TreeNode* root, int targetSum){ + // 递归结束条件:若当前节点不存在,返回false + if(!root) + return false; + // 若当前节点为叶子节点,且targetSum-root的值为0。(当前路径上的节点值的和满足条件)返回true + if(!root->right && !root->left && targetSum == root->val) + return true; + + // 查看左子树和右子树的所有节点是否满足条件 + return hasPathSum(root->right, targetSum - root->val) || hasPathSum(root->left, targetSum - root->val); +} +``` + +迭代法: +```c +// 存储一个节点以及当前的和 +struct Pair { + struct TreeNode* node; + int sum; +}; + +bool hasPathSum(struct TreeNode* root, int targetSum){ + struct Pair stack[1000]; + int stackTop = 0; + + // 若root存在,则将节点和值封装成一个pair入栈 + if(root) { + struct Pair newPair = {root, root->val}; + stack[stackTop++] = newPair; + } + + // 当栈不为空时 + while(stackTop) { + // 出栈栈顶元素 + struct Pair topPair = stack[--stackTop]; + // 若栈顶元素为叶子节点,且和为targetSum时,返回true + if(!topPair.node->left && !topPair.node->right && topPair.sum == targetSum) + return true; + + // 若当前栈顶节点有左右孩子,计算和并入栈 + if(topPair.node->left) { + struct Pair newPair = {topPair.node->left, topPair.sum + topPair.node->left->val}; + stack[stackTop++] = newPair; + } + if(topPair.node->right) { + struct Pair newPair = {topPair.node->right, topPair.sum + topPair.node->right->val}; + stack[stackTop++] = newPair; + } + } + return false; +} +``` +> 0113.路径总和 II +```c +int** ret; +int* path; +int* colSize; +int retTop; +int pathTop; + +void traversal(const struct TreeNode* const node, int count) { + // 若当前节点为叶子节点 + if(!node->right && !node->left) { + // 若当前path上的节点值总和等于targetSum。 + if(count == 0) { + // 复制当前path + int *curPath = (int*)malloc(sizeof(int) * pathTop); + memcpy(curPath, path, sizeof(int) * pathTop); + // 记录当前path的长度为pathTop + colSize[retTop] = pathTop; + // 将当前path加入到ret数组中 + ret[retTop++] = curPath; + } + return; + } + + // 若节点有左/右孩子 + if(node->left) { + // 将左孩子的值加入path中 + path[pathTop++] = node->left->val; + traversal(node->left, count - node->left->val); + // 回溯 + pathTop--; + } + if(node->right) { + // 将右孩子的值加入path中 + path[pathTop++] = node->right->val; + traversal(node->right, count - node->right->val); + // 回溯 + --pathTop; + } +} + +int** pathSum(struct TreeNode* root, int targetSum, int* returnSize, int** returnColumnSizes){ + // 初始化数组 + ret = (int**)malloc(sizeof(int*) * 1000); + path = (int*)malloc(sizeof(int*) * 1000); + colSize = (int*)malloc(sizeof(int) * 1000); + retTop = pathTop = 0; + *returnSize = 0; + + // 若根节点不存在,返回空的ret + if(!root) + return ret; + // 将根节点加入到path中 + path[pathTop++] = root->val; + traversal(root, targetSum - root->val); + + // 设置返回ret数组大小,以及其中每个一维数组元素的长度 + *returnSize = retTop; + *returnColumnSizes = colSize; + + return ret; +} +``` + ----------------------- diff --git a/problems/0121.买卖股票的最佳时机.md b/problems/0121.买卖股票的最佳时机.md index f0bc3b97..a2498bb6 100644 --- a/problems/0121.买卖股票的最佳时机.md +++ b/problems/0121.买卖股票的最佳时机.md @@ -426,6 +426,46 @@ var maxProfit = function(prices) { }; ``` +TypeScript: + +> 贪心法 + +```typescript +function maxProfit(prices: number[]): number { + if (prices.length === 0) return 0; + let buy: number = prices[0]; + let profitMax: number = 0; + for (let i = 1, length = prices.length; i < length; i++) { + profitMax = Math.max(profitMax, prices[i] - buy); + buy = Math.min(prices[i], buy); + } + return profitMax; +}; +``` + +> 动态规划 + +```typescript +function maxProfit(prices: number[]): number { + /** + dp[i][0]: 第i天持有股票的最大现金 + dp[i][1]: 第i天不持有股票的最大现金 + */ + const length = prices.length; + if (length === 0) return 0; + const dp: number[][] = []; + dp[0] = [-prices[0], 0]; + for (let i = 1; i < length; i++) { + dp[i] = []; + dp[i][0] = Math.max(dp[i - 1][0], -prices[i]); + dp[i][1] = Math.max(dp[i - 1][0] + prices[i], dp[i - 1][1]); + } + return dp[length - 1][1]; +}; +``` + + + -----------------------
diff --git a/problems/0122.买卖股票的最佳时机II(动态规划).md b/problems/0122.买卖股票的最佳时机II(动态规划).md index 5a165a14..12b21fde 100644 --- a/problems/0122.买卖股票的最佳时机II(动态规划).md +++ b/problems/0122.买卖股票的最佳时机II(动态规划).md @@ -295,6 +295,42 @@ const maxProfit = (prices) => { } ``` +TypeScript: + +> 动态规划 + +```typescript +function maxProfit(prices: number[]): number { + /** + dp[i][0]: 第i天持有股票 + dp[i][1]: 第i天不持有股票 + */ + const length: number = prices.length; + if (length === 0) return 0; + const dp: number[][] = new Array(length).fill(0).map(_ => []); + dp[0] = [-prices[0], 0]; + for (let i = 1; i < length; i++) { + dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] - prices[i]); + dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] + prices[i]); + } + return dp[length - 1][1]; +}; +``` + +> 贪心法 + +```typescript +function maxProfit(prices: number[]): number { + let resProfit: number = 0; + for (let i = 1, length = prices.length; i < length; i++) { + if (prices[i] > prices[i - 1]) { + resProfit += prices[i] - prices[i - 1]; + } + } + return resProfit; +}; +``` + ----------------------- diff --git a/problems/0123.买卖股票的最佳时机III.md b/problems/0123.买卖股票的最佳时机III.md index 56ade343..67c99497 100644 --- a/problems/0123.买卖股票的最佳时机III.md +++ b/problems/0123.买卖股票的最佳时机III.md @@ -352,6 +352,36 @@ const maxProfit = prices => { }; ``` +TypeScript: + +> 版本一 + +```typescript +function maxProfit(prices: number[]): number { + /** + dp[i][0]: 无操作; + dp[i][1]: 第一次买入; + dp[i][2]: 第一次卖出; + dp[i][3]: 第二次买入; + dp[i][4]: 第二次卖出; + */ + const length: number = prices.length; + if (length === 0) return 0; + const dp: number[][] = new Array(length).fill(0) + .map(_ => new Array(5).fill(0)); + dp[0][1] = -prices[0]; + dp[0][3] = -prices[0]; + for (let i = 1; i < length; i++) { + dp[i][0] = dp[i - 1][0]; + dp[i][1] = Math.max(dp[i - 1][1], -prices[i]); + dp[i][2] = Math.max(dp[i - 1][2], dp[i - 1][1] + prices[i]); + dp[i][3] = Math.max(dp[i - 1][3], dp[i - 1][2] - prices[i]); + dp[i][4] = Math.max(dp[i - 1][4], dp[i - 1][3] + prices[i]); + } + return Math.max(dp[length - 1][2], dp[length - 1][4]); +}; +``` + Go: > 版本一: diff --git a/problems/0139.单词拆分.md b/problems/0139.单词拆分.md index ac834f04..5b4e92b9 100644 --- a/problems/0139.单词拆分.md +++ b/problems/0139.单词拆分.md @@ -345,6 +345,48 @@ const wordBreak = (s, wordDict) => { } ``` +TypeScript: + +> 动态规划 + +```typescript +function wordBreak(s: string, wordDict: string[]): boolean { + const dp: boolean[] = new Array(s.length + 1).fill(false); + dp[0] = true; + for (let i = 1; i <= s.length; i++) { + for (let j = 0; j < i; j++) { + const tempStr: string = s.slice(j, i); + if (wordDict.includes(tempStr) && dp[j] === true) { + dp[i] = true; + break; + } + } + } + return dp[s.length]; +}; +``` + +> 记忆化回溯 + +```typescript +function wordBreak(s: string, wordDict: string[]): boolean { + // 只需要记忆结果为false的情况 + const memory: boolean[] = []; + return backTracking(s, wordDict, 0, memory); + function backTracking(s: string, wordDict: string[], startIndex: number, memory: boolean[]): boolean { + if (startIndex >= s.length) return true; + if (memory[startIndex] === false) return false; + for (let i = startIndex + 1, length = s.length; i <= length; i++) { + const str: string = s.slice(startIndex, i); + if (wordDict.includes(str) && backTracking(s, wordDict, i, memory)) + return true; + } + memory[startIndex] = false; + return false; + } +}; +``` + ----------------------- diff --git a/problems/0142.环形链表II.md b/problems/0142.环形链表II.md index e8ca950d..f8e62d45 100644 --- a/problems/0142.环形链表II.md +++ b/problems/0142.环形链表II.md @@ -370,7 +370,31 @@ ListNode *detectCycle(ListNode *head) { } ``` - +Scala: +```scala +object Solution { + def detectCycle(head: ListNode): ListNode = { + var fast = head // 快指针 + var slow = head // 慢指针 + while (fast != null && fast.next != null) { + fast = fast.next.next // 快指针一次走两步 + slow = slow.next // 慢指针一次走一步 + // 如果相遇,fast快指针回到头 + if (fast == slow) { + fast = head + // 两个指针一步一步的走,第一次相遇的节点必是入环节点 + while (fast != slow) { + fast = fast.next + slow = slow.next + } + return fast + } + } + // 如果fast指向空值,必然无环返回null + null + } +} +``` -----------------------
diff --git a/problems/0150.逆波兰表达式求值.md b/problems/0150.逆波兰表达式求值.md index fd3d69aa..5452c304 100644 --- a/problems/0150.逆波兰表达式求值.md +++ b/problems/0150.逆波兰表达式求值.md @@ -136,19 +136,19 @@ java: class Solution { public int evalRPN(String[] tokens) { Deque stack = new LinkedList(); - for (int i = 0; i < tokens.length; ++i) { - if ("+".equals(tokens[i])) { // leetcode 内置jdk的问题,不能使用==判断字符串是否相等 + for (String s : tokens) { + if ("+".equals(s)) { // leetcode 内置jdk的问题,不能使用==判断字符串是否相等 stack.push(stack.pop() + stack.pop()); // 注意 - 和/ 需要特殊处理 - } else if ("-".equals(tokens[i])) { + } else if ("-".equals(s)) { stack.push(-stack.pop() + stack.pop()); - } else if ("*".equals(tokens[i])) { + } else if ("*".equals(s)) { stack.push(stack.pop() * stack.pop()); - } else if ("/".equals(tokens[i])) { + } else if ("/".equals(s)) { int temp1 = stack.pop(); int temp2 = stack.pop(); stack.push(temp2 / temp1); } else { - stack.push(Integer.valueOf(tokens[i])); + stack.push(Integer.valueOf(s)); } } return stack.pop(); @@ -325,6 +325,33 @@ func evalRPN(_ tokens: [String]) -> Int { return stack.last! } ``` - +Scala: +```scala +object Solution { + import scala.collection.mutable + def evalRPN(tokens: Array[String]): Int = { + val stack = mutable.Stack[Int]() // 定义栈 + // 抽取运算操作,需要传递x,y,和一个函数 + def operator(x: Int, y: Int, f: (Int, Int) => Int): Int = f(x, y) + for (token <- tokens) { + // 模式匹配,匹配不同的操作符做什么样的运算 + token match { + // 最后一个参数 _+_,代表x+y,遵循Scala的函数至简原则,以下运算同理 + case "+" => stack.push(operator(stack.pop(), stack.pop(), _ + _)) + case "-" => stack.push(operator(stack.pop(), stack.pop(), -_ + _)) + case "*" => stack.push(operator(stack.pop(), stack.pop(), _ * _)) + case "/" => { + var pop1 = stack.pop() + var pop2 = stack.pop() + stack.push(operator(pop2, pop1, _ / _)) + } + case _ => stack.push(token.toInt) // 不是运算符就入栈 + } + } + // 最后返回栈顶,不需要加return关键字 + stack.pop() + } +} +``` -----------------------
diff --git a/problems/0151.翻转字符串里的单词.md b/problems/0151.翻转字符串里的单词.md index d03de421..0e25fc4d 100644 --- a/problems/0151.翻转字符串里的单词.md +++ b/problems/0151.翻转字符串里的单词.md @@ -758,7 +758,63 @@ func reverseWord(_ s: inout [Character]) { } ``` +Scala: +```scala +object Solution { + def reverseWords(s: String): String = { + var sb = removeSpace(s) // 移除多余的空格 + reverseString(sb, 0, sb.length - 1) // 翻转字符串 + reverseEachWord(sb) + sb.mkString + } + + // 移除多余的空格 + def removeSpace(s: String): Array[Char] = { + var start = 0 + var end = s.length - 1 + // 移除字符串前面的空格 + while (start < s.length && s(start) == ' ') start += 1 + // 移除字符串后面的空格 + while (end >= 0 && s(end) == ' ') end -= 1 + var sb = "" // String + // 当start小于等于end的时候,执行添加操作 + while (start <= end) { + var c = s(start) + // 当前字符不等于空,sb的最后一个字符不等于空的时候添加到sb + if (c != ' ' || sb(sb.length - 1) != ' ') { + sb ++= c.toString + } + start += 1 // 指针向右移动 + } + sb.toArray + } + + // 翻转字符串 + def reverseString(s: Array[Char], start: Int, end: Int): Unit = { + var (left, right) = (start, end) + while (left < right) { + var tmp = s(left) + s(left) = s(right) + s(right) = tmp + left += 1 + right -= 1 + } + } + + // 翻转每个单词 + def reverseEachWord(s: Array[Char]): Unit = { + var i = 0 + while (i < s.length) { + var j = i + 1 + // 向后迭代寻找每个单词的坐标 + while (j < s.length && s(j) != ' ') j += 1 + reverseString(s, i, j - 1) // 翻转每个单词 + i = j + 1 // i往后更新 + } + } +} +``` diff --git a/problems/0188.买卖股票的最佳时机IV.md b/problems/0188.买卖股票的最佳时机IV.md index 61c558a1..27eb38c3 100644 --- a/problems/0188.买卖股票的最佳时机IV.md +++ b/problems/0188.买卖股票的最佳时机IV.md @@ -409,5 +409,27 @@ var maxProfit = function(k, prices) { }; ``` +TypeScript: + +```typescript +function maxProfit(k: number, prices: number[]): number { + const length: number = prices.length; + if (length === 0) return 0; + const dp: number[][] = new Array(length).fill(0) + .map(_ => new Array(k * 2 + 1).fill(0)); + for (let i = 1; i <= k; i++) { + dp[0][i * 2 - 1] = -prices[0]; + } + for (let i = 1; i < length; i++) { + for (let j = 1; j < 2 * k + 1; j++) { + dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - 1] + Math.pow(-1, j) * prices[i]); + } + } + return dp[length - 1][2 * k]; +}; +``` + + + -----------------------
diff --git a/problems/0198.打家劫舍.md b/problems/0198.打家劫舍.md index dfe1f3a0..a828b9a9 100644 --- a/problems/0198.打家劫舍.md +++ b/problems/0198.打家劫舍.md @@ -189,6 +189,29 @@ const rob = nums => { }; ``` +TypeScript: + +```typescript +function rob(nums: number[]): number { + /** + dp[i]: 前i个房屋能偷到的最大金额 + dp[0]: nums[0]; + dp[1]: max(nums[0], nums[1]); + ... + dp[i]: max(dp[i-1], dp[i-2]+nums[i]); + */ + const length: number = nums.length; + if (length === 1) return nums[0]; + const dp: number[] = []; + dp[0] = nums[0]; + dp[1] = Math.max(nums[0], nums[1]); + for (let i = 2; i < length; i++) { + dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]); + } + return dp[length - 1]; +}; +``` + diff --git a/problems/0202.快乐数.md b/problems/0202.快乐数.md index 741a735a..be8686f7 100644 --- a/problems/0202.快乐数.md +++ b/problems/0202.快乐数.md @@ -385,5 +385,61 @@ bool isHappy(int n){ return bHappy; } ``` + +Scala: +```scala +object Solution { + // 引入mutable + import scala.collection.mutable + def isHappy(n: Int): Boolean = { + // 存放每次计算后的结果 + val set: mutable.HashSet[Int] = new mutable.HashSet[Int]() + var tmp = n // 因为形参是不可变量,所以需要找到一个临时变量 + // 开始进入循环 + while (true) { + val sum = getSum(tmp) // 获取这个数每个值的平方和 + if (sum == 1) return true // 如果最终等于 1,则返回true + // 如果set里面已经有这个值了,说明进入无限循环,可以返回false,否则添加这个值到set + if (set.contains(sum)) return false + else set.add(sum) + tmp = sum + } + // 最终需要返回值,直接返回个false + false + } + + def getSum(n: Int): Int = { + var sum = 0 + var tmp = n + while (tmp != 0) { + sum += (tmp % 10) * (tmp % 10) + tmp = tmp / 10 + } + sum + } + + +C#: +```csharp +public class Solution { + private int getSum(int n) { + int sum = 0; + //每位数的换算 + while (n > 0) { + sum += (n % 10) * (n % 10); + n /= 10; + } + return sum; + } + public bool IsHappy(int n) { + HashSet set = new HashSet(); + while(n != 1 && !set.Contains(n)) { //判断避免循环 + set.Add(n); + n = getSum(n); + } + return n == 1; + } +} +``` -----------------------
diff --git a/problems/0203.移除链表元素.md b/problems/0203.移除链表元素.md index 751553e2..fe78ddab 100644 --- a/problems/0203.移除链表元素.md +++ b/problems/0203.移除链表元素.md @@ -266,6 +266,27 @@ public ListNode removeElements(ListNode head, int val) { } return head; } +/** + * 不添加虚拟节点and pre Node方式 + * 时间复杂度 O(n) + * 空间复杂度 O(1) + * @param head + * @param val + * @return + */ +public ListNode removeElements(ListNode head, int val) { + while(head!=null && head.val==val){ + head = head.next; + } + ListNode curr = head; + while(curr!=null){ + while(curr.next!=null && curr.next.val == val){ + curr.next = curr.next.next; + } + curr = curr.next; + } + return head; +} ``` Python: @@ -478,6 +499,36 @@ impl Solution { } } ``` - +Scala: +```scala +/** + * Definition for singly-linked list. + * class ListNode(_x: Int = 0, _next: ListNode = null) { + * var next: ListNode = _next + * var x: Int = _x + * } + */ +object Solution { + def removeElements(head: ListNode, `val`: Int): ListNode = { + if (head == null) return head + var dummy = new ListNode(-1, head) // 定义虚拟头节点 + var cur = head // cur 表示当前节点 + var pre = dummy // pre 表示cur前一个节点 + while (cur != null) { + if (cur.x == `val`) { + // 相等,就删除那么cur的前一个节点pre执行cur的下一个 + pre.next = cur.next + } else { + // 不相等,pre就等于当前cur节点 + pre = cur + } + // 向下迭代 + cur = cur.next + } + // 最终返回dummy的下一个,就是链表的头 + dummy.next + } +} +``` -----------------------
diff --git a/problems/0206.翻转链表.md b/problems/0206.翻转链表.md index 941928ba..25b16907 100644 --- a/problems/0206.翻转链表.md +++ b/problems/0206.翻转链表.md @@ -496,6 +496,40 @@ struct ListNode* reverseList(struct ListNode* head){ return reverse(NULL, head); } ``` +Scala: +双指针法: +```scala +object Solution { + def reverseList(head: ListNode): ListNode = { + var pre: ListNode = null + var cur = head + while (cur != null) { + var tmp = cur.next + cur.next = pre + pre = cur + cur = tmp + } + pre + } +} +``` +递归法: +```scala +object Solution { + def reverseList(head: ListNode): ListNode = { + reverse(null, head) + } + + def reverse(pre: ListNode, cur: ListNode): ListNode = { + if (cur == null) { + return pre // 如果当前cur为空,则返回pre + } + val tmp: ListNode = cur.next + cur.next = pre + reverse(cur, tmp) // 此时cur成为前一个节点,tmp是当前节点 + } +} +``` -----------------------
diff --git a/problems/0209.长度最小的子数组.md b/problems/0209.长度最小的子数组.md index d31cba3f..fbef7692 100644 --- a/problems/0209.长度最小的子数组.md +++ b/problems/0209.长度最小的子数组.md @@ -400,6 +400,54 @@ class Solution { } } ``` +Scala: + +滑动窗口: +```scala +object Solution { + def minSubArrayLen(target: Int, nums: Array[Int]): Int = { + var result = Int.MaxValue // 返回结果,默认最大值 + var left = 0 // 慢指针,当sum>=target,向右移动 + var sum = 0 // 窗口值的总和 + for (right <- 0 until nums.length) { + sum += nums(right) + while (sum >= target) { + result = math.min(result, right - left + 1) // 产生新结果 + sum -= nums(left) // 左指针移动,窗口总和减去左指针的值 + left += 1 // 左指针向右移动 + } + } + // 相当于三元运算符,return关键字可以省略 + if (result == Int.MaxValue) 0 else result + } +} +``` + +暴力解法: +```scala +object Solution { + def minSubArrayLen(target: Int, nums: Array[Int]): Int = { + import scala.util.control.Breaks + var res = Int.MaxValue + var subLength = 0 + for (i <- 0 until nums.length) { + var sum = 0 + Breaks.breakable( + for (j <- i until nums.length) { + sum += nums(j) + if (sum >= target) { + subLength = j - i + 1 + res = math.min(subLength, res) + Breaks.break() + } + } + ) + } + // 相当于三元运算符 + if (res == Int.MaxValue) 0 else res + } +} +``` -----------------------
diff --git a/problems/0213.打家劫舍II.md b/problems/0213.打家劫舍II.md index 8e569e46..9e698d01 100644 --- a/problems/0213.打家劫舍II.md +++ b/problems/0213.打家劫舍II.md @@ -165,7 +165,30 @@ const robRange = (nums, start, end) => { return dp[end] } ``` +TypeScript: + +```typescript +function rob(nums: number[]): number { + const length: number = nums.length; + if (length === 0) return 0; + if (length === 1) return nums[0]; + return Math.max(robRange(nums, 0, length - 2), + robRange(nums, 1, length - 1)); +}; +function robRange(nums: number[], start: number, end: number): number { + if (start === end) return nums[start]; + const dp: number[] = []; + dp[start] = nums[start]; + dp[start + 1] = Math.max(nums[start], nums[start + 1]); + for (let i = start + 2; i <= end; i++) { + dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]); + } + return dp[end]; +} +``` + Go: + ```go // 打家劫舍Ⅱ 动态规划 // 时间复杂度O(n) 空间复杂度O(n) diff --git a/problems/0225.用队列实现栈.md b/problems/0225.用队列实现栈.md index 3457c4b3..3c134870 100644 --- a/problems/0225.用队列实现栈.md +++ b/problems/0225.用队列实现栈.md @@ -815,6 +815,89 @@ class MyStack { } } ``` +Scala: +使用两个队列模拟栈: +```scala +import scala.collection.mutable + +class MyStack() { + + val queue1 = new mutable.Queue[Int]() + val queue2 = new mutable.Queue[Int]() + + def push(x: Int) { + queue1.enqueue(x) + } + + def pop(): Int = { + var size = queue1.size + // 将queue1中的每个元素都移动到queue2 + for (i <- 0 until size - 1) { + queue2.enqueue(queue1.dequeue()) + } + var res = queue1.dequeue() + // 再将queue2中的每个元素都移动到queue1 + while (!queue2.isEmpty) { + queue1.enqueue(queue2.dequeue()) + } + res + } + + def top(): Int = { + var size = queue1.size + for (i <- 0 until size - 1) { + queue2.enqueue(queue1.dequeue()) + } + var res = queue1.dequeue() + while (!queue2.isEmpty) { + queue1.enqueue(queue2.dequeue()) + } + // 最终还需要把res送进queue1 + queue1.enqueue(res) + res + } + + def empty(): Boolean = { + queue1.isEmpty + } +} +``` +使用一个队列模拟: +```scala +import scala.collection.mutable + +class MyStack() { + + val queue = new mutable.Queue[Int]() + + def push(x: Int) { + queue.enqueue(x) + } + + def pop(): Int = { + var size = queue.size + for (i <- 0 until size - 1) { + queue.enqueue(queue.head) // 把头添到队列最后 + queue.dequeue() // 再出队 + } + queue.dequeue() + } + + def top(): Int = { + var size = queue.size + var res = 0 + for (i <- 0 until size) { + queue.enqueue(queue.head) // 把头添到队列最后 + res = queue.dequeue() // 再出队 + } + res + } + + def empty(): Boolean = { + queue.isEmpty + } +} +``` -----------------------
diff --git a/problems/0226.翻转二叉树.md b/problems/0226.翻转二叉树.md index a3ebe24d..af5b8043 100644 --- a/problems/0226.翻转二叉树.md +++ b/problems/0226.翻转二叉树.md @@ -368,9 +368,7 @@ func invertTree(root *TreeNode) *TreeNode { if root ==nil{ return nil } - temp:=root.Left - root.Left=root.Right - root.Right=temp + root.Left,root.Right=root.Right,root.Left//交换 invertTree(root.Left) invertTree(root.Right) diff --git a/problems/0232.用栈实现队列.md b/problems/0232.用栈实现队列.md index 1a56d9f3..d9ba8e26 100644 --- a/problems/0232.用栈实现队列.md +++ b/problems/0232.用栈实现队列.md @@ -495,6 +495,45 @@ void myQueueFree(MyQueue* obj) { obj->stackOutTop = 0; } ``` +Scala: +```scala +class MyQueue() { + import scala.collection.mutable + val stackIn = mutable.Stack[Int]() // 负责出栈 + val stackOut = mutable.Stack[Int]() // 负责入栈 + // 添加元素 + def push(x: Int) { + stackIn.push(x) + } + + // 复用代码,如果stackOut为空就把stackIn的所有元素都压入StackOut + def dumpStackIn(): Unit = { + if (!stackOut.isEmpty) return + while (!stackIn.isEmpty) { + stackOut.push(stackIn.pop()) + } + } + + // 弹出元素 + def pop(): Int = { + dumpStackIn() + stackOut.pop() + } + + // 获取队头 + def peek(): Int = { + dumpStackIn() + val res: Int = stackOut.pop() + stackOut.push(res) + res + } + + // 判断是否为空 + def empty(): Boolean = { + stackIn.isEmpty && stackOut.isEmpty + } +} +``` -----------------------
diff --git a/problems/0239.滑动窗口最大值.md b/problems/0239.滑动窗口最大值.md index f269450f..eb32fdd2 100644 --- a/problems/0239.滑动窗口最大值.md +++ b/problems/0239.滑动窗口最大值.md @@ -630,6 +630,53 @@ func maxSlidingWindow(_ nums: [Int], _ k: Int) -> [Int] { return result } ``` +Scala: +```scala +import scala.collection.mutable.ArrayBuffer +object Solution { + def maxSlidingWindow(nums: Array[Int], k: Int): Array[Int] = { + var len = nums.length - k + 1 // 滑动窗口长度 + var res: Array[Int] = new Array[Int](len) // 声明存储结果的数组 + var index = 0 // 结果数组指针 + val queue: MyQueue = new MyQueue // 自定义队列 + // 将前k个添加到queue + for (i <- 0 until k) { + queue.add(nums(i)) + } + res(index) = queue.peek // 第一个滑动窗口的最大值 + index += 1 + for (i <- k until nums.length) { + queue.poll(nums(i - k)) // 首先移除第i-k个元素 + queue.add(nums(i)) // 添加当前数字到队列 + res(index) = queue.peek() // 赋值 + index+=1 + } + // 最终返回res,return关键字可以省略 + res + } +} + +class MyQueue { + var queue = ArrayBuffer[Int]() + + // 移除元素,如果传递进来的跟队头相等,那么移除 + def poll(value: Int): Unit = { + if (!queue.isEmpty && queue.head == value) { + queue.remove(0) + } + } + + // 添加元素,当队尾大于当前元素就删除 + def add(value: Int): Unit = { + while (!queue.isEmpty && value > queue.last) { + queue.remove(queue.length - 1) + } + queue.append(value) + } + + def peek(): Int = queue.head +} +``` -----------------------
diff --git a/problems/0242.有效的字母异位词.md b/problems/0242.有效的字母异位词.md index 080166fd..8fd9c604 100644 --- a/problems/0242.有效的字母异位词.md +++ b/problems/0242.有效的字母异位词.md @@ -307,6 +307,52 @@ impl Solution { } } ``` + + +Scala: +```scala +object Solution { + def isAnagram(s: String, t: String): Boolean = { + // 如果两个字符串的长度不等,直接返回false + if (s.length != t.length) return false + val record = new Array[Int](26) // 记录每个单词出现了多少次 + // 遍历字符串,对于s字符串单词对应的记录+=1,t字符串对应的记录-=1 + for (i <- 0 until s.length) { + record(s(i) - 97) += 1 + record(t(i) - 97) -= 1 + } + // 如果不等于则直接返回false + for (i <- 0 until 26) { + if (record(i) != 0) { + return false + } + } + // 如果前面不返回false,说明匹配成功,返回true,return可以省略 + true + } +} +``` + + +C#: +```csharp + public bool IsAnagram(string s, string t) { + int sl=s.Length,tl=t.Length; + if(sl!=tl) return false; + int[] a = new int[26]; + for(int i = 0; i < sl; i++){ + a[s[i] - 'a']++; + a[t[i] - 'a']--; + } + foreach (int i in a) + { + if (i != 0) + return false; + } + return true; + } +``` + ## 相关题目 * 383.赎金信 diff --git a/problems/0279.完全平方数.md b/problems/0279.完全平方数.md index 9bad2085..5b15639c 100644 --- a/problems/0279.完全平方数.md +++ b/problems/0279.完全平方数.md @@ -355,5 +355,24 @@ var numSquares2 = function(n) { }; ``` +TypeScript: + +```typescript +function numSquares(n: number): number { + const goodsNum: number = Math.floor(Math.sqrt(n)); + const dp: number[] = new Array(n + 1).fill(Infinity); + dp[0] = 0; + for (let i = 1; i <= goodsNum; i++) { + const tempVal: number = i * i; + for (let j = tempVal; j <= n; j++) { + dp[j] = Math.min(dp[j], dp[j - tempVal] + 1); + } + } + return dp[n]; +}; +``` + + + -----------------------
diff --git a/problems/0300.最长上升子序列.md b/problems/0300.最长上升子序列.md index cfa8ae12..ffa66c02 100644 --- a/problems/0300.最长上升子序列.md +++ b/problems/0300.最长上升子序列.md @@ -237,6 +237,27 @@ const lengthOfLIS = (nums) => { }; ``` +TypeScript + +```typescript +function lengthOfLIS(nums: number[]): number { + /** + dp[i]: 前i个元素中,以nums[i]结尾,最长子序列的长度 + */ + const dp: number[] = new Array(nums.length).fill(1); + let resMax: number = 0; + for (let i = 0, length = nums.length; i < length; i++) { + for (let j = 0; j < i; j++) { + if (nums[i] > nums[j]) { + dp[i] = Math.max(dp[i], dp[j] + 1); + } + } + resMax = Math.max(resMax, dp[i]); + } + return resMax; +}; +``` + diff --git a/problems/0309.最佳买卖股票时机含冷冻期.md b/problems/0309.最佳买卖股票时机含冷冻期.md index f3e7541b..f037fe85 100644 --- a/problems/0309.最佳买卖股票时机含冷冻期.md +++ b/problems/0309.最佳买卖股票时机含冷冻期.md @@ -325,6 +325,66 @@ const maxProfit = (prices) => { }; ``` +TypeScript: + +> 版本一,与本文思路一致 + +```typescript +function maxProfit(prices: number[]): number { + /** + dp[i][0]: 持股状态; + dp[i][1]: 无股状态,当天为非冷冻期; + dp[i][2]: 无股状态,当天卖出; + dp[i][3]: 无股状态,当天为冷冻期; + */ + const length: number = prices.length; + const dp: number[][] = new Array(length).fill(0).map(_ => []); + dp[0][0] = -prices[0]; + dp[0][1] = dp[0][2] = dp[0][3] = 0; + for (let i = 1; i < length; i++) { + dp[i][0] = Math.max( + dp[i - 1][0], + Math.max(dp[i - 1][1], dp[i - 1][3]) - prices[i] + ); + dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][3]); + dp[i][2] = dp[i - 1][0] + prices[i]; + dp[i][3] = dp[i - 1][2]; + } + const lastEl: number[] = dp[length - 1]; + return Math.max(lastEl[1], lastEl[2], lastEl[3]); +}; +``` + +> 版本二,状态定义略有不同,可以帮助理解 + +```typescript +function maxProfit(prices: number[]): number { + /** + dp[i][0]: 持股状态,当天买入; + dp[i][1]: 持股状态,当天未买入; + dp[i][2]: 无股状态,当天卖出; + dp[i][3]: 无股状态,当天未卖出; + + 买入有冷冻期限制,其实就是状态[0]只能由前一天的状态[3]得到; + 如果卖出有冷冻期限制,其实就是[2]由[1]得到。 + */ + const length: number = prices.length; + const dp: number[][] = new Array(length).fill(0).map(_ => []); + dp[0][0] = -prices[0]; + dp[0][1] = -Infinity; + dp[0][2] = dp[0][3] = 0; + for (let i = 1; i < length; i++) { + dp[i][0] = dp[i - 1][3] - prices[i]; + dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0]); + dp[i][2] = Math.max(dp[i - 1][0], dp[i - 1][1]) + prices[i]; + dp[i][3] = Math.max(dp[i - 1][3], dp[i - 1][2]); + } + return Math.max(dp[length - 1][2], dp[length - 1][3]); +}; +``` + + + -----------------------
diff --git a/problems/0322.零钱兑换.md b/problems/0322.零钱兑换.md index 43c735be..fc0490c8 100644 --- a/problems/0322.零钱兑换.md +++ b/problems/0322.零钱兑换.md @@ -340,7 +340,21 @@ const coinChange = (coins, amount) => { } ``` +TypeScript: +```typescript +function coinChange(coins: number[], amount: number): number { + const dp: number[] = new Array(amount + 1).fill(Infinity); + dp[0] = 0; + for (let i = 0; i < coins.length; i++) { + for (let j = coins[i]; j <= amount; j++) { + if (dp[j - coins[i]] === Infinity) continue; + dp[j] = Math.min(dp[j], dp[j - coins[i]] + 1); + } + } + return dp[amount] === Infinity ? -1 : dp[amount]; +}; +``` -----------------------
diff --git a/problems/0337.打家劫舍III.md b/problems/0337.打家劫舍III.md index a4d8f6b2..6f50723d 100644 --- a/problems/0337.打家劫舍III.md +++ b/problems/0337.打家劫舍III.md @@ -429,7 +429,50 @@ const rob = root => { }; ``` +### TypeScript + +> 记忆化后序遍历 + +```typescript +const memory: Map = new Map(); +function rob(root: TreeNode | null): number { + if (root === null) return 0; + if (memory.has(root)) return memory.get(root); + // 不取当前节点 + const res1: number = rob(root.left) + rob(root.right); + // 取当前节点 + let res2: number = root.val; + if (root.left !== null) res2 += rob(root.left.left) + rob(root.left.right); + if (root.right !== null) res2 += rob(root.right.left) + rob(root.right.right); + const res: number = Math.max(res1, res2); + memory.set(root, res); + return res; +}; +``` + +> 状态标记化后序遍历 + +```typescript +function rob(root: TreeNode | null): number { + return Math.max(...robNode(root)); +}; +// [0]-不偷当前节点能获得的最大金额; [1]-偷~~ +type MaxValueArr = [number, number]; +function robNode(node: TreeNode | null): MaxValueArr { + if (node === null) return [0, 0]; + const leftArr: MaxValueArr = robNode(node.left); + const rightArr: MaxValueArr = robNode(node.right); + // 不偷 + const val1: number = Math.max(leftArr[0], leftArr[1]) + + Math.max(rightArr[0], rightArr[1]); + // 偷 + const val2: number = leftArr[0] + rightArr[0] + node.val; + return [val1, val2]; +} +``` + ### Go + ```go // 打家劫舍Ⅲ 动态规划 // 时间复杂度O(n) 空间复杂度O(logn) diff --git a/problems/0344.反转字符串.md b/problems/0344.反转字符串.md index 58bada05..e1f27bd7 100644 --- a/problems/0344.反转字符串.md +++ b/problems/0344.反转字符串.md @@ -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 + } + } +} +``` -----------------------
diff --git a/problems/0347.前K个高频元素.md b/problems/0347.前K个高频元素.md index 1d6a358b..20932b28 100644 --- a/problems/0347.前K个高频元素.md +++ b/problems/0347.前K个高频元素.md @@ -374,7 +374,49 @@ function topKFrequent(nums: number[], k: number): number[] { }; ``` +Scala: +解法一: 优先级队列 +```scala +object Solution { + import scala.collection.mutable + def topKFrequent(nums: Array[Int], k: Int): Array[Int] = { + val map = mutable.HashMap[Int, Int]() + // 将所有元素都放入Map + for (num <- nums) { + map.put(num, map.getOrElse(num, 0) + 1) + } + // 声明一个优先级队列,在函数柯里化那块需要指明排序方式 + var queue = mutable.PriorityQueue[(Int, Int)]()(Ordering.fromLessThan((x, y) => x._2 > y._2)) + // 将map里面的元素送入优先级队列 + for (elem <- map) { + queue.enqueue(elem) + if(queue.size > k){ + queue.dequeue // 如果队列元素大于k个,出队 + } + } + // 最终只需要key的Array形式就可以了,return关键字可以省略 + queue.map(_._1).toArray + } +} +``` +解法二: 相当于一个wordCount程序 +```scala +object Solution { + def topKFrequent(nums: Array[Int], k: Int): Array[Int] = { + // 首先将数据变为(x,1),然后按照x分组,再使用map进行转换(x,sum),变换为Array + // 再使用sort针对sum进行排序,最后take前k个,再把数据变为x,y,z这种格式 + nums.map((_, 1)).groupBy(_._1) + .map { + case (x, arr) => (x, arr.map(_._2).sum) + } + .toArray + .sortWith(_._2 > _._2) + .take(k) + .map(_._1) + } +} +``` -----------------------
diff --git a/problems/0349.两个数组的交集.md b/problems/0349.两个数组的交集.md index 45f19b6e..f7dab3d7 100644 --- a/problems/0349.两个数组的交集.md +++ b/problems/0349.两个数组的交集.md @@ -313,6 +313,69 @@ int* intersection1(int* nums1, int nums1Size, int* nums2, int nums2Size, int* re } ``` +Scala: + +正常解法: +```scala +object Solution { + def intersection(nums1: Array[Int], nums2: Array[Int]): Array[Int] = { + // 导入mutable + import scala.collection.mutable + // 临时Set,用于记录数组1出现的每个元素 + val tmpSet: mutable.HashSet[Int] = new mutable.HashSet[Int]() + // 结果Set,存储最终结果 + val resSet: mutable.HashSet[Int] = new mutable.HashSet[Int]() + // 遍历nums1,把每个元素添加到tmpSet + nums1.foreach(tmpSet.add(_)) + // 遍历nums2,如果在tmpSet存在就添加到resSet + nums2.foreach(elem => { + if (tmpSet.contains(elem)) { + resSet.add(elem) + } + }) + // 将结果转换为Array返回,return可以省略 + resSet.toArray + } +} +``` +骚操作1: +```scala +object Solution { + def intersection(nums1: Array[Int], nums2: Array[Int]): Array[Int] = { + // 先转为Set,然后取交集,最后转换为Array + (nums1.toSet).intersect(nums2.toSet).toArray + } +} +``` +骚操作2: +```scala +object Solution { + def intersection(nums1: Array[Int], nums2: Array[Int]): Array[Int] = { + // distinct去重,然后取交集 + (nums1.distinct).intersect(nums2.distinct) + } +} + + +C#: +```csharp + public int[] Intersection(int[] nums1, int[] nums2) { + if(nums1==null||nums1.Length==0||nums2==null||nums1.Length==0) + return new int[0]; //注意数组条件 + HashSet one = Insert(nums1); + HashSet two = Insert(nums2); + one.IntersectWith(two); + return one.ToArray(); + } + public HashSet Insert(int[] nums){ + HashSet one = new HashSet(); + foreach(int num in nums){ + one.Add(num); + } + return one; + } + +``` ## 相关题目 * 350.两个数组的交集 II diff --git a/problems/0377.组合总和Ⅳ.md b/problems/0377.组合总和Ⅳ.md index aaf27e61..1d808a3a 100644 --- a/problems/0377.组合总和Ⅳ.md +++ b/problems/0377.组合总和Ⅳ.md @@ -221,7 +221,27 @@ const combinationSum4 = (nums, target) => { }; ``` +TypeScript: + +```typescript +function combinationSum4(nums: number[], target: number): number { + const dp: number[] = new Array(target + 1).fill(0); + dp[0] = 1; + // 遍历背包 + for (let i = 1; i <= target; i++) { + // 遍历物品 + for (let j = 0, length = nums.length; j < length; j++) { + if (i >= nums[j]) { + dp[i] += dp[i - nums[j]]; + } + } + } + return dp[target]; +}; +``` + Rust + ```Rust impl Solution { pub fn combination_sum4(nums: Vec, target: i32) -> i32 { diff --git a/problems/0383.赎金信.md b/problems/0383.赎金信.md index 6177cc41..75dafb72 100644 --- a/problems/0383.赎金信.md +++ b/problems/0383.赎金信.md @@ -363,5 +363,87 @@ impl Solution { } ``` +Scala: + +版本一: 使用数组作为哈希表 +```scala +object Solution { + def canConstruct(ransomNote: String, magazine: String): Boolean = { + // 如果magazine的长度小于ransomNote的长度,必然是false + if (magazine.length < ransomNote.length) { + return false + } + // 定义一个数组,存储magazine字符出现的次数 + val map: Array[Int] = new Array[Int](26) + // 遍历magazine字符串,对应的字符+=1 + for (i <- magazine.indices) { + map(magazine(i) - 'a') += 1 + } + // 遍历ransomNote + for (i <- ransomNote.indices) { + if (map(ransomNote(i) - 'a') > 0) + map(ransomNote(i) - 'a') -= 1 + else return false + } + // 如果上面没有返回false,直接返回true,关键字return可以省略 + true + } +} +``` + +```scala +object Solution { + import scala.collection.mutable + def canConstruct(ransomNote: String, magazine: String): Boolean = { + // 如果magazine的长度小于ransomNote的长度,必然是false + if (magazine.length < ransomNote.length) { + return false + } + // 定义map,key是字符,value是字符出现的次数 + val map = new mutable.HashMap[Char, Int]() + // 遍历magazine,把所有的字符都记录到map里面 + for (i <- magazine.indices) { + val tmpChar = magazine(i) + // 如果map包含该字符,那么对应的value++,否则添加该字符 + if (map.contains(tmpChar)) { + map.put(tmpChar, map.get(tmpChar).get + 1) + } else { + map.put(tmpChar, 1) + } + } + // 遍历ransomNote + for (i <- ransomNote.indices) { + val tmpChar = ransomNote(i) + // 如果map包含并且该字符的value大于0,则匹配成功,map对应的--,否则直接返回false + if (map.contains(tmpChar) && map.get(tmpChar).get > 0) { + map.put(tmpChar, map.get(tmpChar).get - 1) + } else { + return false + } + } + // 如果上面没有返回false,直接返回true,关键字return可以省略 + true + } +} + + +C#: +```csharp +public bool CanConstruct(string ransomNote, string magazine) { + if(ransomNote.Length > magazine.Length) return false; + int[] letters = new int[26]; + foreach(char c in magazine){ + letters[c-'a']++; + } + foreach(char c in ransomNote){ + letters[c-'a']--; + if(letters[c-'a']<0){ + return false; + } + } + return true; + } + +``` -----------------------
diff --git a/problems/0404.左叶子之和.md b/problems/0404.左叶子之和.md index d7fd629e..4ef68d3b 100644 --- a/problems/0404.左叶子之和.md +++ b/problems/0404.左叶子之和.md @@ -336,8 +336,8 @@ var sumOfLeftLeaves = function(root) { if(node===null){ return 0; } - let leftValue = sumOfLeftLeaves(node.left); - let rightValue = sumOfLeftLeaves(node.right); + let leftValue = nodesSum(node.left); + let rightValue = nodesSum(node.right); // 3. 单层递归逻辑 let midValue = 0; if(node.left&&node.left.left===null&&node.left.right===null){ diff --git a/problems/0416.分割等和子集.md b/problems/0416.分割等和子集.md index 6e93ae8e..eb6601e1 100644 --- a/problems/0416.分割等和子集.md +++ b/problems/0416.分割等和子集.md @@ -417,6 +417,163 @@ var canPartition = function(nums) { ``` +C: +二维dp: +```c +/** +1. dp数组含义:dp[i][j]为背包重量为j时,从[0-i]元素和最大值 +2. 递推公式:dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - nums[i]] + nums[i]) +3. 初始化:dp[i][0]初始化为0。因为背包重量为0时,不可能放入元素。dp[0][j] = nums[0],当j >= nums[0] && j < target时 +4. 遍历顺序:先遍历物品,再遍历背包 +*/ +#define MAX(a, b) (((a) > (b)) ? (a) : (b)) + +int getSum(int* nums, int numsSize) { + int sum = 0; + + int i; + for(i = 0; i < numsSize; ++i) { + sum += nums[i]; + } + return sum; +} + +bool canPartition(int* nums, int numsSize){ + // 求出元素总和 + int sum = getSum(nums, numsSize); + // 若元素总和为奇数,则不可能得到两个和相等的子数组 + if(sum % 2) + return false; + + // 若子数组的和等于target,则nums可以被分割 + int target = sum / 2; + // 初始化dp数组 + int dp[numsSize][target + 1]; + // dp[j][0]都应被设置为0。因为当背包重量为0时,不可放入元素 + memset(dp, 0, sizeof(int) * numsSize * (target + 1)); + + int i, j; + // 当背包重量j大于nums[0]时,可以在dp[0][j]中放入元素nums[0] + for(j = nums[0]; j <= target; ++j) { + dp[0][j] = nums[0]; + } + + for(i = 1; i < numsSize; ++i) { + for(j = 1; j <= target; ++j) { + // 若当前背包重量j小于nums[i],则其值等于只考虑0到i-1物品时的值 + if(j < nums[i]) + dp[i][j] = dp[i - 1][j]; + // 否则,背包重量等于在背包中放入num[i]/不放入nums[i]的较大值 + else + dp[i][j] = MAX(dp[i - 1][j], dp[i - 1][j - nums[i]] + nums[i]); + } + } + // 判断背包重量为target,且考虑到所有物品时,放入的元素和是否等于target + return dp[numsSize - 1][target] == target; +} +``` +滚动数组: +```c +/** +1. dp数组含义:dp[j]为背包重量为j时,其中可放入元素的最大值 +2. 递推公式:dp[j] = max(dp[j], dp[j - nums[i]] + nums[i]) +3. 初始化:均初始化为0即可 +4. 遍历顺序:先遍历物品,再后序遍历背包 +*/ +#define MAX(a, b) (((a) > (b)) ? (a) : (b)) + +int getSum(int* nums, int numsSize) { + int sum = 0; + + int i; + for(i = 0; i < numsSize; ++i) { + sum += nums[i]; + } + return sum; +} + +bool canPartition(int* nums, int numsSize){ + // 求出元素总和 + int sum = getSum(nums, numsSize); + // 若元素总和为奇数,则不可能得到两个和相等的子数组 + if(sum % 2) + return false; + // 背包容量 + int target = sum / 2; + + // 初始化dp数组,元素均为0 + int dp[target + 1]; + memset(dp, 0, sizeof(int) * (target + 1)); + + int i, j; + // 先遍历物品,后遍历背包 + for(i = 0; i < numsSize; ++i) { + for(j = target; j >= nums[i]; --j) { + dp[j] = MAX(dp[j], dp[j - nums[i]] + nums[i]); + } + } + + // 查看背包容量为target时,元素总和是否等于target + return dp[target] == target; +} +``` + +TypeScript: + +> 一维数组,简洁 + +```typescript +function canPartition(nums: number[]): boolean { + const sum: number = nums.reduce((pre, cur) => pre + cur); + if (sum % 2 === 1) return false; + const bagSize: number = sum / 2; + const goodsNum: number = nums.length; + const dp: number[] = new Array(bagSize + 1).fill(0); + for (let i = 0; i < goodsNum; i++) { + for (let j = bagSize; j >= nums[i]; j--) { + dp[j] = Math.max(dp[j], dp[j - nums[i]] + nums[i]); + } + } + return dp[bagSize] === bagSize; +}; +``` + +> 二维数组,易懂 + +```typescript +function canPartition(nums: number[]): boolean { + /** + weightArr = nums; + valueArr = nums; + bagSize = sum / 2; (sum为nums各元素总和); + 按照0-1背包处理 + */ + const sum: number = nums.reduce((pre, cur) => pre + cur); + if (sum % 2 === 1) return false; + const bagSize: number = sum / 2; + const weightArr: number[] = nums; + const valueArr: number[] = nums; + const goodsNum: number = weightArr.length; + const dp: number[][] = new Array(goodsNum) + .fill(0) + .map(_ => new Array(bagSize + 1).fill(0)); + for (let i = weightArr[0]; i <= bagSize; i++) { + dp[0][i] = valueArr[0]; + } + for (let i = 1; i < goodsNum; i++) { + for (let j = 0; j <= bagSize; j++) { + if (j < weightArr[i]) { + dp[i][j] = dp[i - 1][j]; + } else { + dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - weightArr[i]] + valueArr[i]); + } + } + } + return dp[goodsNum - 1][bagSize] === bagSize; +}; +``` + + ----------------------- diff --git a/problems/0454.四数相加II.md b/problems/0454.四数相加II.md index a6cd413b..bfdee26e 100644 --- a/problems/0454.四数相加II.md +++ b/problems/0454.四数相加II.md @@ -318,5 +318,69 @@ impl Solution { } ``` + +Scala: +```scala +object Solution { + // 导包 + import scala.collection.mutable + def fourSumCount(nums1: Array[Int], nums2: Array[Int], nums3: Array[Int], nums4: Array[Int]): Int = { + // 定义一个HashMap,key存储值,value存储该值出现的次数 + val map = new mutable.HashMap[Int, Int]() + // 遍历前两个数组,把他们所有可能的情况都记录到map + for (i <- nums1.indices) { + for (j <- nums2.indices) { + val tmp = nums1(i) + nums2(j) + // 如果包含该值,则对他的key加1,不包含则添加进去 + if (map.contains(tmp)) { + map.put(tmp, map.get(tmp).get + 1) + } else { + map.put(tmp, 1) + } + } + } + var res = 0 // 结果变量 + // 遍历后两个数组 + for (i <- nums3.indices) { + for (j <- nums4.indices) { + val tmp = -(nums3(i) + nums4(j)) + // 如果map中存在该值,结果就+=value + if (map.contains(tmp)) { + res += map.get(tmp).get + } + } + } + // 返回最终结果,可以省略关键字return + res + } +} + +C#: +```csharp +public int FourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) { + Dictionary dic = new Dictionary(); + foreach(var i in nums1){ + foreach(var j in nums2){ + int sum = i + j; + if(dic.ContainsKey(sum)){ + dic[sum]++; + }else{ + dic.Add(sum, 1); + } + + } + } + int res = 0; + foreach(var a in nums3){ + foreach(var b in nums4){ + int sum = a+b; + if(dic.TryGetValue(-sum, out var result)){ + res += result; + } + } + } + return res; + } +``` -----------------------
diff --git a/problems/0474.一和零.md b/problems/0474.一和零.md index 964df4a8..d38ce03f 100644 --- a/problems/0474.一和零.md +++ b/problems/0474.一和零.md @@ -323,6 +323,129 @@ const findMaxForm = (strs, m, n) => { }; ``` +TypeScript: + +> 滚动数组,二维数组法 + +```typescript +type BinaryInfo = { numOfZero: number, numOfOne: number }; +function findMaxForm(strs: string[], m: number, n: number): number { + const goodsNum: number = strs.length; + const dp: number[][] = new Array(m + 1).fill(0) + .map(_ => new Array(n + 1).fill(0)); + for (let i = 0; i < goodsNum; i++) { + const { numOfZero, numOfOne } = countBinary(strs[i]); + for (let j = m; j >= numOfZero; j--) { + for (let k = n; k >= numOfOne; k--) { + dp[j][k] = Math.max(dp[j][k], dp[j - numOfZero][k - numOfOne] + 1); + } + } + } + return dp[m][n]; +}; +function countBinary(str: string): BinaryInfo { + let numOfZero: number = 0, + numOfOne: number = 0; + for (let s of str) { + if (s === '0') { + numOfZero++; + } else { + numOfOne++; + } + } + return { numOfZero, numOfOne }; +} +``` + +> 传统背包,三维数组法 + +```typescript +type BinaryInfo = { numOfZero: number, numOfOne: number }; +function findMaxForm(strs: string[], m: number, n: number): number { + /** + dp[i][j][k]: 前i个物品中, 背包的0容量为j, 1容量为k, 最多能放的物品数量 + */ + const goodsNum: number = strs.length; + const dp: number[][][] = new Array(goodsNum).fill(0) + .map(_ => new Array(m + 1) + .fill(0) + .map(_ => new Array(n + 1).fill(0)) + ); + const { numOfZero, numOfOne } = countBinary(strs[0]); + for (let i = numOfZero; i <= m; i++) { + for (let j = numOfOne; j <= n; j++) { + dp[0][i][j] = 1; + } + } + for (let i = 1; i < goodsNum; i++) { + const { numOfZero, numOfOne } = countBinary(strs[i]); + for (let j = 0; j <= m; j++) { + for (let k = 0; k <= n; k++) { + if (j < numOfZero || k < numOfOne) { + dp[i][j][k] = dp[i - 1][j][k]; + } else { + dp[i][j][k] = Math.max(dp[i - 1][j][k], dp[i - 1][j - numOfZero][k - numOfOne] + 1); + } + } + } + } + return dp[dp.length - 1][m][n]; +}; +function countBinary(str: string): BinaryInfo { + let numOfZero: number = 0, + numOfOne: number = 0; + for (let s of str) { + if (s === '0') { + numOfZero++; + } else { + numOfOne++; + } + } + return { numOfZero, numOfOne }; +} +``` + +> 回溯法(会超时) + +```typescript +function findMaxForm(strs: string[], m: number, n: number): number { + /** + 思路:暴力枚举strs的所有子集,记录符合条件子集的最大长度 + */ + let resMax: number = 0; + backTrack(strs, m, n, 0, []); + return resMax; + function backTrack( + strs: string[], m: number, n: number, + startIndex: number, route: string[] + ): void { + if (startIndex === strs.length) return; + for (let i = startIndex, length = strs.length; i < length; i++) { + route.push(strs[i]); + if (isValidSubSet(route, m, n)) { + resMax = Math.max(resMax, route.length); + backTrack(strs, m, n, i + 1, route); + } + route.pop(); + } + } +}; +function isValidSubSet(strs: string[], m: number, n: number): boolean { + let zeroNum: number = 0, + oneNum: number = 0; + strs.forEach(str => { + for (let s of str) { + if (s === '0') { + zeroNum++; + } else { + oneNum++; + } + } + }); + return zeroNum <= m && oneNum <= n; +} +``` + ----------------------- diff --git a/problems/0494.目标和.md b/problems/0494.目标和.md index 99b76834..8ce1f6f1 100644 --- a/problems/0494.目标和.md +++ b/problems/0494.目标和.md @@ -351,6 +351,25 @@ const findTargetSumWays = (nums, target) => { }; ``` +TypeScript: + +```typescript +function findTargetSumWays(nums: number[], target: number): number { + const sum: number = nums.reduce((pre, cur) => pre + cur); + if (Math.abs(target) > sum) return 0; + if ((target + sum) % 2 === 1) return 0; + const bagSize: number = (target + sum) / 2; + const dp: number[] = new Array(bagSize + 1).fill(0); + dp[0] = 1; + for (let i = 0; i < nums.length; i++) { + for (let j = bagSize; j >= nums[i]; j--) { + dp[j] += dp[j - nums[i]]; + } + } + return dp[bagSize]; +}; +``` + ----------------------- diff --git a/problems/0516.最长回文子序列.md b/problems/0516.最长回文子序列.md index 69536cef..63120b14 100644 --- a/problems/0516.最长回文子序列.md +++ b/problems/0516.最长回文子序列.md @@ -186,29 +186,28 @@ class Solution: Go: ```Go func longestPalindromeSubseq(s string) int { - lenth:=len(s) - dp:=make([][]int,lenth) - for i:=0;i b { + return a + } + return b + } + dp := make([][]int, size) + for i := 0; i < size; i++ { + dp[i] = make([]int, size) + dp[i][i] = 1 + } + for i := size - 1; i >= 0; i-- { + for j := i + 1; j < size; j++ { + if s[i] == s[j] { + dp[i][j] = dp[i+1][j-1] + 2 + } else { + dp[i][j] = max(dp[i][j-1], dp[i+1][j]) } } } - for i:=lenth-1;i>=0;i--{ - for j:=i+1;j { } ``` +TypeScript: + +```typescript +function change(amount: number, coins: number[]): number { + const dp: number[] = new Array(amount + 1).fill(0); + dp[0] = 1; + for (let i = 0, length = coins.length; i < length; i++) { + for (let j = coins[i]; j <= amount; j++) { + dp[j] += dp[j - coins[i]]; + } + } + return dp[amount]; +}; +``` + ----------------------- diff --git a/problems/0541.反转字符串II.md b/problems/0541.反转字符串II.md index 8c13a390..99d6ebe0 100644 --- a/problems/0541.反转字符串II.md +++ b/problems/0541.反转字符串II.md @@ -347,6 +347,47 @@ public class Solution } } ``` +Scala: +版本一: (正常解法) +```scala +object Solution { + def reverseStr(s: String, k: Int): String = { + val res = s.toCharArray // 转换为Array好处理 + for (i <- s.indices by 2 * k) { + // 如果i+k大于了res的长度,则需要全部翻转 + if (i + k > res.length) { + reverse(res, i, s.length - 1) + } else { + reverse(res, i, i + k - 1) + } + } + new String(res) + } + // 翻转字符串,从start到end + def reverse(s: Array[Char], start: Int, end: Int): Unit = { + var (left, right) = (start, end) + while (left < right) { + var tmp = s(left) + s(left) = s(right) + s(right) = tmp + left += 1 + right -= 1 + } + } +} +``` +版本二: 首先利用sliding每隔k个进行分割,随后转换为数组,再使用zipWithIndex添加每个数组的索引,紧接着利用map做变换,如果索引%2==0则说明需要翻转,否则原封不动,最后再转换为String +```scala +object Solution { + def reverseStr(s: String, k: Int): String = { + s.sliding(k, k) + .toArray + .zipWithIndex + .map(v => if (v._2 % 2 == 0) v._1.reverse else v._1) + .mkString + } +} +``` -----------------------
diff --git a/problems/0674.最长连续递增序列.md b/problems/0674.最长连续递增序列.md index 4f571d09..b1ed80c3 100644 --- a/problems/0674.最长连续递增序列.md +++ b/problems/0674.最长连续递增序列.md @@ -338,6 +338,45 @@ const findLengthOfLCIS = (nums) => { }; ``` +TypeScript: + +> 动态规划: + +```typescript +function findLengthOfLCIS(nums: number[]): number { + /** + dp[i]: 前i个元素,以nums[i]结尾,最长连续子序列的长度 + */ + const dp: number[] = new Array(nums.length).fill(1); + let resMax: number = 1; + for (let i = 1, length = nums.length; i < length; i++) { + if (nums[i] > nums[i - 1]) { + dp[i] = dp[i - 1] + 1; + } + resMax = Math.max(resMax, dp[i]); + } + return resMax; +}; +``` + +> 贪心: + +```typescript +function findLengthOfLCIS(nums: number[]): number { + let resMax: number = 1; + let count: number = 1; + for (let i = 0, length = nums.length; i < length - 1; i++) { + if (nums[i] < nums[i + 1]) { + count++; + } else { + count = 1; + } + resMax = Math.max(resMax, count); + } + return resMax; +}; +``` + diff --git a/problems/0701.二叉搜索树中的插入操作.md b/problems/0701.二叉搜索树中的插入操作.md index df6a3954..50e39ade 100644 --- a/problems/0701.二叉搜索树中的插入操作.md +++ b/problems/0701.二叉搜索树中的插入操作.md @@ -279,7 +279,7 @@ class Solution: root.right = self.insertIntoBST(root.right, val) # 返回更新后的以当前root为根节点的新树 - return roo + return root ``` **递归法** - 无返回值 diff --git a/problems/0704.二分查找.md b/problems/0704.二分查找.md index ce8253af..fed25393 100644 --- a/problems/0704.二分查找.md +++ b/problems/0704.二分查找.md @@ -612,7 +612,48 @@ public class Solution{ } } ``` +**Scala:** +(版本一)左闭右闭区间 +```scala +object Solution { + def search(nums: Array[Int], target: Int): Int = { + var left = 0 + var right = nums.length - 1 + while (left <= right) { + var mid = left + ((right - left) / 2) + if (target == nums(mid)) { + return mid + } else if (target < nums(mid)) { + right = mid - 1 + } else { + left = mid + 1 + } + } + -1 + } +} +``` +(版本二)左闭右开区间 +```scala +object Solution { + def search(nums: Array[Int], target: Int): Int = { + var left = 0 + var right = nums.length + while (left < right) { + val mid = left + (right - left) / 2 + if (target == nums(mid)) { + return mid + } else if (target < nums(mid)) { + right = mid + } else { + left = mid + 1 + } + } + -1 + } +} +``` -----------------------
diff --git a/problems/0707.设计链表.md b/problems/0707.设计链表.md index 37ce15ad..dcdb53f4 100644 --- a/problems/0707.设计链表.md +++ b/problems/0707.设计链表.md @@ -1154,7 +1154,75 @@ class MyLinkedList { } ``` +Scala: +```scala +class ListNode(_x: Int = 0, _next: ListNode = null) { + var next: ListNode = _next + var x: Int = _x +} +class MyLinkedList() { + + var size = 0 // 链表尺寸 + var dummy: ListNode = new ListNode(0) // 虚拟头节点 + + // 获取第index个节点的值 + def get(index: Int): Int = { + if (index < 0 || index >= size) { + return -1; + } + var cur = dummy + for (i <- 0 to index) { + cur = cur.next + } + cur.x // 返回cur的值 + } + + // 在链表最前面插入一个节点 + def addAtHead(`val`: Int) { + addAtIndex(0, `val`) + } + + // 在链表最后面插入一个节点 + def addAtTail(`val`: Int) { + addAtIndex(size, `val`) + } + + // 在第index个节点之前插入一个新节点 + // 如果index等于链表长度,则说明新插入的节点是尾巴 + // 如果index等于0,则说明新插入的节点是头 + // 如果index>链表长度,则说明为空 + def addAtIndex(index: Int, `val`: Int) { + if (index > size) { + return + } + var loc = index // 因为参数index是val不可变类型,所以需要赋值给一个可变类型 + if (index < 0) { + loc = 0 + } + size += 1 //链表尺寸+1 + var pre = dummy + for (i <- 0 until loc) { + pre = pre.next + } + val node: ListNode = new ListNode(`val`, pre.next) + pre.next = node + } + // 删除第index个节点 + def deleteAtIndex(index: Int) { + if (index < 0 || index >= size) { + return + } + size -= 1 + var pre = dummy + for (i <- 0 until index) { + pre = pre.next + } + pre.next = pre.next.next + } + +} +``` ----------------------- diff --git a/problems/0714.买卖股票的最佳时机含手续费(动态规划).md b/problems/0714.买卖股票的最佳时机含手续费(动态规划).md index 4ab63e79..5625604b 100644 --- a/problems/0714.买卖股票的最佳时机含手续费(动态规划).md +++ b/problems/0714.买卖股票的最佳时机含手续费(动态规划).md @@ -200,6 +200,29 @@ const maxProfit = (prices,fee) => { } ``` +TypeScript: + +```typescript +function maxProfit(prices: number[], fee: number): number { + /** + dp[i][0]:持有股票 + dp[i][1]: 不持有 + */ + const length: number = prices.length; + if (length === 0) return 0; + const dp: number[][] = new Array(length).fill(0).map(_ => []); + dp[0][0] = -prices[0]; + dp[0][1] = 0; + for (let i = 1; i < length; i++) { + dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] - prices[i]); + dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] + prices[i] - fee); + } + return dp[length - 1][1]; +}; +``` + + + -----------------------
diff --git a/problems/0739.每日温度.md b/problems/0739.每日温度.md index 58edd489..987ce27e 100644 --- a/problems/0739.每日温度.md +++ b/problems/0739.每日温度.md @@ -34,7 +34,8 @@ 那么单调栈的原理是什么呢?为什么时间复杂度是O(n)就可以找到每一个元素的右边第一个比它大的元素位置呢? -单调栈的本质是空间换时间,因为在遍历的过程中需要用一个栈来记录右边第一个比当前元素大的元素,优点是只需要遍历一次。 + +单调栈的本质是空间换时间,因为在遍历的过程中需要用一个栈来记录右边第一个比当前元素高的元素,优点是只需要遍历一次。 在使用单调栈的时候首先要明确如下几点: @@ -192,7 +193,7 @@ class Solution { 否则的话,可以直接入栈。 注意,单调栈里 加入的元素是 下标。 */ - Stackstack=new Stack<>(); + Deque stack=new LinkedList<>(); stack.push(0); for(int i=1;istack=new Stack<>(); + Deque stack=new LinkedList<>(); for(int i=0;itemperatures[stack.peek()]){ diff --git a/problems/0977.有序数组的平方.md b/problems/0977.有序数组的平方.md index 8811f3d7..bfe8cda1 100644 --- a/problems/0977.有序数组的平方.md +++ b/problems/0977.有序数组的平方.md @@ -360,7 +360,41 @@ class Solution { } } ``` +Scala: +双指针: +```scala +object Solution { + def sortedSquares(nums: Array[Int]): Array[Int] = { + val res: Array[Int] = new Array[Int](nums.length) + var top = nums.length - 1 + var i = 0 + var j = nums.length - 1 + while (i <= j) { + if (nums(i) * nums(i) <= nums(j) * nums(j)) { + // 当左侧平方小于等于右侧,res数组顶部放右侧的平方,并且top下移,j左移 + res(top) = nums(j) * nums(j) + top -= 1 + j -= 1 + } else { + // 当左侧平方大于右侧,res数组顶部放左侧的平方,并且top下移,i右移 + res(top) = nums(i) * nums(i) + top -= 1 + i += 1 + } + } + res + } +} +``` +骚操作(暴力思路): +```scala +object Solution { + def sortedSquares(nums: Array[Int]): Array[Int] = { + nums.map(x=>{x*x}).sortWith(_ < _) + } +} +``` ----------------------- diff --git a/problems/1002.查找常用字符.md b/problems/1002.查找常用字符.md index 36937b0b..075b5ef1 100644 --- a/problems/1002.查找常用字符.md +++ b/problems/1002.查找常用字符.md @@ -418,6 +418,38 @@ char ** commonChars(char ** words, int wordsSize, int* returnSize){ return ret; } ``` - +Scala: +```scala +object Solution { + def commonChars(words: Array[String]): List[String] = { + // 声明返回结果的不可变List集合,因为res要重新赋值,所以声明为var + var res = List[String]() + var hash = new Array[Int](26) // 统计字符出现的最小频率 + // 统计第一个字符串中字符出现的次数 + for (i <- 0 until words(0).length) { + hash(words(0)(i) - 'a') += 1 + } + // 统计其他字符串出现的频率 + for (i <- 1 until words.length) { + // 统计其他字符出现的频率 + var hashOtherStr = new Array[Int](26) + for (j <- 0 until words(i).length) { + hashOtherStr(words(i)(j) - 'a') += 1 + } + // 更新hash,取26个字母最小出现的频率 + for (k <- 0 until 26) { + hash(k) = math.min(hash(k), hashOtherStr(k)) + } + } + // 根据hash的结果转换输出的形式 + for (i <- 0 until 26) { + for (j <- 0 until hash(i)) { + res = res :+ (i + 'a').toChar.toString + } + } + res + } +} +``` -----------------------
diff --git a/problems/1005.K次取反后最大化的数组和.md b/problems/1005.K次取反后最大化的数组和.md index 79767deb..202534da 100644 --- a/problems/1005.K次取反后最大化的数组和.md +++ b/problems/1005.K次取反后最大化的数组和.md @@ -209,6 +209,22 @@ var largestSumAfterKNegations = function(nums, k) { return a + b }) }; + +// 版本二 (优化: 一次遍历) +var largestSumAfterKNegations = function(nums, k) { + nums.sort((a, b) => Math.abs(b) - Math.abs(a)); // 排序 + let sum = 0; + for(let i = 0; i < nums.length; i++) { + if(nums[i] < 0 && k-- > 0) { // 负数取反(k 数量足够时) + nums[i] = -nums[i]; + } + sum += nums[i]; // 求和 + } + if(k % 2 > 0) { // k 有多余的(k若消耗完则应为 -1) + sum -= 2 * nums[nums.length - 1]; // 减去两倍的最小值(因为之前加过一次) + } + return sum; +}; ``` diff --git a/problems/1047.删除字符串中的所有相邻重复项.md b/problems/1047.删除字符串中的所有相邻重复项.md index 638c8f4e..a92a3911 100644 --- a/problems/1047.删除字符串中的所有相邻重复项.md +++ b/problems/1047.删除字符串中的所有相邻重复项.md @@ -374,6 +374,27 @@ func removeDuplicates(_ s: String) -> String { return String(stack) } ``` - +Scala: +```scala +object Solution { + import scala.collection.mutable + def removeDuplicates(s: String): String = { + var stack = mutable.Stack[Int]() + var str = "" // 保存最终结果 + for (i <- s.indices) { + var tmp = s(i) + // 如果栈非空并且栈顶元素等于当前字符,那么删掉栈顶和字符串最后一个元素 + if (!stack.isEmpty && tmp == stack.head) { + str = str.take(str.length - 1) + stack.pop() + } else { + stack.push(tmp) + str += tmp + } + } + str + } +} +``` -----------------------
diff --git a/problems/1049.最后一块石头的重量II.md b/problems/1049.最后一块石头的重量II.md index ee0ddef2..3d256c3d 100644 --- a/problems/1049.最后一块石头的重量II.md +++ b/problems/1049.最后一块石头的重量II.md @@ -277,5 +277,26 @@ var lastStoneWeightII = function (stones) { }; ``` +TypeScript: + +```typescript +function lastStoneWeightII(stones: number[]): number { + const sum: number = stones.reduce((pre, cur) => pre + cur); + const bagSize: number = Math.floor(sum / 2); + const weightArr: number[] = stones; + const valueArr: number[] = stones; + const goodsNum: number = weightArr.length; + const dp: number[] = new Array(bagSize + 1).fill(0); + for (let i = 0; i < goodsNum; i++) { + for (let j = bagSize; j >= weightArr[i]; j--) { + dp[j] = Math.max(dp[j], dp[j - weightArr[i]] + valueArr[i]); + } + } + return sum - dp[bagSize] * 2; +}; +``` + + + -----------------------
diff --git a/problems/二叉树理论基础.md b/problems/二叉树理论基础.md index 9c151e32..9e10ac20 100644 --- a/problems/二叉树理论基础.md +++ b/problems/二叉树理论基础.md @@ -258,6 +258,13 @@ class TreeNode { } } ``` - +Scala: +```scala +class TreeNode(_value: Int = 0, _left: TreeNode = null, _right: TreeNode = null) { + var value: Int = _value + var left: TreeNode = _left + var right: TreeNode = _right +} +``` -----------------------
diff --git a/problems/二叉树的统一迭代法.md b/problems/二叉树的统一迭代法.md index f6edf586..9ca6ac39 100644 --- a/problems/二叉树的统一迭代法.md +++ b/problems/二叉树的统一迭代法.md @@ -591,6 +591,80 @@ function postorderTraversal(root: TreeNode | null): number[] { return res; }; ``` +Scala: +```scala +// 前序遍历 +object Solution { + import scala.collection.mutable + def preorderTraversal(root: TreeNode): List[Int] = { + val res = mutable.ListBuffer[Int]() + val stack = mutable.Stack[TreeNode]() + if (root != null) stack.push(root) + while (!stack.isEmpty) { + var curNode = stack.top + if (curNode != null) { + stack.pop() + if (curNode.right != null) stack.push(curNode.right) + if (curNode.left != null) stack.push(curNode.left) + stack.push(curNode) + stack.push(null) + } else { + stack.pop() + res.append(stack.pop().value) + } + } + res.toList + } +} +// 中序遍历 +object Solution { + import scala.collection.mutable + def inorderTraversal(root: TreeNode): List[Int] = { + val res = mutable.ListBuffer[Int]() + val stack = mutable.Stack[TreeNode]() + if (root != null) stack.push(root) + while (!stack.isEmpty) { + var curNode = stack.top + if (curNode != null) { + stack.pop() + if (curNode.right != null) stack.push(curNode.right) + stack.push(curNode) + stack.push(null) + if (curNode.left != null) stack.push(curNode.left) + } else { + // 等于空的时候好办,弹出这个元素 + stack.pop() + res.append(stack.pop().value) + } + } + res.toList + } +} + +// 后序遍历 +object Solution { + import scala.collection.mutable + def postorderTraversal(root: TreeNode): List[Int] = { + val res = mutable.ListBuffer[Int]() + val stack = mutable.Stack[TreeNode]() + if (root != null) stack.push(root) + while (!stack.isEmpty) { + var curNode = stack.top + if (curNode != null) { + stack.pop() + stack.push(curNode) + stack.push(null) + if (curNode.right != null) stack.push(curNode.right) + if (curNode.left != null) stack.push(curNode.left) + } else { + stack.pop() + res.append(stack.pop().value) + } + } + res.toList + } +} +``` -----------------------
diff --git a/problems/二叉树的迭代遍历.md b/problems/二叉树的迭代遍历.md index 13ba5f1e..fac30f99 100644 --- a/problems/二叉树的迭代遍历.md +++ b/problems/二叉树的迭代遍历.md @@ -568,6 +568,71 @@ func inorderTraversal(_ root: TreeNode?) -> [Int] { return result } ``` +Scala: +```scala +// 前序遍历(迭代法) +object Solution { + import scala.collection.mutable + def preorderTraversal(root: TreeNode): List[Int] = { + val res = mutable.ListBuffer[Int]() + if (root == null) return res.toList + // 声明一个栈,泛型为TreeNode + val stack = mutable.Stack[TreeNode]() + stack.push(root) // 先把根节点压入栈 + while (!stack.isEmpty) { + var curNode = stack.pop() + res.append(curNode.value) // 先把这个值压入栈 + // 如果当前节点的左右节点不为空,则入栈,先放右节点,再放左节点 + if (curNode.right != null) stack.push(curNode.right) + if (curNode.left != null) stack.push(curNode.left) + } + res.toList + } +} +// 中序遍历(迭代法) +object Solution { + import scala.collection.mutable + def inorderTraversal(root: TreeNode): List[Int] = { + val res = mutable.ArrayBuffer[Int]() + if (root == null) return res.toList + val stack = mutable.Stack[TreeNode]() + var curNode = root + // 将左节点都入栈,当遍历到最左(到空)的时候,再弹出栈顶元素,加入res + // 再把栈顶元素的右节点加进来,继续下一轮遍历 + while (curNode != null || !stack.isEmpty) { + if (curNode != null) { + stack.push(curNode) + curNode = curNode.left + } else { + curNode = stack.pop() + res.append(curNode.value) + curNode = curNode.right + } + } + res.toList + } +} + +// 后序遍历(迭代法) +object Solution { + import scala.collection.mutable + def postorderTraversal(root: TreeNode): List[Int] = { + val res = mutable.ListBuffer[Int]() + if (root == null) return res.toList + val stack = mutable.Stack[TreeNode]() + stack.push(root) + while (!stack.isEmpty) { + val curNode = stack.pop() + res.append(curNode.value) + // 这次左节点先入栈,右节点再入栈 + if(curNode.left != null) stack.push(curNode.left) + if(curNode.right != null) stack.push(curNode.right) + } + // 最后需要翻转List + res.reverse.toList + } +} +``` -----------------------
diff --git a/problems/二叉树的递归遍历.md b/problems/二叉树的递归遍历.md index 186c39d3..29c0cfda 100644 --- a/problems/二叉树的递归遍历.md +++ b/problems/二叉树的递归遍历.md @@ -470,5 +470,56 @@ func postorder(_ root: TreeNode?, res: inout [Int]) { res.append(root!.val) } ``` +Scala: 前序遍历:(144.二叉树的前序遍历) +```scala +object Solution { + import scala.collection.mutable.ListBuffer + def preorderTraversal(root: TreeNode): List[Int] = { + val res = ListBuffer[Int]() + def traversal(curNode: TreeNode): Unit = { + if(curNode == null) return + res.append(curNode.value) + traversal(curNode.left) + traversal(curNode.right) + } + traversal(root) + res.toList + } +} +``` +中序遍历:(94. 二叉树的中序遍历) +```scala +object Solution { + import scala.collection.mutable.ListBuffer + def inorderTraversal(root: TreeNode): List[Int] = { + val res = ListBuffer[Int]() + def traversal(curNode: TreeNode): Unit = { + if(curNode == null) return + traversal(curNode.left) + res.append(curNode.value) + traversal(curNode.right) + } + traversal(root) + res.toList + } +} +``` +后序遍历:(145. 二叉树的后序遍历) +```scala +object Solution { + import scala.collection.mutable.ListBuffer + def postorderTraversal(root: TreeNode): List[Int] = { + val res = ListBuffer[Int]() + def traversal(curNode: TreeNode): Unit = { + if (curNode == null) return + traversal(curNode.left) + traversal(curNode.right) + res.append(curNode.value) + } + traversal(root) + res.toList + } +} +``` -----------------------
diff --git a/problems/剑指Offer05.替换空格.md b/problems/剑指Offer05.替换空格.md index 037bd427..eecd7f0c 100644 --- a/problems/剑指Offer05.替换空格.md +++ b/problems/剑指Offer05.替换空格.md @@ -413,8 +413,62 @@ func replaceSpace(_ s: String) -> String { } ``` +Scala: - +方式一: 双指针 +```scala +object Solution { + def replaceSpace(s: String): String = { + var count = 0 + s.foreach(c => if (c == ' ') count += 1) // 统计空格的数量 + val sOldSize = s.length // 旧数组字符串长度 + val sNewSize = s.length + count * 2 // 新数组字符串长度 + val res = new Array[Char](sNewSize) // 新数组 + var index = sNewSize - 1 // 新数组索引 + // 逆序遍历 + for (i <- (0 until sOldSize).reverse) { + if (s(i) == ' ') { + res(index) = '0' + index -= 1 + res(index) = '2' + index -= 1 + res(index) = '%' + } else { + res(index) = s(i) + } + index -= 1 + } + res.mkString + } +} +``` +方式二: 使用一个集合,遇到空格就添加%20 +```scala +object Solution { + import scala.collection.mutable.ListBuffer + def replaceSpace(s: String): String = { + val res: ListBuffer[Char] = ListBuffer[Char]() + for (i <- s.indices) { + if (s(i) == ' ') { + res += '%' + res += '2' + res += '0' + }else{ + res += s(i) + } + } + res.mkString + } +} +``` +方式三: 使用map +```scala +object Solution { + def replaceSpace(s: String): String = { + s.map(c => if(c == ' ') "%20" else c).mkString + } +} +``` ----------------------- diff --git a/problems/剑指Offer58-II.左旋转字符串.md b/problems/剑指Offer58-II.左旋转字符串.md index fec83e1d..7c39ed69 100644 --- a/problems/剑指Offer58-II.左旋转字符串.md +++ b/problems/剑指Offer58-II.左旋转字符串.md @@ -291,6 +291,56 @@ func reverseString(_ s: inout [Character], startIndex: Int, endIndex: Int) { ``` +### PHP + +```php +function reverseLeftWords($s, $n) { + $this->reverse($s,0,$n-1); //反转区间为前n的子串 + $this->reverse($s,$n,strlen($s)-1); //反转区间为n到末尾的子串 + $this->reverse($s,0,strlen($s)-1); //反转整个字符串 + return $s; +} + +// 按指定进行翻转 【array、string都可】 +function reverse(&$s, $start, $end) { + for ($i = $start, $j = $end; $i < $j; $i++, $j--) { + $tmp = $s[$i]; + $s[$i] = $s[$j]; + $s[$j] = $tmp; + } +} +``` + + +Scala: + +```scala +object Solution { + def reverseLeftWords(s: String, n: Int): String = { + var str = s.toCharArray // 转换为Array + // abcdefg => ba cdefg + reverseString(str, 0, n - 1) + // ba cdefg => ba gfedc + reverseString(str, n, str.length - 1) + // ba gfedc => cdefgab + reverseString(str, 0, str.length - 1) + // 最终返回,return关键字可以省略 + new String(str) + } + // 翻转字符串 + def reverseString(s: Array[Char], start: Int, end: Int): Unit = { + var (left, right) = (start, end) + while (left < right) { + var tmp = s(left) + s(left) = s(right) + s(right) = tmp + left += 1 + right -= 1 + } + } +} +``` + diff --git a/problems/背包理论基础01背包-1.md b/problems/背包理论基础01背包-1.md index 36daffcf..a40a92ab 100644 --- a/problems/背包理论基础01背包-1.md +++ b/problems/背包理论基础01背包-1.md @@ -380,58 +380,84 @@ func main() { ### javascript ```js -/** - * - * @param {Number []} weight - * @param {Number []} value - * @param {Number} size - * @returns - */ +function testWeightBagProblem (weight, value, size) { + // 定义 dp 数组 + const len = weight.length, + dp = Array(len).fill().map(() => Array(size + 1).fill(0)); -function testWeightBagProblem(weight, value, size) { -const len = weight.length, -dp = Array.from({length: len}).map( -() => Array(size + 1)) //JavaScript 数组是引用类型 -for(let i = 0; i < len; i++) { //初始化最左一列,即背包容量为0时的情况 -dp[i][0] = 0; -} -for(let j = 1; j < size+1; j++) { //初始化第0行, 只有一件物品的情况 -if(weight[0] <= j) { -dp[0][j] = value[0]; -} else { -dp[0][j] = 0; -} -} - -for(let i = 1; i < len; i++) { //dp[i][j]由其左上方元素推导得出 -for(let j = 1; j < size+1; j++) { -if(j < weight[i]) dp[i][j] = dp[i - 1][j]; -else dp[i][j] = Math.max(dp[i-1][j], dp[i-1][j - weight[i]] + value[i]); -} -} - -return dp[len-1][size] //满足条件的最大值 -} - -function testWeightBagProblem2 (wight, value, size) { - const len = wight.length, - dp = Array(size + 1).fill(0); - for(let i = 1; i <= len; i++) { - for(let j = size; j >= wight[i - 1]; j--) { - dp[j] = Math.max(dp[j], value[i - 1] + dp[j - wight[i - 1]]); + // 初始化 + for(let j = weight[0]; j <= size; j++) { + dp[0][j] = value[0]; } - } - return dp[size]; -} + // weight 数组的长度len 就是物品个数 + for(let i = 1; i < len; i++) { // 遍历物品 + for(let j = 0; j <= size; j++) { // 遍历背包容量 + if(j < weight[i]) dp[i][j] = dp[i - 1][j]; + else dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]); + } + } + + console.table(dp) + + return dp[len - 1][size]; +} function test () { - console.log(testWeightBagProblem([1, 3, 4, 5], [15, 20, 30, 55], 6)); + console.log(testWeightBagProblem([1, 3, 4, 5], [15, 20, 30, 55], 6)); } test(); ``` + +### C +```c +#include +#include +#include + +#define MAX(a, b) (((a) > (b)) ? (a) : (b)) +#define ARR_SIZE(a) (sizeof((a)) / sizeof((a)[0])) +#define BAG_WEIGHT 4 + +void backPack(int* weights, int weightSize, int* costs, int costSize, int bagWeight) { + // 开辟dp数组 + int dp[weightSize][bagWeight + 1]; + memset(dp, 0, sizeof(int) * weightSize * (bagWeight + 1)); + + int i, j; + // 当背包容量大于物品0的重量时,将物品0放入到背包中 + for(j = weights[0]; j <= bagWeight; ++j) { + dp[0][j] = costs[0]; + } + + // 先遍历物品,再遍历重量 + for(j = 1; j <= bagWeight; ++j) { + for(i = 1; i < weightSize; ++i) { + // 如果当前背包容量小于物品重量 + if(j < weights[i]) + // 背包物品的价值等于背包不放置当前物品时的价值 + dp[i][j] = dp[i-1][j]; + // 若背包当前重量可以放置物品 + else + // 背包的价值等于放置该物品或不放置该物品的最大值 + dp[i][j] = MAX(dp[i - 1][j], dp[i - 1][j - weights[i]] + costs[i]); + } + } + + printf("%d\n", dp[weightSize - 1][bagWeight]); +} + +int main(int argc, char* argv[]) { + int weights[] = {1, 3, 4}; + int costs[] = {15, 20, 30}; + backPack(weights, ARR_SIZE(weights), costs, ARR_SIZE(costs), BAG_WEIGHT); + return 0; +} +``` + + ### TypeScript ```typescript diff --git a/problems/背包理论基础01背包-2.md b/problems/背包理论基础01背包-2.md index dabdfb2d..b66b74a6 100644 --- a/problems/背包理论基础01背包-2.md +++ b/problems/背包理论基础01背包-2.md @@ -137,6 +137,8 @@ dp[1] = dp[1 - weight[0]] + value[0] = 15 因为一维dp的写法,背包容量一定是要倒序遍历(原因上面已经讲了),如果遍历背包容量放在上一层,那么每个dp[j]就只会放入一个物品,即:背包里只放入了一个物品。 +倒序遍历的原因是,本质上还是一个对二维数组的遍历,并且右下角的值依赖上一层左上角的值,因此需要保证左边的值仍然是上一层的,从右向左覆盖。 + (这里如果读不懂,就在回想一下dp[j]的定义,或者就把两个for循环顺序颠倒一下试试!) **所以一维dp数组的背包在遍历顺序上和二维其实是有很大差异的!**,这一点大家一定要注意。 @@ -315,6 +317,64 @@ function test () { test(); ``` +### C +```c +#include +#include + +#define MAX(a, b) (((a) > (b)) ? (a) : (b)) +#define ARR_SIZE(arr) ((sizeof((arr))) / sizeof((arr)[0])) +#define BAG_WEIGHT 4 + +void test_back_pack(int* weights, int weightSize, int* values, int valueSize, int bagWeight) { + int dp[bagWeight + 1]; + memset(dp, 0, sizeof(int) * (bagWeight + 1)); + + int i, j; + // 先遍历物品 + for(i = 0; i < weightSize; ++i) { + // 后遍历重量。从后向前遍历 + for(j = bagWeight; j >= weights[i]; --j) { + dp[j] = MAX(dp[j], dp[j - weights[i]] + values[i]); + } + } + + // 打印最优结果 + printf("%d\n", dp[bagWeight]); +} + +int main(int argc, char** argv) { + int weights[] = {1, 3, 4}; + int values[] = {15, 20, 30}; + test_back_pack(weights, ARR_SIZE(weights), values, ARR_SIZE(values), BAG_WEIGHT); + return 0; +} +``` + +### TypeScript + +```typescript +function testWeightBagProblem( + weight: number[], + value: number[], + size: number +): number { + const goodsNum: number = weight.length; + const dp: number[] = new Array(size + 1).fill(0); + for (let i = 0; i < goodsNum; i++) { + for (let j = size; j >= weight[i]; j--) { + dp[j] = Math.max(dp[j], dp[j - weight[i]] + value[i]); + } + } + return dp[size]; +} +const weight = [1, 3, 4]; +const value = [15, 20, 30]; +const size = 4; +console.log(testWeightBagProblem(weight, value, size)); + +``` + ----------------------- diff --git a/problems/背包问题理论基础多重背包.md b/problems/背包问题理论基础多重背包.md index a988db2c..712380f4 100644 --- a/problems/背包问题理论基础多重背包.md +++ b/problems/背包问题理论基础多重背包.md @@ -334,6 +334,64 @@ func Test_multiplePack(t *testing.T) { PASS ``` +TypeScript: + +> 版本一(改变数据源): + +```typescript +function testMultiPack() { + const bagSize: number = 10; + const weightArr: number[] = [1, 3, 4], + valueArr: number[] = [15, 20, 30], + amountArr: number[] = [2, 3, 2]; + for (let i = 0, length = amountArr.length; i < length; i++) { + while (amountArr[i] > 1) { + weightArr.push(weightArr[i]); + valueArr.push(valueArr[i]); + amountArr[i]--; + } + } + const goodsNum: number = weightArr.length; + const dp: number[] = new Array(bagSize + 1).fill(0); + // 遍历物品 + for (let i = 0; i < goodsNum; i++) { + // 遍历背包容量 + for (let j = bagSize; j >= weightArr[i]; j--) { + dp[j] = Math.max(dp[j], dp[j - weightArr[i]] + valueArr[i]); + } + } + console.log(dp); +} +testMultiPack(); +``` + +> 版本二(改变遍历方式): + +```typescript +function testMultiPack() { + const bagSize: number = 10; + const weightArr: number[] = [1, 3, 4], + valueArr: number[] = [15, 20, 30], + amountArr: number[] = [2, 3, 2]; + const goodsNum: number = weightArr.length; + const dp: number[] = new Array(bagSize + 1).fill(0); + // 遍历物品 + for (let i = 0; i < goodsNum; i++) { + // 遍历物品个数 + for (let j = 0; j < amountArr[i]; j++) { + // 遍历背包容量 + for (let k = bagSize; k >= weightArr[i]; k--) { + dp[k] = Math.max(dp[k], dp[k - weightArr[i]] + valueArr[i]); + } + } + } + console.log(dp); +} +testMultiPack(); +``` + + + -----------------------
diff --git a/problems/背包问题理论基础完全背包.md b/problems/背包问题理论基础完全背包.md index 3ec399f1..54e772e0 100644 --- a/problems/背包问题理论基础完全背包.md +++ b/problems/背包问题理论基础完全背包.md @@ -340,6 +340,27 @@ function test_completePack2() { } ``` +TypeScript: + +```typescript +// 先遍历物品,再遍历背包容量 +function test_CompletePack(): void { + const weight: number[] = [1, 3, 4]; + const value: number[] = [15, 20, 30]; + const bagSize: number = 4; + const dp: number[] = new Array(bagSize + 1).fill(0); + for (let i = 0; i < weight.length; i++) { + for (let j = weight[i]; j <= bagSize; j++) { + dp[j] = Math.max(dp[j], dp[j - weight[i]] + value[i]); + } + } + console.log(dp); +} +test_CompletePack(); +``` + + + -----------------------
diff --git a/problems/链表理论基础.md b/problems/链表理论基础.md index 2fe9f14c..1a29c32a 100644 --- a/problems/链表理论基础.md +++ b/problems/链表理论基础.md @@ -210,6 +210,13 @@ type ListNode struct { } ``` +Scala: +```scala +class ListNode(_x: Int = 0, _next: ListNode = null) { + var next: ListNode = _next + var x: Int = _x +} +``` ----------------------- diff --git a/problems/面试题02.07.链表相交.md b/problems/面试题02.07.链表相交.md index 2e7226de..0a38cc33 100644 --- a/problems/面试题02.07.链表相交.md +++ b/problems/面试题02.07.链表相交.md @@ -317,7 +317,55 @@ ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { } ``` - +Scala: +```scala +object Solution { + def getIntersectionNode(headA: ListNode, headB: ListNode): ListNode = { + var lenA = 0 // headA链表的长度 + var lenB = 0 // headB链表的长度 + var tmp = headA // 临时变量 + // 统计headA的长度 + while (tmp != null) { + lenA += 1; + tmp = tmp.next + } + // 统计headB的长度 + tmp = headB // 临时变量赋值给headB + while (tmp != null) { + lenB += 1 + tmp = tmp.next + } + // 因为传递过来的参数是不可变量,所以需要重新定义 + var listA = headA + var listB = headB + // 两个链表的长度差 + // 如果gap>0,lenA>lenB,headA(listA)链表往前移动gap步 + // 如果gap<0,lenA 0) { + // 因为不可以i-=1,所以可以使用for + for (i <- 0 until gap) { + listA = listA.next // 链表headA(listA) 移动 + } + } else { + gap = math.abs(gap) // 此刻gap为负值,取绝对值 + for (i <- 0 until gap) { + listB = listB.next + } + } + // 现在两个链表同时往前走,如果相等则返回 + while (listA != null && listB != null) { + if (listA == listB) { + return listA + } + listA = listA.next + listB = listB.next + } + // 如果链表没有相交则返回null,return可以省略 + null + } +} +``` -----------------------