From 290e15e69e41d06f07c44ea70f01f009034c81cf Mon Sep 17 00:00:00 2001 From: yunskj Date: Tue, 9 Jul 2024 21:25:11 +0800 Subject: [PATCH 1/2] =?UTF-8?q?Update=200977.=E6=9C=89=E5=BA=8F=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E7=9A=84=E5=B9=B3=E6=96=B9.md=20=E5=A2=9E=E5=8A=A0Jav?= =?UTF-8?q?a=E6=8E=92=E5=BA=8F=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 977.有序数组的平方Java排序法 --- problems/0977.有序数组的平方.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/problems/0977.有序数组的平方.md b/problems/0977.有序数组的平方.md index b10620d0..7119dda5 100644 --- a/problems/0977.有序数组的平方.md +++ b/problems/0977.有序数组的平方.md @@ -100,6 +100,18 @@ public: ## 其他语言版本 ### Java: +排序法 +```Java +class Solution { + public int[] sortedSquares(int[] nums) { + for (int i = 0; i < nums.length; i++) { + nums[i] = nums[i] * nums[i]; + } + Arrays.sort(nums); + return nums; + } +} +``` ```Java class Solution { From 6fd1df5bd7c6e665d6caf56368bf0289c2c27cdc Mon Sep 17 00:00:00 2001 From: markwang Date: Wed, 10 Jul 2024 10:19:39 +0800 Subject: [PATCH 2/2] =?UTF-8?q?404.=E5=B7=A6=E5=8F=B6=E5=AD=90=E4=B9=8B?= =?UTF-8?q?=E5=92=8C=E5=A2=9E=E5=8A=A0Go=E9=80=92=E5=BD=92=E7=B2=BE?= =?UTF-8?q?=E7=AE=80=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0404.左叶子之和.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/problems/0404.左叶子之和.md b/problems/0404.左叶子之和.md index 1ba71dc9..66aff68f 100644 --- a/problems/0404.左叶子之和.md +++ b/problems/0404.左叶子之和.md @@ -337,6 +337,21 @@ func sumOfLeftLeaves(root *TreeNode) int { } ``` +**递归精简版** + +```go +func sumOfLeftLeaves(root *TreeNode) int { + if root == nil { + return 0 + } + leftValue := 0 + if root.Left != nil && root.Left.Left == nil && root.Left.Right == nil { + leftValue = root.Left.Val + } + return leftValue + sumOfLeftLeaves(root.Left) + sumOfLeftLeaves(root.Right) +} +``` + **迭代法(前序遍历)** ```go