<?php

$array=array(
             array("name","word","hello","haha"),
             array("user","push","array","pop"),
             array("code","course","content","public"),
    );

#计算该数组中 o 出现的频率

$num=0;
// 1.常规解法 遍历 时间复杂度 m*n
// foreach ($array as $k => $v)
// {
//     foreach ($v as $val)
//     {
//         if(strpos($val,"o")!==false)
//         {
//             $num++;
//         }
//     }
// }

// 2.运用函数 减少循环 时间复杂度m
// foreach ($array as $k => $v)
// {
//     $string=implode("",$v);//数组变字符串
//     $num+= substr_count($string,'o');//查找出现的次数并累加
// }

// 3.运用json 剔除循环 时间复杂度 1
// $string=json_encode($array);
// $num+= substr_count($string,'o');



echo $num;

?>

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-17
  • 2022-12-23
  • 2021-07-12
猜你喜欢
  • 2021-10-29
  • 2022-12-23
  • 2022-12-23
  • 2021-09-24
  • 2022-12-23
  • 2021-11-29
  • 2022-12-23
相关资源
相似解决方案