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来做。代码如下:

 public class Solution {
/**
* @param str: str: the given string
* @return: char: the first unique character in a given string
*/
public char firstUniqChar(String str) {
// Write your code here
HashMap<Character, Integer>map = new HashMap<Character, Integer>();
for(int i = 0; i < str.length(); i++){
char temp = str.charAt(i);
if(map.containsKey(temp)){
map.put(temp, map.get(temp) + 1);
}else{
map.put(temp, 1);
}
}
for(int i = 0; i < str.length(); i++)
if(map.get(str.charAt(i)) == 1)
return str.charAt(i); return str.charAt(0); }
}

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. 【洛谷P2279】[HNOI2003]消防局的设立

    消防局的设立 题目链接 贪心:每次取出深度最大的节点,若没有被覆盖到,要想覆盖它, 最优的做法显然是将它的爷爷设为消防局 (因为该节点深度为最大,选兄弟.父亲所覆盖的节点,选了爷爷后都能够覆盖) 用优 ...

  2. Xcode菜单功能中文翻译

    Xcode菜单功能中文翻译 File  文件 Edit  编辑 View 视图 Navigate 导航 Editor 编辑 Product 产品 Window  窗口 Help 帮助 File  文件 ...

  3. 工具 | Axure基础操作 No.2

    不废话了,直接如之前一样上操作图才是正道. 1.设置文本类型为密码或者文件类型 可以在属性中也选择最大长度制定长度. 如果设置类型为文件,在浏览器中就会自动变成选择本地文件的按钮. 2.文本框提示文字 ...

  4. Linux Mysql 卸载

    Linux下mysql的卸载: 1.查找以前是否装有mysql 命令:rpm -qa|grep -i mysql 可以看到mysql的两个包: mysql-4.1.12-3.RHEL4.1 mysql ...

  5. js实现点击按钮可实现编辑

    <script type="text/javascript">//修改密码//抓取到的数据 function edit() { document.getElementB ...

  6. IDEA中使用单元测试@Test等,提示没有 Junit.jar包

    1.File-->Project Structure-->Modules-->右侧Dependencies-->+号-->JARs or directories... 2 ...

  7. PHP curl 携带cookie请求抓取源码,模拟登陆。

    公司需要采集一批手机号码,有指定网站.但是需要登陆后才能看到客户号码,手动点击复制太慢,如此就写了以下模拟登陆采集号码程序,分享给大家参考参考. function request_url_data($ ...

  8. 树莓派3B+学习笔记:2、更改显示分辨率

    1.打开终端,输入 sudo raspi-config 选择第7行: 2.选择第5行: 3.选择一个自己习惯的分辨率(我选择1024X768),确定后重启,VNC会自动连接: 4.更改分辨率完成,方便 ...

  9. VIM 键

    输入 vimtutor命令,可以打开Linux使用手册(基本使用). ***. 插入键: A: 行尾插入 a: 字符后面插入 i: 字符前面插入 I: 行首插入 r:只替换一次字符  R:一直替换,直 ...

  10. fedora/centos下gcc编译出现gcc: error trying to exec ‘cc1plus’: execvp: No such file or directory

    fedora/centos下gcc编译出现gcc: error trying to exec 'cc1plus': execvp: No such file or directory解决办法 翻译自: ...