From fc56c8a4f715a87722b1b45519be1c4b895fe508 Mon Sep 17 00:00:00 2001 From: "guowei.liu" Date: Sun, 23 Mar 2025 23:51:52 +0800 Subject: [PATCH] Update Solution.java add easy solution --- .../0000-0099/0053.Maximum Subarray/Solution.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/solution/0000-0099/0053.Maximum Subarray/Solution.java b/solution/0000-0099/0053.Maximum Subarray/Solution.java index d8641665f84d6..54a56607acb5e 100644 --- a/solution/0000-0099/0053.Maximum Subarray/Solution.java +++ b/solution/0000-0099/0053.Maximum Subarray/Solution.java @@ -1,10 +1,10 @@ class Solution { public int maxSubArray(int[] nums) { - int ans = nums[0]; - for (int i = 1, f = nums[0]; i < nums.length; ++i) { - f = Math.max(f, 0) + nums[i]; - ans = Math.max(ans, f); + int pre = 0, maxAns = nums[0]; + for (int x : nums) { + pre = Math.max(pre + x, x); + maxAns = Math.max(maxAns, pre); } - return ans; + return maxAns; } -} \ No newline at end of file +}