当前位置:网站首页>【剑指offer】面试题42:连续子数组的最大和——附0x80000000与INT_MIN
【剑指offer】面试题42:连续子数组的最大和——附0x80000000与INT_MIN
2022-07-27 14:24:00 【Jocelin47】

方法一:暴力法(超时)
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int n= nums.size();
int res=-1e8;
for(int i=0;i<n;i++){
int sum=0;
for(int j=i;j<n;j++) {
sum+=nums[j];
res=max(res,sum);
}
}
return res;
}
};
方法二:
1、如果当前的nSum小于0的话,则可以把前面累加的都舍弃,只把nSum等于当前的nums[i]
2、把一个最小的数给记录最终最大值的结果。
class Solution {
public:
int maxSubArray(vector<int>& nums) {
if( nums.size() == 0 )
return 0;
int nSum=0;
//int greatSum=0x80000000;
int greatSum=INT_MIN;//求最大值的结果给 最小的数
for( int i=0; i<nums.size(); i++ )
{
//当sum小于0时,就从下一个数重新开始
if( nSum < 0 )
nSum = nums[i];
else
nSum += nums[i];
if(nSum > greatSum)
greatSum=nSum;
}
return greatSum;
}
};
方法三:动态规划
dp[i] 对应f(i)
class Solution {
public:
int maxSubArray(vector<int>& nums) {
vector<int> dp(nums.size());
dp[0]=nums[0];
int res=dp[0];
for(int i = 1; i < nums.size() ; i++ ) {
if( dp[i-1] <= 0 )
dp[i]=nums[i];
else
dp[i]=dp[i-1]+nums[i];
res= max( dp[i], res);
}
return res;
}
};
附:方法二中用到C/C++中的常量:
INT_MIN
INT_MIN在头文件limits.h中
因为int占4字节32位,根据二进制编码的规则,INT_MAX = 2^31-1,INT_MIN= -2^31
INT_MAX + 1 = INT_MIN
INT_MIN - 1 = INT_MAX
abs(INT_MIN) = INT_MIN
INT_MAX + 1 < INT_MAX, INT_MIN - 1 > INT_MIN, abs(INT_MIN) < 0
0x80000000
0x80000000 的二进制位
原码 1000 0000 0000 0000 0000 0000 0000 0000
若最高位为符号位,则为-0,可是输出int i = 0x80000000
i = -(2^31)
边栏推荐
- Multi table query_ Sub query overview and multi table query_ Sub query situation 1 & situation 2 & situation 3
- 谷粒商城配置CorsWebFilter后,报错:Resource sharing error:MultipleAllowOriginValues
- Deveco studio2.1 operation item error
- 华为鸿蒙模拟器去除顶部导航栏方法
- QT (IV) mixed development using code and UI files
- Introduction of the connecting circuit between ad7606 and stm32
- 仪表放大器和运算放大器优缺点对比
- Network equipment hard core technology insider router Chapter 6 tompkinson roaming the online world (middle)
- 4种单片机驱动继电器方案
- 【剑指offer】面试题53-Ⅱ:0~n-1中缺失的数字——二分查找
猜你喜欢

Watermelon book machine learning reading notes Chapter 1 Introduction

华为鸿蒙模拟器去除顶部导航栏方法

学习Parquet文件格式

Alibaba's latest summary 2022 big factory interview real questions + comprehensive coverage of core knowledge points + detailed answers
![[daily question 1] 558. Intersection of quadtrees](/img/96/16ec3031161a2efdb4ac69b882a681.png)
[daily question 1] 558. Intersection of quadtrees

Pictures to be delivered

EMC design scheme of RS485 interface
Principle of MOS tube to prevent reverse connection of power supply

QT (IV) mixed development using code and UI files

光电隔离电路设计方案(六款基于光耦、AD210AN的光电隔离电路图)
随机推荐
Network equipment hard core technology insider router 19 dpdk (IV)
【剑指offer】面试题55 - Ⅰ/Ⅱ:二叉树的深度/平衡二叉树
Deveco studio2.1 operation item error
Several basic uses of tl431-2.5v voltage reference chip
/dev/loop1占用100%问题
【剑指offer】面试题46:把数字翻译成字符串——动态规划
Unity's simplest object pool implementation
Leetcode interview question 17.21. water volume double pointer of histogram, monotonic stack /hard
光电隔离电路设计方案(六款基于光耦、AD210AN的光电隔离电路图)
Leetcode 783. binary search tree node minimum distance tree /easy
lua学习笔记
Leetcode 456.132 mode monotone stack /medium
Inside router of network equipment hard core technology (10) disassembly of Cisco asr9900 (4)
Selenium 报错:session not created: This version of ChromeDriver only supports Chrome version 81
Method of removing top navigation bar in Huawei Hongmeng simulator
谷粒商城配置CorsWebFilter后,报错:Resource sharing error:MultipleAllowOriginValues
Network equipment hard core technology insider router chapter Cisco asr9900 disassembly (I)
Network device hard core technology insider router Chapter 15 from deer by device to router (Part 2)
【剑指offer】面试题51:数组中的逆序对——归并排序
js使用for in和for of来简化普通for循环