avatar
神油刷机# PDA - 掌中宝
N*B
1
家里的存货不是太大就是太小, 扫了几个都不好意思告诉神医
谁指点下则么在uline买,5个包子
avatar
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; idiff = 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
avatar
o*e
3
avatar
r*a
4
原厂的asus ac68u刷成merlin还需要downgrade和修改cfe吗?谢谢。
avatar
z*3
5
14*12*8
avatar
n*n
6
c/c++里数组长度只能是常数,不能是变量
avatar
j*9
7
How are your stocks doing right now?
avatar
z*e
8
no

【在 r**a 的大作中提到】
: 原厂的asus ac68u刷成merlin还需要downgrade和修改cfe吗?谢谢。
avatar
s*r
9
太贵,还有SHIPPING
还不如去HOMEDEPOT买 16×12×12的,现在特价才57分还是67分的~
avatar
n*n
10
可以是变量。C99

【在 n*****n 的大作中提到】
: c/c++里数组长度只能是常数,不能是变量
avatar
w*t
11
上拨菜楼...
美股现在成了拉磨的驴了...
avatar
r*a
12
多谢。

【在 z*********e 的大作中提到】
: no
avatar
m*r
13
EN,NA HAI BU CUO

【在 s***r 的大作中提到】
: 太贵,还有SHIPPING
: 还不如去HOMEDEPOT买 16×12×12的,现在特价才57分还是67分的~

avatar
s*n
14
还是用vector resize下安稳吧
avatar
W*t
15
不错啥啊
要塞多少纸...........纸都oos了

【在 m*r 的大作中提到】
: EN,NA HAI BU CUO
avatar
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);

avatar
W*t
17
还是amazon以前的一个箱子好
刚好fit

【在 m*r 的大作中提到】
: EN,NA HAI BU CUO
avatar
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

avatar
c*n
19
13X13*8
avatar
n*n
20
gdb看一下。

【在 t*********3 的大作中提到】
: 输出错误的case是这个:
: Input: 2, [3,2,6,5,0,3]
: Output: 20292312
: Expected: 7
:
: +
: where

avatar
N*B
21
头2位请收包子,谢谢
avatar
j*g
22
我觉得是初始化问题,你加个memset(local,0,sizeof(local));试试
avatar
n*n
23
直接拿C写吧!

【在 j********g 的大作中提到】
: 我觉得是初始化问题,你加个memset(local,0,sizeof(local));试试
avatar
m*n
24
Then what I said is unlikely to be the problem. This case is small. Perhaps
you can trace it in gdb.

【在 t*********3 的大作中提到】
: 输出错误的case是这个:
: Input: 2, [3,2,6,5,0,3]
: Output: 20292312
: Expected: 7
:
: +
: where

avatar
n*e
25
用vector或者动态分配,用new和delete
avatar
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);

avatar
r*g
27
问这种问题看了的确觉得无奈
不过lz可能是转行的啦 包容点吧

【在 c****g 的大作中提到】
: 天啦。。。现在的CS是不是只学算法,连static allocate, dynamic allocate 这些基
: 本概念都不学了?
:
: leetcode

avatar
t*3
28
确实是初始化的问题,加了memset就行了。我原以为 int local[days][k+1] = {0}
就可以把数组初始化为0,但是GDB之后发现不行。

【在 j********g 的大作中提到】
: 我觉得是初始化问题,你加个memset(local,0,sizeof(local));试试
avatar
t*3
29
这个为什么是和static allocate, danamic allocate相关呢?这两种allocate对初始
化要求不同吗?

【在 c****g 的大作中提到】
: 天啦。。。现在的CS是不是只学算法,连static allocate, dynamic allocate 这些基
: 本概念都不学了?
:
: leetcode

avatar
c*g
30
更无奈的是好多回答,几乎没一个答在点子上的,这年头
刷题刷成这样。。。

【在 r*g 的大作中提到】
: 问这种问题看了的确觉得无奈
: 不过lz可能是转行的啦 包容点吧

avatar
t*3
31
请问“点子”应该是什么?谢谢

【在 c****g 的大作中提到】
: 更无奈的是好多回答,几乎没一个答在点子上的,这年头
: 刷题刷成这样。。。

avatar
c*g
32
知道static 和dynamic allocation 的区别不?学过compiler 没有?

【在 t*********3 的大作中提到】
: 请问“点子”应该是什么?谢谢
avatar
t*3
33
我只知道一个是固定的,一个可以是变长的。C不能写int a[n],但是新一些的C++编译
器可以这么写。

【在 c****g 的大作中提到】
: 知道static 和dynamic allocation 的区别不?学过compiler 没有?
avatar
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++编译
: 器可以这么写。

avatar
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++, 犯这种错误,刷再多的题都没用了。

avatar
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++, 犯这种错误,刷再多的题都没用了。

avatar
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++, 犯这种错误,刷再多的题都没用了。

avatar
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?
:
: 它的

avatar
s*e
39
楼上的态度还是好点吧。。。
avatar
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);

avatar
c*g
41
现在除了刷题,写几个function, 估计啥都
没人愿意学了。G的刷题文化最后搞出这么
个结果。

