我遇到的编程面试问题:反转字符串并找到数组的模式 我对一家大的科技公司进行了C++编程测试,这是不可命名的。我得到了以下代码块,并要求使用它们反转字符串并返回数组模式 char * reverse(char * str, int n) { ... }

我遇到的编程面试问题:反转字符串并找到数组的模式 我对一家大的科技公司进行了C++编程测试,这是不可命名的。我得到了以下代码块,并要求使用它们反转字符串并返回数组模式 char * reverse(char * str, int n) { ... },c++,algorithm,hivert,C++,Algorithm,Hivert,及 我不确定我是否用了“最好”的方法,但我很好奇Stack Overflow的高级牧师会怎么做。我也很好奇为什么他们在第一个案例中给了我这么奇怪的原型。作为一个空函数不是更好吗?在这两种情况下,他们不应该为n给出我的size\t 我写 for (char* i(str), j(str+n); i < j; ++i, --j) std::swap(*i, *j) return str; for(字符*i(str),j(str+n);i

我不确定我是否用了“最好”的方法,但我很好奇Stack Overflow的高级牧师会怎么做。我也很好奇为什么他们在第一个案例中给了我这么奇怪的原型。作为一个空函数不是更好吗?在这两种情况下,他们不应该为
n
给出我的
size\t

我写

for (char* i(str), j(str+n); i < j; ++i, --j)
    std::swap(*i, *j)
return str;
for(字符*i(str),j(str+n);i

if(n==0)
抛出(“不能有空数组的模式,兄弟。”)
std::map-cntMap;
std::对顶(arr[0],1);
cntMap.insert(顶部);
for(int k(1);k顶秒)
top=std::make_pair(*k,cntMap[*k])
返回顶部。首先;
}

我失败了吗?

第一个for循环显然是错误的,至少有两个原因:
I
j
的类型,以及数组最后一个元素的索引,它应该是
str+n-1

for (char *i = str, *j = str+n-1; i < j; ++i, --j)
    std::swap(*i, *j)
return str;
for(char*i=str,*j=str+n-1;i
您的第二个代码过于复杂

你只需要:

++cntMap[*k];
您只需要跟踪最高计数的“k”值,无需将其配对

第二,你的
返回top.first在循环完成之前,这显然不是您想要的


您使用的是
intk
,然后使用
*k
,显然无法编译。这也适用于
反向
功能中的
*i
*j
。你似乎混淆了整数索引和迭代器

对于第一个问题,字符串是否有空终止符?暂时忽略由提出的问题(尽管这是一个非常有效的问题),交换第一个数组值和最后一个数组值会将空字符放在字符串的开头,这对大多数字符串打印函数来说,告诉它们“这里没有字符串”

另外,您是否确保(n-1)=字符串长度,或者仅仅是数组大小?如果字符串长度小于(n-1),则会有垃圾数据通过空终止符…您将继续交换到字符串的前面,导致大多数字符串打印函数打印垃圾数据,然后在“良好”反转字符串出现之前停止

因此,您需要按照以下思路做一些事情:

char * reverse(char * str, int n)
{
   // Sanity check, if the string starts with null terminator, 
   // there's nothing in the string.
   if(str[0] = NULL)
   {
       return str;
   }
   size_t i, j = 0;
   // While you aren't at the end of the array and
   // while the next character is not null (0x00)
   while((i < n) && (str[i+1] != NULL))
   {
       i++; // Found another good character
   }
   // You're going to process the array from both ends. 
   // i tells you the end, j the front.
   while(j < i)
   {
       // Leaves the null terminator alone.
       swap(str[i], str[j]);
       i--;
       j++;
   }
return str;
}
char*反向(char*str,int-n)
{
//健全性检查,如果字符串以null终止符开头,
//绳子上什么也没有。
如果(str[0]=NULL)
{
返回str;
}
尺寸i,j=0;
//当您不在数组的末尾时
//而下一个字符不为空(0x00)
而((i

编辑:意识到我发布的代码有自己的错误。为角落案例欢呼。

它有效吗?你测试过所有的角点案例吗?关于数组模式,我的第一个问题是问“是否排序”?这是我在网上参加的一个测试,时间非常有限。这是我第一次看到询问整数数组模式的面试问题。这个问题似乎离题了,因为它是关于代码审查的。在codereview.stackexchange.comMode上发布是数据中最常出现的值,是标准英语吗?我从未听说过(我不是以英语为母语的人)。我不完全确定,但这是标准(和基本)统计数据()。它当然不像
平均值
中值
那样经常使用,至少在我的经验中是这样。。
++cntMap[*k];
char * reverse(char * str, int n)
{
   // Sanity check, if the string starts with null terminator, 
   // there's nothing in the string.
   if(str[0] = NULL)
   {
       return str;
   }
   size_t i, j = 0;
   // While you aren't at the end of the array and
   // while the next character is not null (0x00)
   while((i < n) && (str[i+1] != NULL))
   {
       i++; // Found another good character
   }
   // You're going to process the array from both ends. 
   // i tells you the end, j the front.
   while(j < i)
   {
       // Leaves the null terminator alone.
       swap(str[i], str[j]);
       i--;
       j++;
   }
return str;
}