由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 为什么加个结束符leetcode就run time error呢?
相关主题
leetcode: Remove Duplicates from Sorted Array请教一道面试题
请教一道题Google店面刚结束
我再说说我挂掉的那道题吧Amazon 电面归来
顶风上来问道题:一个很大char[], 如何in-place 删除重复元素Question on email from Google
贡献两道的面试题两道algorithm电面题(update 答案)
[合集] 新鲜热辣Google Phone Interview题问个常见算法题的变形
remove duplicate in sorted array问个google面试题
amazon tel interviewlongest subarray with numbers arranged as a seq
相关话题的讨论汇总
话题: index话题: 结束符话题: newlen话题: int
进入JobHunting版参与讨论
1 (共1页)
W********e
发帖数: 45
1
remove duplicate in sorted array这道题,虽然现在leetcode已经没有这题了,只有
follow up 的judge。不过我还是写了一段代码,顺便用leetcode来judge一下。不知道
为什么我在A数组最后加了一个结束符,就出现run time error了?而我用c-free验证
却没问题?代码如下,麻烦大家了!
class Solution {
public:
int removeDuplicates(int A[], int n) {
int newArrIndex=0,index=0,newlen=0;
if(n==0)
return 0;
while(index {
if(A[index]==A[index+1])
{
A[newArrIndex]=A[index];
}
else
{
A[newArrIndex]=A[index]; //这种情况包括了A【index+1】是结束符
的情况
newArrIndex++;
newlen++;
}
index++;
}

A[newArrIndex]='\0'; //到底要不要加结束符?????????
return newlen;
}
};
o****d
发帖数: 2835
2
1 why you need to add '\0', it is not a c string.
2 A[index+1] is out of array bound when index is n-1
3 if the input does not have duplicates, what happens when you add '\0'?

【在 W********e 的大作中提到】
: remove duplicate in sorted array这道题,虽然现在leetcode已经没有这题了,只有
: follow up 的judge。不过我还是写了一段代码,顺便用leetcode来judge一下。不知道
: 为什么我在A数组最后加了一个结束符,就出现run time error了?而我用c-free验证
: 却没问题?代码如下,麻烦大家了!
: class Solution {
: public:
: int removeDuplicates(int A[], int n) {
: int newArrIndex=0,index=0,newlen=0;
: if(n==0)
: return 0;

1 (共1页)
进入JobHunting版参与讨论
相关主题
longest subarray with numbers arranged as a seq贡献两道的面试题
[emc/greenplum面试]senior engineer[合集] 新鲜热辣Google Phone Interview题
LinkedIn家电面面经remove duplicate in sorted array
FB电面amazon tel interview
leetcode: Remove Duplicates from Sorted Array请教一道面试题
请教一道题Google店面刚结束
我再说说我挂掉的那道题吧Amazon 电面归来
顶风上来问道题:一个很大char[], 如何in-place 删除重复元素Question on email from Google
相关话题的讨论汇总
话题: index话题: 结束符话题: newlen话题: int