当前位置:网站首页>[sword finger offer] interview question 42: the maximum sum of continuous subarrays -- with 0x80000000 and int_ MIN
[sword finger offer] interview question 42: the maximum sum of continuous subarrays -- with 0x80000000 and int_ MIN
2022-07-27 15:52:00 【Jocelin47】

Method 1 : Violence law ( Overtime )
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;
}
};
Method 2 :
1、 If the current nSum Less than 0 Words , You can discard all the previous accumulation , Only nSum Equal to the current nums[i]
2、 Record the result of the final maximum with a minimum number .
class Solution {
public:
int maxSubArray(vector<int>& nums) {
if( nums.size() == 0 )
return 0;
int nSum=0;
//int greatSum=0x80000000;
int greatSum=INT_MIN;// The result of finding the maximum gives Minimum number
for( int i=0; i<nums.size(); i++ )
{
// When sum Less than 0 when , Just start over with the next number
if( nSum < 0 )
nSum = nums[i];
else
nSum += nums[i];
if(nSum > greatSum)
greatSum=nSum;
}
return greatSum;
}
};
Method 3 : Dynamic programming
dp[i] Corresponding 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;
}
};
attach : Method 2 uses C/C++ Constant in :
INT_MIN
INT_MIN In the header file limits.h in
because int Occupy 4 byte 32 position , According to the rules of binary coding ,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 Binary bit of
Original code 1000 0000 0000 0000 0000 0000 0000 0000
If the highest bit is the sign bit , Then for -0, But output int i = 0x80000000
i = -(2^31)
边栏推荐
- NPM install error unable to access
- C language: minesweeping games
- After the table is inserted into an in row formula, the cell loses focus
- js操作dom节点
- C language: Sanzi game
- multimap案例
- On juicefs
- Stock account opening commission discount, stock trading account opening which securities company is good, is online account opening safe
- Network principle (2) -- network development
- Implement custom spark optimization rules
猜你喜欢
随机推荐
实体类(VO,DO,DTO)的划分
js使用一元运算符简化字符串转数字
Extended log4j supports the automatic deletion of log files according to time division and expired files
[正则表达式] 匹配分组
数组名是首元素地址吗?
Inter thread wait and wake-up mechanism, singleton mode, blocking queue, timer
Spark 3.0 Adaptive Execution 代码实现及数据倾斜优化
It is said that the US government will issue sales licenses to Huawei to some US enterprises!
Hj8 consolidated statement record
Use deconstruction to exchange the values of two variables
go语言慢速入门——基本内置类型
CAS比较交换的知识、ABA问题、锁升级的流程
Go language slow start - package
【剑指offer】面试题54:二叉搜索树的第k大节点
使用解构交换两个变量的值
Binder initialization process
【剑指offer】面试题41:数据流中的中位数——大、小堆实现
网络原理(1)——基础原理概述
Spark troubleshooting finishing
js操作dom节点









