本篇讲解了php怎样获取字符流中第一个不重复字符。
请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符”go”时,第一个只出现一次的字符是”g”。当从该字符流中读出前六个字符“google”时,第一个只出现一次的字符是”l”。
输出描述:
如果当前字符流没有存在出现一次的字符,返回#字符
题解
使用索引数组
实现代码
global $result;
//init module if you need
function init(){
global $result;
$result = [];
}
//insert one char from stringstream
function insert($ch)
{
global $result;
// write code here
if(isset($result[$ch])){
$result[$ch]++;
}else{
$result[$ch] =1;
}
}
//return the first appearence once char in current stringstream
function firstappearingonce()
{
global $result;
foreach($result as $k =>$v){
if($v ==1){
return $k;
}
}
return "#";
}
本篇讲解了php怎样获取字符流中第一个不重复字符,更多相关内容请关注。
相关推荐:
php使用一行代码删除目录下所有文件方法
php实现顺时针打印矩阵及螺旋矩阵的方法
php判断二叉树是否对称的方法
以上就是php怎样获取字符流中第一个不重复字符的详细内容。
