t*3
2 楼
Leetcode的Best Time to Buy and Sell Stock IV 那道题,我看到网上的答案就改了
下来:
请注意看定义数组local和global的那两行。如果按照int a[n]的方式定义,leetcode
能编译但是输出错误。请问这是为什么?
class Solution {
public:
int maxProfit(int k, vector& prices) {
int days = prices.size();
if(days == 0) return 0;
if(k >= days/2) return maxProfitUnlimitedTimes(prices);
//int local[days][k+1] = {0}; //-> Compile OK but run error, if I
define the array in this way. Why?
//int global[days][k+1] = {0};
vector > local(days, vector(k+1)); // -> Run OK.
vector > global(days, vector(k+1));
int diff;
for(int i=1; i diff = prices[i] - prices[i-1];
for(int j=1; j<=k; j++) {
local[i][j] = max(global[i-1][j-1] + diff, local[i-1][j] +
diff);
global[i][j] = max(local[i][j], global[i-1][j]);
}
}
return global[days-1][k];
}
int maxProfitUnlimitedTimes(vector &prices)
{
int profit = 0;
for(int i=1; i {
if(prices[i] > prices[i-1])
{
profit += prices[i] - prices[i-1];
}
}
return profit;
}
};
=============================
输出错误的case是这个:
Input: 2, [3,2,6,5,0,3]
Output: 20292312
Expected: 7
下来:
请注意看定义数组local和global的那两行。如果按照int a[n]的方式定义,leetcode
能编译但是输出错误。请问这是为什么?
class Solution {
public:
int maxProfit(int k, vector
int days = prices.size();
if(days == 0) return 0;
if(k >= days/2) return maxProfitUnlimitedTimes(prices);
//int local[days][k+1] = {0}; //-> Compile OK but run error, if I
define the array in this way. Why?
//int global[days][k+1] = {0};
vector
vector
int diff;
for(int i=1; i
for(int j=1; j<=k; j++) {
local[i][j] = max(global[i-1][j-1] + diff, local[i-1][j] +
diff);
global[i][j] = max(local[i][j], global[i-1][j]);
}
}
return global[days-1][k];
}
int maxProfitUnlimitedTimes(vector
{
int profit = 0;
for(int i=1; i
if(prices[i] > prices[i-1])
{
profit += prices[i] - prices[i-1];
}
}
return profit;
}
};
=============================
输出错误的case是这个:
Input: 2, [3,2,6,5,0,3]
Output: 20292312
Expected: 7
o*e
3 楼
啊
r*a
4 楼
原厂的asus ac68u刷成merlin还需要downgrade和修改cfe吗?谢谢。
z*3
5 楼
14*12*8
n*n
6 楼
c/c++里数组长度只能是常数,不能是变量
j*9
7 楼
How are your stocks doing right now?
s*r
9 楼
太贵,还有SHIPPING
还不如去HOMEDEPOT买 16×12×12的,现在特价才57分还是67分的~
还不如去HOMEDEPOT买 16×12×12的,现在特价才57分还是67分的~
w*t
11 楼
上拨菜楼...
美股现在成了拉磨的驴了...
美股现在成了拉磨的驴了...
s*n
14 楼
还是用vector resize下安稳吧
m*n
16 楼
int a[][] is allocated entirely on stack, while vector's data array is in
heap. So the problem probably lies in the call frame size. I've seen a C++
compiler that overlays two call frames because the actual call frame size
exceeds the internal max. Does the code fail all cases or just those where
k * days seem large?
leetcode
【在 t*********3 的大作中提到】
: Leetcode的Best Time to Buy and Sell Stock IV 那道题,我看到网上的答案就改了
: 下来:
: 请注意看定义数组local和global的那两行。如果按照int a[n]的方式定义,leetcode
: 能编译但是输出错误。请问这是为什么?
: class Solution {
: public:
: int maxProfit(int k, vector& prices) {
: int days = prices.size();
: if(days == 0) return 0;
: if(k >= days/2) return maxProfitUnlimitedTimes(prices);
heap. So the problem probably lies in the call frame size. I've seen a C++
compiler that overlays two call frames because the actual call frame size
exceeds the internal max. Does the code fail all cases or just those where
k * days seem large?
leetcode
【在 t*********3 的大作中提到】
: Leetcode的Best Time to Buy and Sell Stock IV 那道题,我看到网上的答案就改了
: 下来:
: 请注意看定义数组local和global的那两行。如果按照int a[n]的方式定义,leetcode
: 能编译但是输出错误。请问这是为什么?
: class Solution {
: public:
: int maxProfit(int k, vector
: int days = prices.size();
: if(days == 0) return 0;
: if(k >= days/2) return maxProfitUnlimitedTimes(prices);
t*3
18 楼
输出错误的case是这个:
Input: 2, [3,2,6,5,0,3]
Output: 20292312
Expected: 7
+
where
【在 m*****n 的大作中提到】
: int a[][] is allocated entirely on stack, while vector's data array is in
: heap. So the problem probably lies in the call frame size. I've seen a C++
: compiler that overlays two call frames because the actual call frame size
: exceeds the internal max. Does the code fail all cases or just those where
: k * days seem large?
:
: leetcode
Input: 2, [3,2,6,5,0,3]
Output: 20292312
Expected: 7
+
where
【在 m*****n 的大作中提到】
: int a[][] is allocated entirely on stack, while vector's data array is in
: heap. So the problem probably lies in the call frame size. I've seen a C++
: compiler that overlays two call frames because the actual call frame size
: exceeds the internal max. Does the code fail all cases or just those where
: k * days seem large?
:
: leetcode
c*n
19 楼
13X13*8
N*B
21 楼
头2位请收包子,谢谢
j*g
22 楼
我觉得是初始化问题,你加个memset(local,0,sizeof(local));试试
n*e
25 楼
用vector或者动态分配,用new和delete
c*g
26 楼
天啦。。。现在的CS是不是只学算法,连static allocate, dynamic allocate 这些基
本概念都不学了?
leetcode
【在 t*********3 的大作中提到】
: Leetcode的Best Time to Buy and Sell Stock IV 那道题,我看到网上的答案就改了
: 下来:
: 请注意看定义数组local和global的那两行。如果按照int a[n]的方式定义,leetcode
: 能编译但是输出错误。请问这是为什么?
: class Solution {
: public:
: int maxProfit(int k, vector& prices) {
: int days = prices.size();
: if(days == 0) return 0;
: if(k >= days/2) return maxProfitUnlimitedTimes(prices);
本概念都不学了?
leetcode
【在 t*********3 的大作中提到】
: Leetcode的Best Time to Buy and Sell Stock IV 那道题,我看到网上的答案就改了
: 下来:
: 请注意看定义数组local和global的那两行。如果按照int a[n]的方式定义,leetcode
: 能编译但是输出错误。请问这是为什么?
: class Solution {
: public:
: int maxProfit(int k, vector
: int days = prices.size();
: if(days == 0) return 0;
: if(k >= days/2) return maxProfitUnlimitedTimes(prices);
c*g
34 楼
Google automatic, dynamic, staitic variable 的区别,它们啥时候allocate的,
allocate 在哪种memory,你
就知道为啥以前的C/C++ compiler 不能用a〔n〕. 再看看这个就知道C99是怎么处理它的
http://www.drdobbs.com/the-new-cwhy-variable-length-arrays/1844
你要真去面C/C++, 犯这种错误,刷再多的题都没用了。
【在 t*********3 的大作中提到】
: 我只知道一个是固定的,一个可以是变长的。C不能写int a[n],但是新一些的C++编译
: 器可以这么写。
allocate 在哪种memory,你
就知道为啥以前的C/C++ compiler 不能用a〔n〕. 再看看这个就知道C99是怎么处理它的
http://www.drdobbs.com/the-new-cwhy-variable-length-arrays/1844
你要真去面C/C++, 犯这种错误,刷再多的题都没用了。
【在 t*********3 的大作中提到】
: 我只知道一个是固定的,一个可以是变长的。C不能写int a[n],但是新一些的C++编译
: 器可以这么写。
A*e
35 楼
现在都是Java/Python,没人关心内存分配了。
就算是C++,也该用堆变量、STL容器、智能指针,用到new/delete的时候很少,除非写
系统软件。
它的
【在 c****g 的大作中提到】
: Google automatic, dynamic, staitic variable 的区别,它们啥时候allocate的,
: allocate 在哪种memory,你
: 就知道为啥以前的C/C++ compiler 不能用a〔n〕. 再看看这个就知道C99是怎么处理它的
: http://www.drdobbs.com/the-new-cwhy-variable-length-arrays/1844
: 你要真去面C/C++, 犯这种错误,刷再多的题都没用了。
就算是C++,也该用堆变量、STL容器、智能指针,用到new/delete的时候很少,除非写
系统软件。
它的
【在 c****g 的大作中提到】
: Google automatic, dynamic, staitic variable 的区别,它们啥时候allocate的,
: allocate 在哪种memory,你
: 就知道为啥以前的C/C++ compiler 不能用a〔n〕. 再看看这个就知道C99是怎么处理它的
: http://www.drdobbs.com/the-new-cwhy-variable-length-arrays/1844
: 你要真去面C/C++, 犯这种错误,刷再多的题都没用了。
t*3
36 楼
你发的网页里的东西我能读懂,我自己也写过*p那种动态数组resize,就是满了就乘以
2。
但是我还是不理解为什么你的回答和我的问题相关:
int local[days][k+1] = {0}; //-> Compile OK but run error, if I
define the array in this way. Why?
它的
【在 c****g 的大作中提到】
: Google automatic, dynamic, staitic variable 的区别,它们啥时候allocate的,
: allocate 在哪种memory,你
: 就知道为啥以前的C/C++ compiler 不能用a〔n〕. 再看看这个就知道C99是怎么处理它的
: http://www.drdobbs.com/the-new-cwhy-variable-length-arrays/1844
: 你要真去面C/C++, 犯这种错误,刷再多的题都没用了。
2。
但是我还是不理解为什么你的回答和我的问题相关:
int local[days][k+1] = {0}; //-> Compile OK but run error, if I
define the array in this way. Why?
它的
【在 c****g 的大作中提到】
: Google automatic, dynamic, staitic variable 的区别,它们啥时候allocate的,
: allocate 在哪种memory,你
: 就知道为啥以前的C/C++ compiler 不能用a〔n〕. 再看看这个就知道C99是怎么处理它的
: http://www.drdobbs.com/the-new-cwhy-variable-length-arrays/1844
: 你要真去面C/C++, 犯这种错误,刷再多的题都没用了。
t*3
37 楼
你说的“这种错误”,具体是指哪个错误?
它的
【在 c****g 的大作中提到】
: Google automatic, dynamic, staitic variable 的区别,它们啥时候allocate的,
: allocate 在哪种memory,你
: 就知道为啥以前的C/C++ compiler 不能用a〔n〕. 再看看这个就知道C99是怎么处理它的
: http://www.drdobbs.com/the-new-cwhy-variable-length-arrays/1844
: 你要真去面C/C++, 犯这种错误,刷再多的题都没用了。
它的
【在 c****g 的大作中提到】
: Google automatic, dynamic, staitic variable 的区别,它们啥时候allocate的,
: allocate 在哪种memory,你
: 就知道为啥以前的C/C++ compiler 不能用a〔n〕. 再看看这个就知道C99是怎么处理它的
: http://www.drdobbs.com/the-new-cwhy-variable-length-arrays/1844
: 你要真去面C/C++, 犯这种错误,刷再多的题都没用了。
c*g
38 楼
你读懂了个P,run time allocate 的memory,你在compile time 初始化,还说读懂
了。compiler 又不知道你的size,怎么知道该给你整几个0 ?
不管你是不是暴力转行的,真要面C/C++,还是先系统学点基础知识,光闷头刷题也不
行。
【在 t*********3 的大作中提到】
: 你发的网页里的东西我能读懂,我自己也写过*p那种动态数组resize,就是满了就乘以
: 2。
: 但是我还是不理解为什么你的回答和我的问题相关:
: int local[days][k+1] = {0}; //-> Compile OK but run error, if I
: define the array in this way. Why?
:
: 它的
了。compiler 又不知道你的size,怎么知道该给你整几个0 ?
不管你是不是暴力转行的,真要面C/C++,还是先系统学点基础知识,光闷头刷题也不
行。
【在 t*********3 的大作中提到】
: 你发的网页里的东西我能读懂,我自己也写过*p那种动态数组resize,就是满了就乘以
: 2。
: 但是我还是不理解为什么你的回答和我的问题相关:
: int local[days][k+1] = {0}; //-> Compile OK but run error, if I
: define the array in this way. Why?
:
: 它的
s*e
39 楼
楼上的态度还是好点吧。。。
h*e
40 楼
现在是不是真没人学c/c++了啊
【在 t*********3 的大作中提到】
: Leetcode的Best Time to Buy and Sell Stock IV 那道题,我看到网上的答案就改了
: 下来:
: 请注意看定义数组local和global的那两行。如果按照int a[n]的方式定义,leetcode
: 能编译但是输出错误。请问这是为什么?
: class Solution {
: public:
: int maxProfit(int k, vector& prices) {
: int days = prices.size();
: if(days == 0) return 0;
: if(k >= days/2) return maxProfitUnlimitedTimes(prices);
【在 t*********3 的大作中提到】
: Leetcode的Best Time to Buy and Sell Stock IV 那道题,我看到网上的答案就改了
: 下来:
: 请注意看定义数组local和global的那两行。如果按照int a[n]的方式定义,leetcode
: 能编译但是输出错误。请问这是为什么?
: class Solution {
: public:
: int maxProfit(int k, vector
: int days = prices.size();
: if(days == 0) return 0;
: if(k >= days/2) return maxProfitUnlimitedTimes(prices);
t*3
50 楼
多谢评论,我确实在“学点基础知识”,没有“光闷头刷题”,所以我才来问问题。如
果我只想刷题,我就没必要来问了。你的态度虽傲慢但并不重要,因为我只是想问问题
而已。
我看有人说variable length array cannot be initialized by any form of
initialization syntax. 因为C11里面说The type of the entity to be initialized
shall be an array of unknown size or a complete object type that is not a
variable length array type.
总之他的意思就是说int a[n] = {0} 其中的 “={0}” 这种写法不能用到变长数组。
但是他这只是告诉我结论,我还是不明白问什么不能这么写。
你说变长数组不能“在compile time 初始化”。你的意思是“={0}” 其实就是在
compile time初始化吗?为什么run time不可以这么做呢?run time时已经知道了数组
的size,然后就把这里面都写成0就可以了啊。为什么run time就不行呢?是不是因为
“={0}”的意思就是“在compile time初始化”?如果是的话,那为什么不可以对于变
长数组就故意在run time初始化呢?不好意思说得这么啰嗦不知道我有没有说明白。
【在 c****g 的大作中提到】
: 你读懂了个P,run time allocate 的memory,你在compile time 初始化,还说读懂
: 了。compiler 又不知道你的size,怎么知道该给你整几个0 ?
: 不管你是不是暴力转行的,真要面C/C++,还是先系统学点基础知识,光闷头刷题也不
: 行。
果我只想刷题,我就没必要来问了。你的态度虽傲慢但并不重要,因为我只是想问问题
而已。
我看有人说variable length array cannot be initialized by any form of
initialization syntax. 因为C11里面说The type of the entity to be initialized
shall be an array of unknown size or a complete object type that is not a
variable length array type.
总之他的意思就是说int a[n] = {0} 其中的 “={0}” 这种写法不能用到变长数组。
但是他这只是告诉我结论,我还是不明白问什么不能这么写。
你说变长数组不能“在compile time 初始化”。你的意思是“={0}” 其实就是在
compile time初始化吗?为什么run time不可以这么做呢?run time时已经知道了数组
的size,然后就把这里面都写成0就可以了啊。为什么run time就不行呢?是不是因为
“={0}”的意思就是“在compile time初始化”?如果是的话,那为什么不可以对于变
长数组就故意在run time初始化呢?不好意思说得这么啰嗦不知道我有没有说明白。
【在 c****g 的大作中提到】
: 你读懂了个P,run time allocate 的memory,你在compile time 初始化,还说读懂
: 了。compiler 又不知道你的size,怎么知道该给你整几个0 ?
: 不管你是不是暴力转行的,真要面C/C++,还是先系统学点基础知识,光闷头刷题也不
: 行。
c*g
51 楼
你是不是CS专业的呀?
initialized
【在 t*********3 的大作中提到】
: 多谢评论,我确实在“学点基础知识”,没有“光闷头刷题”,所以我才来问问题。如
: 果我只想刷题,我就没必要来问了。你的态度虽傲慢但并不重要,因为我只是想问问题
: 而已。
: 我看有人说variable length array cannot be initialized by any form of
: initialization syntax. 因为C11里面说The type of the entity to be initialized
: shall be an array of unknown size or a complete object type that is not a
: variable length array type.
: 总之他的意思就是说int a[n] = {0} 其中的 “={0}” 这种写法不能用到变长数组。
: 但是他这只是告诉我结论,我还是不明白问什么不能这么写。
: 你说变长数组不能“在compile time 初始化”。你的意思是“={0}” 其实就是在
initialized
【在 t*********3 的大作中提到】
: 多谢评论,我确实在“学点基础知识”,没有“光闷头刷题”,所以我才来问问题。如
: 果我只想刷题,我就没必要来问了。你的态度虽傲慢但并不重要,因为我只是想问问题
: 而已。
: 我看有人说variable length array cannot be initialized by any form of
: initialization syntax. 因为C11里面说The type of the entity to be initialized
: shall be an array of unknown size or a complete object type that is not a
: variable length array type.
: 总之他的意思就是说int a[n] = {0} 其中的 “={0}” 这种写法不能用到变长数组。
: 但是他这只是告诉我结论,我还是不明白问什么不能这么写。
: 你说变长数组不能“在compile time 初始化”。你的意思是“={0}” 其实就是在
n*n
52 楼
你为何要坚持用变长数组呢?用向量简单有效。
initialized
【在 t*********3 的大作中提到】
: 多谢评论,我确实在“学点基础知识”,没有“光闷头刷题”,所以我才来问问题。如
: 果我只想刷题,我就没必要来问了。你的态度虽傲慢但并不重要,因为我只是想问问题
: 而已。
: 我看有人说variable length array cannot be initialized by any form of
: initialization syntax. 因为C11里面说The type of the entity to be initialized
: shall be an array of unknown size or a complete object type that is not a
: variable length array type.
: 总之他的意思就是说int a[n] = {0} 其中的 “={0}” 这种写法不能用到变长数组。
: 但是他这只是告诉我结论,我还是不明白问什么不能这么写。
: 你说变长数组不能“在compile time 初始化”。你的意思是“={0}” 其实就是在
initialized
【在 t*********3 的大作中提到】
: 多谢评论,我确实在“学点基础知识”,没有“光闷头刷题”,所以我才来问问题。如
: 果我只想刷题,我就没必要来问了。你的态度虽傲慢但并不重要,因为我只是想问问题
: 而已。
: 我看有人说variable length array cannot be initialized by any form of
: initialization syntax. 因为C11里面说The type of the entity to be initialized
: shall be an array of unknown size or a complete object type that is not a
: variable length array type.
: 总之他的意思就是说int a[n] = {0} 其中的 “={0}” 这种写法不能用到变长数组。
: 但是他这只是告诉我结论,我还是不明白问什么不能这么写。
: 你说变长数组不能“在compile time 初始化”。你的意思是“={0}” 其实就是在
t*3
55 楼
我知道int a[n] = {0}无法在compile time初始化。但是为什么不能在run time初始化
呢?run time时已经知道n的值了。难道说“initialization”这个词就是指compile
time?
我又找了找,确实是C99不允许int a[n] = {0}当中的“={0}”。原因是standard
commettee认为如果是compile time无法初始化的东西,那就不能这么写。理论上确实
可以允许这么写,然后在run time初始化吧?但是他们就是不允许。
【在 c****g 的大作中提到】
: 你读懂了个P,run time allocate 的memory,你在compile time 初始化,还说读懂
: 了。compiler 又不知道你的size,怎么知道该给你整几个0 ?
: 不管你是不是暴力转行的,真要面C/C++,还是先系统学点基础知识,光闷头刷题也不
: 行。
呢?run time时已经知道n的值了。难道说“initialization”这个词就是指compile
time?
我又找了找,确实是C99不允许int a[n] = {0}当中的“={0}”。原因是standard
commettee认为如果是compile time无法初始化的东西,那就不能这么写。理论上确实
可以允许这么写,然后在run time初始化吧?但是他们就是不允许。
【在 c****g 的大作中提到】
: 你读懂了个P,run time allocate 的memory,你在compile time 初始化,还说读懂
: 了。compiler 又不知道你的size,怎么知道该给你整几个0 ?
: 不管你是不是暴力转行的,真要面C/C++,还是先系统学点基础知识,光闷头刷题也不
: 行。
l*i
57 楼
@cutegg
你能给一个详细的解释吗?
ttownal2013 也已经努力做了很多功课,也一直很谦虚呀.
你能给一个详细的解释吗?
ttownal2013 也已经努力做了很多功课,也一直很谦虚呀.
o*e
58 楼
checkout this
http://stackoverflow.com/questions/1887097/variable-length-arra
有些人自已没有constructive suggestion的,是因为从来没有人能教育他做人的道理。
【在 l*********i 的大作中提到】
: @cutegg
: 你能给一个详细的解释吗?
: ttownal2013 也已经努力做了很多功课,也一直很谦虚呀.
http://stackoverflow.com/questions/1887097/variable-length-arra
有些人自已没有constructive suggestion的,是因为从来没有人能教育他做人的道理。
【在 l*********i 的大作中提到】
: @cutegg
: 你能给一个详细的解释吗?
: ttownal2013 也已经努力做了很多功课,也一直很谦虚呀.
l*8
61 楼
可以的。
你的local,global数组要用memset初始化为0.
试试行不行。
leetcode
【在 t*********3 的大作中提到】
: Leetcode的Best Time to Buy and Sell Stock IV 那道题,我看到网上的答案就改了
: 下来:
: 请注意看定义数组local和global的那两行。如果按照int a[n]的方式定义,leetcode
: 能编译但是输出错误。请问这是为什么?
: class Solution {
: public:
: int maxProfit(int k, vector& prices) {
: int days = prices.size();
: if(days == 0) return 0;
: if(k >= days/2) return maxProfitUnlimitedTimes(prices);
你的local,global数组要用memset初始化为0.
试试行不行。
leetcode
【在 t*********3 的大作中提到】
: Leetcode的Best Time to Buy and Sell Stock IV 那道题,我看到网上的答案就改了
: 下来:
: 请注意看定义数组local和global的那两行。如果按照int a[n]的方式定义,leetcode
: 能编译但是输出错误。请问这是为什么?
: class Solution {
: public:
: int maxProfit(int k, vector
: int days = prices.size();
: if(days == 0) return 0;
: if(k >= days/2) return maxProfitUnlimitedTimes(prices);
c*g
62 楼
这儿有一段讲得比较清楚的,要人家还是不明白,我
真没办法了。
http://www.geeksforgeeks.org/memory-layout-of-c-program/
简尔言之,dynamic (runtime) allocation 用的时heap
static (compile time) allocation 用的是 stack 和 data segment
其中data segment 是用来存static 和 global variable 的。
从语法来说,用叉叉alloc function 和 new 的,是
Runtime allocation, 其它(C99 的 a[n]除外) 是compile time allocation.
【在 l*********i 的大作中提到】
: @cutegg
: 你能给一个详细的解释吗?
: ttownal2013 也已经努力做了很多功课,也一直很谦虚呀.
相关阅读
为哈Nexus没有固定厂家?请推荐nook color的root版本?N4开卖了。快抢sprint 好用吗大家有什么好玩的App可以推荐啊请教 这个图标是什么意思建议大家挑戏当地卖Nexus 4的黄牛。不陪狗狗玩了手机可以暂停服务吗我在等一款精致高端的4.3-4.5寸智能机一个建议(关于各平台的应用)Apple 把mapping team的头儿解雇了又一个Nexus 4的黄牛verizon 的 iphone5 拿回国能用么怎么没人申请隔壁的版主啊在美国你们nexus7 3G 32G版的卖多少钱Amazon要是没有那个Prime真是渣啊想买nexus 7 32gb, 下午office depot的deal没赶上。还有办法吗?surface 会不会卖99美元win phone app上的不足