由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - leetcode上zigzag converstion那题怎么才能通过large?
相关主题
ZigZag 又读不懂题了,求助!IF语句&&前后换个顺序就超时!!!搞笑啊!!!
Memory Limit Exceeded 错误T onsite一题
面到reverse words in string过不了leetcode Zigzag Level Order Traversal
lc最变态的是不是那个word ladder ii?请问大牛们Leetcode Palindrome Number 这道题(思路很简单,就是程序写不对)
不明白leetcode OJ wordladder 2 总是 Time Limit ExceededG电面一题
Time limit exceeded for Word Ladder(leetcode)关于string的substr的问题
求DEBUG Substring with Concatenation of All Words问道FB的题
SUM3这道题请教一道面试题
相关话题的讨论汇总
话题: nrows话题: int话题: string话题: ret话题: return
进入JobHunting版参与讨论
1 (共1页)
B********t
发帖数: 147
1
小弟的代码在此,能通过small的test,但是large时说memory limit exceeded,难道
这题还能in place不成?
class Solution {
public:
string convert(string s, int nRows) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(nRows == 1)
return s;
int circle = 2*nRows - 2;
string ret;
for(int i = 0; i < nRows; i++)
{
int j = i;
while(j < s.size())
{
ret.push_back(s[j]);
j += circle;
if(i != 0 && i != nRows - 1)
if((j - i*2) < s.size())
ret.push_back(s[j - i*2]);
}
}
return ret;
}
};
d**********x
发帖数: 4083
2
大体上没错吧,你不妨先给ret分配好空间,直接赋值进去
或者至少reserve一下。

【在 B********t 的大作中提到】
: 小弟的代码在此,能通过small的test,但是large时说memory limit exceeded,难道
: 这题还能in place不成?
: class Solution {
: public:
: string convert(string s, int nRows) {
: // Start typing your C/C++ solution below
: // DO NOT write int main() function
: if(nRows == 1)
: return s;
: int circle = 2*nRows - 2;

l*****a
发帖数: 14598
3
你弄个stringbuilder
然后一列一列append,每列也是个stringbuilder,偶数列头尾没有reverse 之后在插入
总结果
###注意最后一列填满
最后对于总的string.
for(int j=0;j //这是一行的
for(int i=0;i str.charAt(i*col+j)
}

【在 B********t 的大作中提到】
: 小弟的代码在此,能通过small的test,但是large时说memory limit exceeded,难道
: 这题还能in place不成?
: class Solution {
: public:
: string convert(string s, int nRows) {
: // Start typing your C/C++ solution below
: // DO NOT write int main() function
: if(nRows == 1)
: return s;
: int circle = 2*nRows - 2;

p****e
发帖数: 3548
4
string convert(string s, int nRows) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(nRows <= 1) return s;
int size = s.length();
if(size <=nRows) return s;
int direction = 1;
int row = 0;
int period = 2*nRows-2;
string * ret = new string[nRows];
for(int i = 0; i < size; i++)
{
ret[row].push_back(s[i]);
int modi = i%period;
if(modi == 0)
direction = 1;
else if(modi == nRows-1)
direction = -1;
row += direction;
}
string rlt("");
for(int i = 0; i rlt += ret[i];
delete [] ret;
return rlt;
}
l*****a
发帖数: 14598
5
rlt += ret[i];
这东西放在循环中,好像效率很差得样子

【在 p****e 的大作中提到】
: string convert(string s, int nRows) {
: // Start typing your C/C++ solution below
: // DO NOT write int main() function
: if(nRows <= 1) return s;
: int size = s.length();
: if(size <=nRows) return s;
: int direction = 1;
: int row = 0;
: int period = 2*nRows-2;
: string * ret = new string[nRows];

p****e
发帖数: 3548
6
可能用append好点
(刚开始看成是要做W形,就做成这样了)

【在 l*****a 的大作中提到】
: rlt += ret[i];
: 这东西放在循环中,好像效率很差得样子

p****e
发帖数: 3548
7
你这个加入
if(nRows<= 1) return s;
就行了

【在 B********t 的大作中提到】
: 小弟的代码在此,能通过small的test,但是large时说memory limit exceeded,难道
: 这题还能in place不成?
: class Solution {
: public:
: string convert(string s, int nRows) {
: // Start typing your C/C++ solution below
: // DO NOT write int main() function
: if(nRows == 1)
: return s;
: int circle = 2*nRows - 2;

B********t
发帖数: 147
8
啊!!!! 是的。。。因为 nRows = 1时 死循环了。。多谢啦

【在 p****e 的大作中提到】
: 你这个加入
: if(nRows<= 1) return s;
: 就行了

P*******b
发帖数: 1001
9
这道题遇到了我就认栽了

【在 B********t 的大作中提到】
: 小弟的代码在此,能通过small的test,但是large时说memory limit exceeded,难道
: 这题还能in place不成?
: class Solution {
: public:
: string convert(string s, int nRows) {
: // Start typing your C/C++ solution below
: // DO NOT write int main() function
: if(nRows == 1)
: return s;
: int circle = 2*nRows - 2;

l*****a
发帖数: 14598
10
why?
看看我的reply.不就是暴力吗?

【在 P*******b 的大作中提到】
: 这道题遇到了我就认栽了
1 (共1页)
进入JobHunting版参与讨论
相关主题
请教一道面试题不明白leetcode OJ wordladder 2 总是 Time Limit Exceeded
给大家推荐个网站,interviewstreet.comTime limit exceeded for Word Ladder(leetcode)
3sum on LeetCode OJ求DEBUG Substring with Concatenation of All Words
leetcode online judge Longest Palindromic Substring memory limit exceededSUM3这道题
ZigZag 又读不懂题了,求助!IF语句&&前后换个顺序就超时!!!搞笑啊!!!
Memory Limit Exceeded 错误T onsite一题
面到reverse words in string过不了leetcode Zigzag Level Order Traversal
lc最变态的是不是那个word ladder ii?请问大牛们Leetcode Palindrome Number 这道题(思路很简单,就是程序写不对)
相关话题的讨论汇总
话题: nrows话题: int话题: string话题: ret话题: return