字符流中第一个不重复的字符
来源:互联网 发布:chocobank网络剧照片 编辑:程序博客网 时间:2024/06/07 08:50
题目描述
请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符”go”时,第一个只出现一次的字符是”g”。当从该字符流中读出前六个字符“google”时,第一个只出现一次的字符是”l”。
输出描述
如果当前字符流没有存在出现一次的字符,返回#字符。
解题思路
1.对每一个插入的字符,统计其出现的次数,不妨使用整型的count[char]表示;
2.按照插入的顺序,读出第一个出现次数为1的字符。
代码如下:
package firstAppearingOnce;public class Solution { String str = ""; int[] count = new int[256]; public static void main(String[] args) { Solution s = new Solution(); s.Insert('g'); s.Insert('o'); s.Insert('o'); s.Insert('g'); s.Insert('l'); s.Insert('e'); System.out.println(s.FirstAppearingOnce()); } //Insert one char from stringstream public void Insert(char ch) { str+=ch; count[ch]++; } //return the first appearence once char in current stringstream public char FirstAppearingOnce() { for(int i = 0; i<str.length(); i++){ char ch = str.charAt(i); if(count[ch] == 1) return ch; } return '#'; }}
运行结果:
l
注:来自剑指offer刷提整理,代码可直接运行。
阅读全文
0 0
- 字符流中第一个不重复的字符
- 55 - 字符流中第一个不重复的字符
- 字符流中第一个不重复的字符
- 字符流中第一个不重复的字符
- 字符流中第一个不重复的字符
- 字符流中第一个不重复的字符
- 字符流中第一个不重复的字符
- 字符流中第一个不重复的字符
- 【算法】字符流中第一个不重复的字符
- 字符流中第一个不重复的字符
- 字符流中第一个不重复的字符
- 字符流中第一个不重复的字符 java
- 字符流中第一个不重复的字符
- 字符流中第一个不重复的字符
- 字符串------字符流中第一个不重复的字符
- 【53】字符流中第一个不重复的字符
- 字符流中第一个不重复的字符
- Q55:字符流中第一个不重复的字符
- Subtree of Another Tree
- HDU-1079-Calendar Game 【奇偶】
- 在笔试当中遇到的php输入问题
- 第十篇:策略模式:网络小说的固定套路
- 页面显示This is the initial start page for the WebDriver server.的解决办法
- 字符流中第一个不重复的字符
- JavaScript.ajax获得后台的返回值
- 2017 ccpc 网络赛 Friend-Graph
- 开闭原则
- imutils with Canny Edge Detection
- 训练总结 8.19
- 动态导航栏
- [笔记分享] [GPIO] MSM8x39 GPIO 硬件部分小结
- POJ