2023-01-13 314
问题
请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符”go”时,第一个只出现一次的字符是”g”。当从该字符流中读出前六个字符“google”时,第一个只出现一次的字符是”l”。
输出描述:
如果当前字符流没有存在出现一次的字符,返回#字符
题解
使用索引数组
实现代码
<?php
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获取字符流中第一个不重复字符的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!
原文链接:https://77isp.com/post/25283.html
=========================================
https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。
数据库技术 2022-03-28
网站技术 2022-11-26
网站技术 2023-01-07
网站技术 2022-11-17
Windows相关 2022-02-23
网站技术 2023-01-14
Windows相关 2022-02-16
Windows相关 2022-02-16
Linux相关 2022-02-27
数据库技术 2022-02-20
抠敌 2023年10月23日
嚼餐 2023年10月23日
男忌 2023年10月22日
瓮仆 2023年10月22日
簿偌 2023年10月22日
扫码二维码
获取最新动态