博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
How to find First Non-Repeated Character from String
阅读量:4598 次
发布时间:2019-06-09

本文共 1291 字,大约阅读时间需要 4 分钟。

You need to write a function, which will accept a String and return first non-repeated character, for example in the world "hello", except 'l' all are non-repeated, but 'h' is the first non-repeated character. Similarly, in word "swiss" 'w' is the first non-repeated character. One way to solve this problem is creating a table to store count of each character, and then picking the first entry which is not repeated. The key thing to remember is order, your code must return first non-repeated letter.

1 public class solution { 2     public char firstNonRepeating(String s) { 3         char result = '#'; 4         if (s == null || s.length == 0) { 5             return result; 6         } 7         Map
map = HashMap<>(); 8 for (int i = 0; i < s.length(); i++) { 9 if (map.containsKey(s.charAt(i))) {10 map.put(s.charAt(i), map.get(s.charAt(i)) + 1);11 } else {12 map.put(s.charAt(i), 1);13 }14 }15 16 for (int i = 0; i < s.length; i++) {17 if (map.get(s.charAt(i) == 1)) {18 result = s.charAt(i); 19 return result;20 }21 }22 return result; 23 }24 }

 

转载于:https://www.cnblogs.com/FLAGyuri/p/5796263.html

你可能感兴趣的文章
oracle中的decode的使用
查看>>
PHP生成中文验证码并检测对错实例
查看>>
数据库经典练习题整理
查看>>
android与javaee通信:登录界面超级简化版
查看>>
Nhibernate3.3.3sp1基础搭建测试
查看>>
Python之模块与包
查看>>
C++中获取文件大小的几种途径汇总~
查看>>
JavaScript原始基础
查看>>
JDBC_基础6步骤- 及优化
查看>>
WCM重启报数据库启动错误
查看>>
totoise svn误将桌面作为checkout路径,界面一堆?
查看>>
java写"\n"写入到txt文本用记事本打开出现黑框解决方案
查看>>
第三章例3-7
查看>>
心得五
查看>>
react antD moment
查看>>
MySql创建指定字符集的数据库
查看>>
bzoj 3172 AC自动机
查看>>
rabbitmq
查看>>
解决Latex中Itemize距离过大的问题
查看>>
1打印沙漏
查看>>