【leetcode】53. (Easy) Maximum Subarray
解題思路:
滑動視窗
提交程式碼:
class Solution {
public int maxSubArray(int[] nums) {
if(nums.length==0) return 0;
int[] values=new int[nums.length];values[0]=nums[0];
int maxValue=values[0];
for(int i=1;i<nums.length;i++) {
if(values[i-1]<0) values[i]=nums[i] ;
else values[i]=nums[i]+values[i-1];
if(values[i]>maxValue) maxValue=values[i];
}
return maxValue;
}
}
執行結果:
相關推薦
【leetcode】53. (Easy) Maximum Subarray
解題思路: 滑動視窗 提交程式碼: class Solution { public int maxSubArray(int[] nums) { if(nums.length==0) return 0; int[] values=new int[num
【Leetcode】53. Maximum Subarray(Easy)
1.題目Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [
【leetcode】53. 最大子序和(Maximum Subarray)
變量 最終 code res com 定義 最大的 array ray 解題思路: 定義兩個變量res和curSum,其中res保存最終要返回的結果,即最大的子數組之和,curSum初始值為0,每遍歷一個數字num,比較curSum + num和num中的較大值存入cur
【leetcode】53.Maximum Subarray
題目 找到陣列中具有最大和的連續序列,並輸出這個序列。 思路 定義一個maxsum儲存最大序列和,定義一個thissum儲存當前序列和。 thissum小於0時,令其等於0. maxsum=max(maxsum,thissum) 程式碼如下 class Solu
【LeetCode】53. 最大子序和
題目連結:https://leetcode-cn.com/problems/maximum-subarray/description/ 題目描述 給定一個整數陣列 nums ,找到一個具有最大和的連續子陣列(子陣列最少包含一個元素),返回其最大和。 示例 輸入: [-2,
【leetcode】28.(Easy)Implement strStr()
題目連結 解題思路: 這道題的意思就是找到字串中一個子串的起始位置,沒有這個子串的話返回-1。 我的做法就是簡單的蠻力法,首先找到第一個匹配的字元,然後再進一步進行判決 提交程式碼: class Solution { public int strStr(Strin
【leetcode】70. (Easy) Climbing Stairs
提交程式碼: class Solution { public int climbStairs(int n) { if(n==1) return 1; if(n==2) return 2; int tmp=3,tm
【LeetCode】209. Minimum Size Subarray Sum 解題報告(Python)
題目描述: Given an array of n positive integers and a positive integer s, find the minimal length of a
【Leetcode】Sliding Window Maximum
題目: Given an array nums, there is a sliding window of size k which is moving from the very left of
【LeetCode】53. 最大子序和 (C++)
題目描述: 給定一個整數陣列 nums ,找到一個具有最大和的連續子陣列(子陣列最少包含一個元素),返回其最大和。 示例: 輸入: [-2,1,-3,4,-1,2,1,-5,4], 輸出: 6 解釋: 連續子陣列 [4,-1,2,1] 的和最大,為 6。 進階:
【leetcode】【53】Maximum Subarray
一、問題描述 Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the
【LeetCode】053. Maximum Subarray
子序列 fin n) cto largest nbsp code ray ive 題目: Find the contiguous subarray within an array (containing at least one number) which has the
【LeetCode】120.Maximum Subarray
題目描述(Easy) Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return
【LeetCode】152. Maximum Product Subarray 解題報告(Python)
題目描述: Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the l
【leetcode】104.(Easy)Maximum Depth of Binary Tree
解題思路: bfs或dfs 提交程式碼:bfs class Solution { public int maxDepth(TreeNode root) { if(root==null) return 0; int
【leetcode】69. (Easy)Sqrt(x)
解題思路: 二分法,同時使用double來記錄數字 此外後面討論區有個答案還挺好的,使用mid>x/mid?來進行判斷,避免了數字型別轉換。 提交程式碼: class Solution { public int mySqrt(int x) { if(x=
【leetcode】67.(Easy)Add Binary
提交程式碼: class Solution { public String addBinary(String a, String b) { int numA,numB,carry=0; int p1=a.length()-1,p2=b.length()-1;
【leetcode】66. (Easy)Plus One
提交程式碼: class Solution { public int[] plusOne(int[] digits) { int i,num; List<Integer> tmp=new ArrayList<Integer>();
【leetcode】58. (Easy)Length of Last Word
提交程式碼: class Solution { public int lengthOfLastWord(String s) { int len=0,p=s.length()-1; while(p>=0&&s.charAt(p)
【leetcode】662. Maximum Width of Binary Tree
題目如下: Given a binary tree, write a function to get the maximum width of the given tree. The width of a tree is the maximum width among all levels. The