1、封禁策略为一个自然分钟内请求签到接口500次则封禁该IP10分钟,如何操作?
get($bannedKey)) { return true; } //获取次数key $ipNumKey = 'redis_cache_num_'. $ip $ipNum = $redis->incr($ipNumKey); if ($ipNum >= 500) { $redis->setex($bannedKey,600); return false; } return true; }?>
2、只提供10M内存,现在要做一个活动,参与活动的用户userId为1~10000000,请问如何设计?
可考虑用redis的setbit和getbit命令来实现此需求,对其userId进行占位,并且8各userId的占位才占有1B的空间,所有10M空间足足有余
3、下面代码的输出值为?
如果不明白,则有直达地址:
4、实现一个base62_encode()和base62_decode()方法,要求base62_encode(1)=1,base62_encode(61)=z,base62_decode('z')=61;语言不限
= 0; $t--) { $a = floor($str / pow(62, $t)); $out = $out . substr($this->string, $a, 1); $str = $str - ($a * pow(62, $t)); } return $out; } public function base62_decode($str) { $out = 0; $len = strlen($str) - 1; for ($t = 0; $t <= $len; $t++) { $out = $out + strpos($this->string, substr($str, $t, 1)) * pow(62, $len - $t); } return substr(sprintf("%f", $out) , 0, -7); }}$object = new Base62();echo $object->base62_encode(1) . "";echo $object->base62_encode(61) . "";echo $object->base62_decode('z') . ""; ?>
5、PHP实现KMP算法
如果只知道PHP是拍簧片技术,而不懂看毛片算法,就low爆了,
"; if ($par[$j] === $src[$i]) { $j++; $i++; } else { if ($j === 0 && $par[$j] != $src[$i]) { $i++; } $j = $K[$j - 1 >= 0 ? $j - 1 : 0]; } } return false;}//测试数据$src = 'BBC ABCDAB ABCDABCDABDE';$par = 'ABCDABD';// 匹配值echo "匹配值:", implode(" ", KMP($par)), "";// 在给定的字符串中查找特定字符(串)echo KMPMatch($src, $par, true), "";
6、实现中英文字符串翻转的操作
" . mb_strrev($str1) . "";
7、PHP实现相关查找算法
$v) { if ($v == $val) { echo '顺序查找成功,KEY=>' . $k; exit(0); } } echo '顺序查找失败!';}echo query_search(3);//顺序查找成功,KEY=>2/** * 插入查找 * 数组数据需要有序**/$i = 0;function insertsearch($arr, $num){ $count = count($arr); $lower = 0; $high = $count - 1; global $i; while ($lower <= $high) { $i++; //计数器 if ($arr[$lower] == $num) { return $lower; } if ($arr[$high] == $num) { return $high; } $middle = intval($lower + ($num - $arr[$lower]) / ($arr[$high] - $arr[$lower]) * ($high - $lower)); if ($num < $arr[$middle]) { $high = $middle - 1; } else if ($num > $arr[$middle]) { $lower = $middle + 1; } else { return $middle; } } return -1;}$arr = [0, 1, 16, 24, 35, 47, 59, 62, 73, 88, 99];$pos = insertsearch($arr, 62);echo $pos;//7echo $i;//2 如果是折半查找的话i=3;
8、PHP实现相关排序算法
1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 6 [7] => 8 [8] => 9 )
9、PHP实现数独求解问题
clear(); } else { $this->matrix = $arr; } } public function clear() { for ($i = 0; $i < 9; $i++) { for ($j = 0; $j < 9; $j++) { $this->matrix[$i][$j] = []; for ($k = 1; $k <= 9; $k++) { $this->matrix[$i][$j][$k] = $k; } } } } public function setCell($row, $col, $value) { $this->matrix[$row][$col] = [$value => $value]; //row for ($i = 0; $i < 9; $i++) { if ($i != $col) { if (!$this->removeValue($row, $i, $value)) { return false; } } } //col for ($i = 0; $i < 9; $i++) { if ($i != $row) { if (!$this->removeValue($i, $col, $value)) { return false; } } } //square $rs = intval($row / 3) * 3; $cs = intval($col / 3) * 3; for ($i = $rs; $i < $rs + 3; $i++) { for ($j = $cs; $j < $cs + 3; $j++) { if ($i != $row && $j != $col) { if (!$this->removeValue($i, $j, $value)) return false; } } } return true; } public function removeValue($row, $col, $value) { $count = count($this->matrix[$row][$col]); if ($count == 1) { $ret = !isset($this->matrix[$row][$col][$value]); return $ret; } if (isset($this->matrix[$row][$col][$value])) { unset($this->matrix[$row][$col][$value]); if ($count - 1 == 1) { return $this->setCell($row, $col, current($this->matrix[$row][$col])); } } return true; } public function set($arr) { for ($i = 0; $i < 9; $i++) { for ($j = 0; $j < 9; $j++) { if ($arr[$i][$j] > 0) { $this->setCell($i, $j, $arr[$i][$j]); } } } } public function dump() { for ($i = 0; $i < 9; $i++) { for ($j = 0; $j < 9; $j++) { $c = count($this->matrix[$i][$j]); if ($c == 1) { echo " " . current($this->matrix[$i][$j]) . " "; } else { echo "(" . $c . ")"; } } echo ""; } echo ""; } public function dumpAll() { for ($i = 0; $i < 9; $i++) { for ($j = 0; $j < 9; $j++) { echo implode('', $this->matrix[$i][$j]) , "\t"; } echo ""; } echo ""; } public function calc($data) { $this->clear(); $this->set($data); $this->_calc(); $this->dump(); } public function _calc() { for ($i = 0; $i < 9; $i++) { for ($j = 0; $j < 9; $j++) { if (count($this->matrix[$i][$j]) == 1) { continue; } foreach ($this->matrix[$i][$j] as $v) { $flag = false; $t = new Sudoku($this->matrix); if (!$t->setCell($i, $j, $v)) { continue; } if (!$t->_calc()) { continue; } $this->matrix = $t->matrix; return true; } return false; } } return true; }}$sd = new ShuDu();$sd->calc([[0, 5, 0, 0, 0, 6, 0, 9, 0], [0, 4, 7, 0, 8, 2, 6, 0, 0], [0, 8, 0, 0, 0, 7, 0, 5, 2], [7, 0, 1, 0, 3, 4, 0, 0, 6], [0, 3, 0, 0, 2, 0, 0, 8, 0], [2, 0, 0, 0, 0, 1, 9, 0, 4], [4, 7, 0, 1, 0, 0, 0, 6, 0], [0, 0, 9, 4, 6, 0, 3, 7, 0], [0, 1, 0, 2, 0, 0, 0, 4, 0], ]);
10、PHP中的Trait特性
PHP是单继承的语言,在PHP 5.4 Traits出现之前,PHP的类无法同时从两个基类继承属性或方法。php的Traits和Go语言的组合功能类似,通过在类中使用use关键字声明要组合的Trait名称,而具体某个Trait的声明使用trait关键词,Trait不能直接实例化,下面有篇文章比较详细的介绍了此功能,
11、深刻理解PHP中的浅复制和深复制
objB = new ObjB(); } //只有实现了下面方法聚合类 才能实现深复制 /*function __clone() { $this->objB = clone $this->objB; }*/}class ObjB{ public $num2 = 0;}//原型对象$objA = new ObjA();//复制对象(=复制引用)$objA2 = $objA;$objA2->num = 2;//随着$objA2->num的变化 $objA->num也变化了print_r($objA->num . ''); //结果为2print_r($objA2->num . ''); //结果为2//复制对象(‘clone’关键字克隆)$objA3 = clone $objA;$objA3->num = 4;//随着$objA3->num的变化 $objA->num没有变化print_r($objA->num . ''); //结果为2print_r($objA3->num . ''); //结果为4//但是clone的对象(是聚合类)中包含其他对象时所包含的对象(objB)复制的是引用$objA3->objB->num2 = 7;print_r($objA3->objB->num2 . ''); //结果是7print_r($objA->objB->num2 . ''); //结果是7