2017-05-09 44 views
0

我们需要检查一个号码中个别数字的出现是否相同。为2244(2次出现2次,4次出现2次)。因此,两位数的出现是相同的。检查一个号码中个别数字的出现是否相同

//This will return true if occurrence of individual digit in 
//a number is same else false 

bool stable(int no) 
{ 
    vector<int> v1; 
    int k , count = 1; 
    int arr[10]; 
    //Initializing all arr[] -> 0 
    for(int k = 0 ; k < 10 ;k++) 
    { 
     arr[k] = 0; 
    } 
    while(no != 0) 
    { 
     k=no%10; 
     arr[k]++; 
     no=no/10; 
    } 
    for(int i = 0 ; i < 10 ; i++) 
    { 
     if(arr[i] != 0) 
     { 
      v1.push_back(arr[i]); //storing count of individual digits 
     } 
    } 
    vector<int>::iterator it , it2; 
    for(it = v1.begin()+1 ,it2 = v1.begin(); it != v1.end() ; it++,it2++) 
    { 
     if(*it == *it2) //if all the values are same return true else false 
     { 
      count++; 
     } 
    } 
    if(count == v1.size()) return true; 
     return false; 
} 

但此代码不会因为没有像2222,1111,444工作。另外,你会提出一些优化代码的好方法吗?

+0

因为2222是稳定的(2是唯一出现4的数字),所以你的代码对'2222'返回true,你的代码看起来是正确的。 –

+0

Shubh,你能解释一下代码不适用于这些数字吗?你有没有做过任何调试? – halfer

+0

Thanx @RajeevSingh,其实在我的机器上做这个我已经设置count = 0这是我得到一个错误。 –

回答

1

我认为你正在做比它需要更加努力这个(或我误解严重的问题,这经常发生)。

假设是要求如下所述:给定一个非零的正值,如果所有数字都以相同的频率出现,包括1(例如:1122,2222,1234全部合格,因为没有数字比任何其他频率更高)。

的算法是简单的:

  1. 为小于10的任何值快速返回;一个数字号码立即被限定。
  2. 建立模数残差的计数器阵列。
  3. 查找数组中的第一个非零值
  4. 从这一点开始,找到与(4)中的值不匹配的第一个非零值。如果到达序列的末尾而没有发现这种差异,则原始数字中的所有数字都必须具有相同的计数。

在所有的复杂度是对数base-10到输入数加上恒定大小数组(10个元素)的单次扫描。

示例代码

#include <algorithm> 
#include <iterator> 

bool stable(unsigned value) 
{ 
    if (value < 10) // single digit only, including zero 
     return true; 

    unsigned ar[10]={0}; 
    do { ++ar[value%10]; } 
    while (value /= 10); 

    auto it = std::find_if(std::begin(ar), std::end(ar), 
          [](auto n) { return n != 0; }); 
    return std::find_if(std::next(it), std::end(ar), 
         [it](auto n){ return n && (n != *it);}) == std::end(ar); 
} 

你总是可以进一步通过该保留的最大位数计数和没有查找操作留下,如果它最终为1(例如:1234,102938这样的例子)。我会把它作为一个练习,让你进行基准测试,以确定是否有任何性能优势。我真的怀疑会有。

1

试试这个:

bool stable(int no) 
{ 
    std::vector<int> v1; 

    while (no != 0){ 
     v1.push_back(no%10); 
     no /= 10; 
    } 

    std::sort(v1.begin(), v1.end()); //SORTING DIGITS IN ASCENDING ORDER 

    std::vector<int> index = {0}; //BUILDING VEC WITH INDEXES WHERE CHANGES HAPPEN 
    for (unsigned int i = 0; i < v1.size()-1; ++i){ 
     if (v1[i] != v1[i+1]) 
      index.push_back(i+1); 
    } 
    //EDGE CASE WHEN ONLY 1 DIGIT APPEARS (e.g. 555) 
    if (index.size() == 1) 
     return true; 

    //CHECKING THAT ALL INDEXES ARE EQUALLY SEPARATED 
    int diff = index[1] - index[0]; 
    for (unsigned int i = 1; i < index.size()-1; ++i){ 
     if (index[i+1] - index[i] != diff) 
      return false; 
    } 
    return true; 
} 
0

虽然检查是否所有的重复计数是相同的,您可以直接返回false如果计数不匹配,没有必要进一步检查。如果矢量只包含一个像2222, 1111这样的数字的单个计数,它将返回true

vector<int>::iterator it , it2; 
for(it = v1.begin()+1 ,it2 = v1.begin(); it != v1.end() ; it++,it2++) 
{ 
    if(*it != *it2) //if two values are not same return false 
    { 
     return false; 
    } 
} 
return true; 
相关问题