Description

Find the first unique character in a given string. You can assume that there is at least one unique character in the string.

Example

For "abaccdeff", return 'b'.

解题:返回仅出现一次,并且排在最前面的那个元素,而不是返回第一个不同的元素,一开始理解错了。如果仅仅对于ASCII中的256个字符,可以用数组来保存每个元素出现的次数。而对于Unicode中的元素,最好用HashMap来做。代码如下:

  1. public class Solution {
  2. /**
  3. * @param str: str: the given string
  4. * @return: char: the first unique character in a given string
  5. */
  6. public char firstUniqChar(String str) {
  7. // Write your code here
  8. HashMap<Character, Integer>map = new HashMap<Character, Integer>();
  9. for(int i = 0; i < str.length(); i++){
  10. char temp = str.charAt(i);
  11. if(map.containsKey(temp)){
  12. map.put(temp, map.get(temp) + 1);
  13. }else{
  14. map.put(temp, 1);
  15. }
  16. }
  17. for(int i = 0; i < str.length(); i++)
  18. if(map.get(str.charAt(i)) == 1)
  19. return str.charAt(i);
  20.  
  21. return str.charAt(0);
  22.  
  23. }
  24. }

209. First Unique Character in a String的更多相关文章

  1. LeetCode_387. First Unique Character in a String

    387. First Unique Character in a String Easy Given a string, find the first non-repeating character ...

  2. Leetcode算法比赛----First Unique Character in a String

    问题描述 Given a string, find the first non-repeating character in it and return it's index. If it doesn ...

  3. LeetCode 387. First Unique Character in a String (字符串中的第一个唯一字符)

    题目标签:String, HashMap 题目给了我们一个 string,让我们找出 第一个 唯一的 char. 设立一个 hashmap,把 char 当作 key,char 的index 当作va ...

  4. [LeetCode] First Unique Character in a String 字符串第一个不同字符

    Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...

  5. 387. First Unique Character in a String

    Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...

  6. LeetCode 387. First Unique Character in a String

    Problem: Given a string, find the first non-repeating character in it and return it's index. If it d ...

  7. LeetCode First Unique Character in a String

    原题链接在这里:https://leetcode.com/problems/first-unique-character-in-a-string/ 题目: Given a string, find t ...

  8. leetcode修炼之路——387. First Unique Character in a String

    最近公司搬家了,有两天没写了,今天闲下来了,继续开始算法之路. leetcode的题目如下: Given a string, find the first non-repeating characte ...

  9. 18. leetcode 387. First Unique Character in a String

    Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...

随机推荐

  1. ListView 中嵌套 GridView

    1.主布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andr ...

  2. 【题解】洛谷P2679 [NOIP2015TG] 子串(DP+滚动数组)

    次元传送门:洛谷P2679 思路 蒟蒻一开始并没有思路而去看了题解 我们发现对于两个字串的位置 我们只需要管他们匹配成功或者匹配失败即可 f[i][j][k] 记录当前 a[i]不论等不等于b[j] ...

  3. mysql千万级数据库插入速度和读取速度的调整

    mysql上百万数据读取和插入更新一般没什么问题,但上千万后速度会很慢,如何调整配置,提高效率.如下: 1.尽量将数据一次性写入DataFile和减少数据库的checkpoint操作,调整如下参数: ...

  4. iPhone 横竖屏切换,全屏播放的三种方式

    1. 调用系统自带的强制屏幕旋转不过还得在AppDelegate中重写下面方法 - (UIInterfaceOrientationMask)application:(UIApplication *)a ...

  5. List、LinkedList、ArrayList、Vector

    目前好像写不出比较高质量的随笔,除了多看看别人写的,就是多写,做好自己可以做的 最近听的和看到的最多的一个词就是“勿忘初心”.的确不少人到了一定高度之后,就不知道该怎么做了.(包括我曾经的偶像娜姐,在 ...

  6. Filter(过滤器)与Interceptor(拦截器)的区别

    Filter能够对请求和响应资源进行拦截: Interceptor只针对请求进行拦截 在 Struts2中: (1)拦截器是基于java反射机制的,而过滤器是基于函数回调的. (2)过滤器依赖与ser ...

  7. drawImage画本地资源,在真机无法显示

    把图片的路径改成本地的绝对路径

  8. ruby中url解码并替换非法字符

    url中中文字符解码 str = URI.decode(url_str) 替换非法字符 if ! str.valid_encoding? p str = str.encode("UTF-16 ...

  9. 《PHP实用问题解决案例》系列分享专栏

    <PHP实用问题解决案例>已整理成PDF文档,点击可直接下载至本地查阅https://www.webfalse.com/read/201725.html 文章 PHP汉字拼音转换和公历农历 ...

  10. AtCoder Regular Contest 098 F.Donation

    传送门 首先,对于一个点i,进入这个点前必须大于等于Ai,每个点必须捐赠Bi 那么我们可以在每个点最后一次经过的时候再捐赠,这样显然更优 现在我们假设每个点都是最后一次经过的时候捐赠.现在我们把捐赠的 ...