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. stm32之remap

    1.GPIO的重映射属于AFIO,不同类型不同引脚数单片机情况不一样,比如下图: 2.AFIO 下面还分default/Remap,如果用到了remap就得使用: GPIO_PinRemapConfi ...

  2. iOS:文件操作相关(18-03-23更)

    0.iOS文件系统 1.工程内文件 2.文件夹管理 3.文件操作 4.NSCache 附录: 1.沙盒文件夹.文件大小 2.清除沙盒 Library / Cache 下所有数据 3.测试plist 0 ...

  3. iOS11.2-11.3.1进行越狱及问题

    设备环境:Electra.iOS11.13.1 PS:Electra最新版本进行越狱只支持11.14以下的版本.同时这是不完美越狱,每次重启手机都需要重新越狱,最后,由于Electra版本推出仓促,一 ...

  4. Vue填坑(1)----通过vue-cli,认识vue-router

    开始 首先,确保之前已经安装过 npm 和 nodejs(为了避免版本的问题,最好使用较新的版本). 全局安装 vue-cli : npm install -g vue-cli 新建文件夹 my-pr ...

  5. js如何生成id随机数

    有时候在我们在新增数据时,需要自动生成主键id等,就经常会遇到需要生成随机数的方法. 下面先介绍一种比较简单的生成随机数方法: //产生随机数函数 function RndNum(n){ var rn ...

  6. springboot的junit4模拟request、response对象

    关键字: MockHttpRequest.Mock测试 问题: 在模拟junit的request.response对象时,会报如下空指针异常. 处理方法: 可用MockHttpServletReque ...

  7. CentOS 7.x下升级Python版本到3.x系列(新老版本共存)

    由于python官方已宣布2.x系列即将停止支持,为了向前看,我们升级系统的python版本为3.x系列服务器系统为当前最新的CentOS 7.4 1.安装前查看当前系统下的python版本号 # p ...

  8. php 批量去除项目文件bom头

    <?php if (isset($_GET['dir'])) { //设置文件目录 $basedir = $_GET['dir']; } else { $basedir = '.'; } $au ...

  9. 关于PHPExcel 导出下载表格,调试器响应乱码

    PHPExcel导出表格是日常程序开发很常见的一功能,有些小伙伴千辛万苦把代码写好之后,运行一下结果发现浏览器没反应,表格下载不了或者表格乱码!!!像这种情况有三种解决方法: 1.在header 之前 ...

  10. [POJ3090]Visible Lattice Points(欧拉函数)

    答案为3+2*∑φ(i),(i=2 to n) Code #include <cstdio> int T,n,A[1010]; void Init(){ for(int i=2;i< ...