Php 十进制数的二进制等价物中两个1之间的最大间隙

Php 十进制数的二进制等价物中两个1之间的最大间隙,php,algorithm,Php,Algorithm,我想写一个程序,在一个相当于十进制数的二进制中找出两个1之间的最大间隔。例如,对于100101:间隙为2,对于10101:间隙为1 <?php $numberGiven = 251; $binaryForm = decbin($numberGiven); $status = false; $count = 0; for($i = 0; $i < strlen($binaryForm); $i++) { var_dump($binaryForm[$i]); if($b

我想写一个程序,在一个相当于十进制数的二进制中找出两个1之间的最大间隔。例如,对于100101:间隙为2,对于10101:间隙为1

<?php
$numberGiven = 251;
$binaryForm = decbin($numberGiven);

$status = false;
$count = 0;
for($i = 0; $i < strlen($binaryForm); $i++)
{
    var_dump($binaryForm[$i]);
    if($binaryForm[$i] == 1)
    {
        $status = false;
        $count = 0;
    }
    else
    {
        $status = true;
        $count += 1;
    }
}
echo "count = " . $count . "<br>";
echo $binaryForm;
?>

但我没有成功。

您当前正在做的是在每次找到1时重置计数

您需要跟踪当前最大值:

$count = 0;
$maxCount = 0;
在设置$count=0的地方,也应该这样做

if ($count > $maxCount)
  $maxCount = $count;
$count = 0;
然后

总共:

<?php
$numberGiven = 251;
$binaryForm = decbin($numberGiven);

$status = false;    
$count = 0;
$maxCount = 0;

for($i = 0; $i < strlen($binaryForm); $i++)
{
  // Don't count leading zeroes.
  if ($status == false && $binaryForm[$i] == 0) continue;
  $status = true;

  var_dump($binaryForm[$i]);

  // We've found a 1. Remember the count.
  if($binaryForm[$i] == 1)
  {
    if ($count > $maxCount)
      $maxCount = $count;
    $count = 0;
  }
  // We found a 0. Add one to count.
  else
  {
    $count += 1;
  }
}

echo "count = " . $count . "<br>";
echo $binaryForm;
?>
免责声明:未测试的代码

我将使用>>并迭代移位1位,检查当前最右边的位是否为1,直到我检查了所有位。如果找到1,则计算上一个1之间的间隙:

foreach(array(5,17,25,1223243) as $number) {
    $lastpos = -1; 
    $gap = -1; // means there are zero or excatly one '1's

    // PHP_INT_SIZE contains the number of bytes an integer
    // will consume on your system. The value * 8 is the number of bits.
    for($pos=0; $pos < PHP_INT_SIZE * 8; $pos++) {
        if(($number >> $pos) & 1) {
            if($lastpos !== -1) {
                $gap = max($gap, $pos - $lastpos -1);
            }
            $lastpos = $pos;
        }
    }   

    echo "$number " . decbin($number) . "  ";
    echo "max gap: {$gap}\n";
}

首先使用正则表达式查找0个组,然后按长度排序,取列表中的第一个组,并获取其长度:

$numberGiven = 37;
$binaryForm = decbin($numberGiven);

// get all groups of "0", put list in $matches
preg_match_all('/(0+)/', $binaryForm, $matches_all);
$matches = $matches_all[0];

// sort descending
rsort($matches, SORT_STRING);

// get first `$matches[0]` and print string length
echo 'count = ' . strlen($matches[0]) . '<br>';
echo $binaryForm;

怎么会不成功呢?你会犯错误吗;如果有,哪一个?你有没有得到任何输出;如果是的话,是哪一个?这是家庭作业,还是有具体的使用案例?也许有人可以建议一种完全不同的方法。是的……您需要提供更多信息,以便我们充分回答您的问题。不需要usort:preg_match_all'/10+/',$data,$matches;排序$matches[1],排序字符串$长度=strlenarray_pop$匹配[1];var_dump$length;很好的代码,但是这个解决方案不包括结束零。它应该只匹配1’谢谢你的回答。我希望您能帮助我更好地理解解决方案。我真的不明白你的方法是什么。我用的是二进制逻辑。为什么I&a数字如果1我可以检查最右边的字节是否为1。如果你一位接一位地右移,你可以数到1。函数解决方案$N{return maxarray_map'strlen',explode'1',decbin$N;}嘿@HADI我鼓励你将此作为答案发布。我对这里的一些性能测试也很好奇,可能你的测试速度更快。@hek2mgl不知道性能测试。刚刚测试过代码,工作起来很有魅力。但我坚信这段代码的性能会很低。仅供参考,主题已关闭,因此在此添加了注释。函数解决方案$N{$binaryForm=decbin$N;preg_match_all'/0*1/',$binaryForm$matches;rsort$matches[1],SORT_STRING;if$matches[1][0]!={return strlen matches$matches[1][0];}return 0;}
$numberGiven = 37;
$binaryForm = decbin($numberGiven);

// get all groups of "0", put list in $matches
preg_match_all('/(0+)/', $binaryForm, $matches_all);
$matches = $matches_all[0];

// sort descending
rsort($matches, SORT_STRING);

// get first `$matches[0]` and print string length
echo 'count = ' . strlen($matches[0]) . '<br>';
echo $binaryForm;
preg_match_all('/(0+)1/', $binaryForm, $matches_all);
$matches = $matches_all[1];