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. Android平台上PMEM的使用及Platform设备注册(一)

    Android中PMEM驱动程序是物理内存的驱动程序,可用于分配物理内存.PMEM在camera和video系统中频繁使用.下面,简单记录一下PMEM的使用方法.另外,由于PMEM设备做为Platfo ...

  2. java文件系统中的的NIO与IO

    java从jdk1.4后就引入了java NIO机制: NIO的显著特点就是通道(channel).缓冲(buffer).选择器(selector),NIO机制中添加了传统I/O机制中没有的非阻塞调用 ...

  3. Canvas制作的下雨动画

    简介 在codepen上看到一个Canvas做的下雨效果动画,感觉蛮有意思的.就研究了下,这里来分享下,实现技巧.效果可以见下面的链接. 霓虹雨: http://codepen.io/natewile ...

  4. Centos7单网卡带VLAN多IP配置

    1.需要使用到vconfig软件,首先yum安装vconfig: 使用指令yum install vconfig:(若是本机找不到vconfig安装包,可以通过其他centos7安装yum-utils ...

  5. duplicate symbols for architecture arm64 导入的类库字符重复

    这个错误大部分时候是引用库重复定义的问题. 项目需要,同时引用ZBar和QQ授权登录SDK,由于二者均使用了Base64处理数据,XCode编译时报错: duplicate symbol _base6 ...

  6. 【Django笔记四】Django2.0中的表单

    一.环境版本信息: 操作系统:windows10 Django版本:2.0.5 Python版本:3.6.4 Mysql版本: 5.5.53   安装mysql 二.基础信息 1.App中的模型mod ...

  7. mysql集群压测

    mysql压测 mysql自带就有一个叫mysqlslap的压力测试工具,通过模拟多个并发客户端访问MySQL来执行压力测试,并且能很好的对比多个存储引擎在相同环境下的并发压力性能差别.通过mysql ...

  8. springboot(2.0以上) --数据源切换时报错

    在进行数据源切换时spring.datasource.type类型根据源码所给的默认值修改后依然报错 先看源码:标色部分 ,  就是springboot所给的数据源 ,  正常来说只要在配置文件中修改 ...

  9. MySQL数据库安装配置步骤详解

    MYSQL的安装 1.打开下载的mysql安装文件mysql-5.5.27-win32.zip,双击解压缩,运行“setup.exe”. 2.选择安装类型,有“Typical(默认)”.“Comple ...

  10. Django的MVT的思路

    1.先上两张图片 2.我的理解 view在MVT框架里面,起到的是中间调度的作用. a.在diango里面有个关键性路径的配置 就是在django2.0前的url和在2.0后的path. 为避免一个项 ...