【在 h**********e 的大作中提到】
: 现在是不是真没人学c/c++了啊
avatar
h*e
42
looks like c/c++ is almost useless in SD nowadays
i know multiple experienced SDEs (who worked in MS, G ... for years) do not
know what namespace is

【在 c****g 的大作中提到】
: 现在除了刷题,写几个function, 估计啥都
: 没人愿意学了。G的刷题文化最后搞出这么
: 个结果。

avatar
n*n
43
语言高级化,大势所趋。跟刷题关系不大。

【在 c****g 的大作中提到】
: 现在除了刷题,写几个function, 估计啥都
: 没人愿意学了。G的刷题文化最后搞出这么
: 个结果。

avatar
c*g
44
干脆直接说不会C/C++也行。但说会又用了,
却连最毛皮的东西都不懂,这个在面试的时候
是最大的忌讳,人家会以为你整体就这水平。
如果简历中写了,面试时可能被问到的东西,
最起码要花点时间把基本知识搞懂,这个懒
偷不得。

not

【在 h**********e 的大作中提到】
: looks like c/c++ is almost useless in SD nowadays
: i know multiple experienced SDEs (who worked in MS, G ... for years) do not
: know what namespace is

avatar
c*g
45

现在的CS学不学系统编程? 俺的映像中
以前好像是必修课。

【在 n******n 的大作中提到】
: 语言高级化,大势所趋。跟刷题关系不大。
avatar
h*e
46
but now you do not need any CS class taken to have a FLG offer

【在 c****g 的大作中提到】
:
: 现在的CS学不学系统编程? 俺的映像中
: 以前好像是必修课。

avatar
c*g
47
没学过的不会没关系,只要有能力,需要的时候
可以补。学过的,有CS学位的,还要犯一些低
级错误就麻烦了。

【在 h**********e 的大作中提到】
: but now you do not need any CS class taken to have a FLG offer
avatar
r*g
48
莫生气
ps:看你说话口气很牛
给推荐个工作吧lz这个我都会
本科通读过effective c++, c++ primer 4
c的陷阱与缺陷
c与指针
c专家编程
会凸优化 机器学习理论 讲过泛函分析
会统计推断
new grad master
做过几年research写过air force的项目
做过bioinformatics
但无工作经验
开张太难找不到工作

【在 c****g 的大作中提到】
: 现在除了刷题,写几个function, 估计啥都
: 没人愿意学了。G的刷题文化最后搞出这么
: 个结果。

avatar
r*g
49
感叹找个工作真难
5月份来加州看看
感觉各种搬砖技能学的再多
也不如刷题刷到手酸
可能这就是为啥现在大家都刷题吧

【在 r*g 的大作中提到】
: 莫生气
: ps:看你说话口气很牛
: 给推荐个工作吧lz这个我都会
: 本科通读过effective c++, c++ primer 4
: c的陷阱与缺陷
: c与指针
: c专家编程
: 会凸优化 机器学习理论 讲过泛函分析
: 会统计推断
: new grad master

avatar
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++,还是先系统学点基础知识,光闷头刷题也不
: 行。

avatar
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}” 其实就是在

avatar
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}” 其实就是在

avatar
n*n
53
显然不是。你何必问呢?

【在 c****g 的大作中提到】
: 你是不是CS专业的呀?
:
: initialized

avatar
b*5
54
什么是系统编程? 我美国正规学校出来的, 也没学过什么系统编程。。。

【在 c****g 的大作中提到】
: 你是不是CS专业的呀?
:
: initialized

avatar
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++,还是先系统学点基础知识,光闷头刷题也不
: 行。

avatar
c*g
56
我服了你。

【在 t*********3 的大作中提到】
: 我知道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初始化吧?但是他们就是不允许。

avatar
l*i
57
@cutegg
你能给一个详细的解释吗?
ttownal2013 也已经努力做了很多功课,也一直很谦虚呀.
avatar
o*e
58
checkout this
http://stackoverflow.com/questions/1887097/variable-length-arra
有些人自已没有constructive suggestion的,是因为从来没有人能教育他做人的道理。

【在 l*********i 的大作中提到】
: @cutegg
: 你能给一个详细的解释吗?
: ttownal2013 也已经努力做了很多功课,也一直很谦虚呀.

avatar
n*n
59
建议从教科书看起。显然他知识背景上缺口很多,这种问答方式用处不大。

【在 l*********i 的大作中提到】
: @cutegg
: 你能给一个详细的解释吗?
: ttownal2013 也已经努力做了很多功课,也一直很谦虚呀.

avatar
c*g
60
大费劲了。我就是敲三千字,他都未必觉得详细。
要是一个老外,连你好吗,你妈好,妈你好都还
闹不明白,你非得跟他讲唐诗宋词,停车做爱枫林晚双月红于二月花啥的,
既是自虐也是折磨别人。
系统学习不能太浮躁。花点时间,找本书看,再带着问题来讨论,
大家都好受点。

【在 l*********i 的大作中提到】
: @cutegg
: 你能给一个详细的解释吗?
: ttownal2013 也已经努力做了很多功课,也一直很谦虚呀.

avatar
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);

avatar
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 也已经努力做了很多功课,也一直很谦虚呀.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。