C#LeetCode刷题之#53-最大子序和(Maximum Subarray)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4012 访问。
给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。
输入: [-2,1,-3,4,-1,2,1,-5,4],
输出: 6
解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。
进阶:
如果你已经实现复杂度为 O(n) 的解法,尝试使用更为精妙的分治法求解。
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
Input: [-2,1,-3,4,-1,2,1,-5,4],
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.
Follow up:
If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4012 访问。
public class Program {
public static void Main(string[] args) {
int[] nums = { -2, 1, -3, 4, -1, 2, 1, -5, 4 };
var res = maxSubArray(nums);
Console.WriteLine(res);
nums = new int[] { 4, 1, 0, -3, -1, 9, 1 };
res = maxSubArray2(nums);
Console.WriteLine(res);
Console.ReadKey();
}
private static int maxSubArray(int[] nums) {
//常规写法
//记录最大和、本次可以“接受”的和(本次和)
var max = int.MinValue;
var thisSum = 0;
for(var i = 0; i < nums.Length; i++) {
//如果和小于等于0,那么当前值没有用,应当舍去
//(为了优化写法,其实是上一次循环时的和)
//即重置本次和为当前值
if(thisSum <= 0) {
thisSum = nums[i];
} else {
//否则,迭加和
thisSum += nums[i];
}
//用max记录更大的值
if(max < thisSum) {
max = thisSum;
}
}
//返回最大值
return max;
}
private static int maxSubArray2(int[] nums) {
//高度精简优化写法
var max = int.MinValue;
var thisSum = 0;
for(var i = 0; i < nums.Length; i++) {
max = Math.Max(max, thisSum = Math.Max(nums[i], thisSum + nums[i]));
}
return max;
}
}
以上给出2种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4012 访问。
6
11
分析:
显而易见,以上2种算法的时间复杂度均为: 。
C#LeetCode刷题之#53-最大子序和(Maximum Subarray)的更多相关文章
- LeetCode 53. 最大子序和(Maximum Subarray)
53. 最大子序和 53. Maximum Subarray 题目描述 给定一个整数数组 nums,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. LeetCode53. M ...
- 【leetcode算法-简单】53. 最大子序和
[题目描述] 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1,2,1,-5,4],输出: 6解释: ...
- C#LeetCode刷题之#559-N叉树的最大深度(Maximum Depth of N-ary Tree)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4088 访问. 给定一个 N 叉树,找到其最大深度. 最大深度是指 ...
- C#LeetCode刷题之#104-二叉树的最大深度(Maximum Depth of Binary Tree)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4072 访问. 给定一个二叉树,找出其最大深度. 二叉树的深度为根 ...
- leetcode刷题-94二叉树的中序遍历
题目 给定一个二叉树,返回它的中序 遍历. 实现 # def __init__(self, x): # self.val = x # self.left = None # self.right = N ...
- [Swift]LeetCode53. 最大子序和 | Maximum Subarray
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- C#LeetCode刷题-树
树篇 # 题名 刷题 通过率 难度 94 二叉树的中序遍历 61.6% 中等 95 不同的二叉搜索树 II 43.4% 中等 96 不同的二叉搜索树 51.6% 中等 98 验证二叉搜索树 ...
- LeetCode刷题总结-数组篇(上)
数组是算法中最常用的一种数据结构,也是面试中最常考的考点.在LeetCode题库中,标记为数组类型的习题到目前为止,已累计到了202题.然而,这202道习题并不是每道题只标记为数组一个考点,大部分习题 ...
- C#LeetCode刷题-动态规划
动态规划篇 # 题名 刷题 通过率 难度 5 最长回文子串 22.4% 中等 10 正则表达式匹配 18.8% 困难 32 最长有效括号 23.3% 困难 44 通配符匹配 17.7% ...
- C#LeetCode刷题-分治算法
分治算法篇 # 题名 刷题 通过率 难度 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组的中位数(Median of Two Sorted Arrays)-该题未达最优解 30 ...
随机推荐
- ajax根据坐标查询WMS地图服务属性信息
<html lang="en"> <head> <meta charset="UTF-8"> <meta name=& ...
- Python Ethical Hacking - The Lab and Needed Software
The Lab and Needed Software Attacker Machine - Kali Linux https://www.kali.org/ 1. Install the softw ...
- Ethical Hacking - NETWORK PENETRATION TESTING(8)
WEP Cracking Basic case Run airdump-ng to log all traffic from the target network. airodump-ng --cha ...
- for语句例题:编写程序FooBizBaz.java,从1循环到150并在每行打印一个值
/** * 编写程序FooBizBaz.java,从1循环到150并在每行打印一个值, * 另外在每个3的倍数行上打印出"foo",在每个5的倍数行上打印"biz&quo ...
- Python基础点记录2
---- PygLatin 1 介绍函数的调用,就是直接函数名 def square(n): squared = n**2 print "%d squared is %d." % ...
- 常用CSS颜色表
1.16进制的CSS颜色代码 > http://www.jsjtt.com/webkaifa/HTML/65.html
- A - New Building for SIS
You are looking at the floor plan of the Summer Informatics School's new building. You were tasked w ...
- 线上CUP负载过高排查方法
1.top命令查看线程占据的CPU 注意:上面行的cpu是多个内核的平均CPU,不可能超过100% 下面的cpu是每个进程实际占用的cpu,可能超过100% 备注:查看多个内核cpu,只需要在输入 ...
- Oracle连接报错之IO异常(The Network Adapter could not establish the connection)
简单介绍:自己封装oracle jdbc的一些常用功能jar包,自己本机玩没啥问题,给别人玩儿,发现总是抛异常 IO异常(The Network Adapter could not establish ...
- Linux系统查看硬件信息神器,比设备管理器好用100倍!
大家都知道,当我们的 Linux 系统计算机出现问题时,需要对其排除故障,首先需要做的是找出计算机的硬件信息.下面介绍一个简单易用的应用程序--HardInfo,你可以利用它来显示你电脑的每个硬件方面 ...