题目描述:

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

Examples:

s = "leetcode"
return 0. s = "loveleetcode",
return 2.

解题思路:

开个26个数的数组,然后先对字符串过一遍,统计每个字母出现的次数,然后从头再国一遍,第一个字母数为1的即为首先出现并且只出现一次的字母。

代码如下:

public class Solution {
public int firstUniqChar(String s) {
int[] a = new int[26];
for(int i = 0; i < s.length(); i++)
a[s.charAt(i) - 'a']++;
for(int i = 0; i < s.length(); i++){
if(a[s.charAt(i) - 'a'] == 1)
return i;
}
return -1;
}
}

  

Java [Leetcode 387]First Unique Character in a String的更多相关文章

  1. [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 ...

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

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

  3. 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 ...

  4. 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 ...

  5. [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 ...

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

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

  7. 【LeetCode】387. First Unique Character in a String

    Difficulty:easy  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/first-unique-cha ...

  8. [LeetCode&Python] Problem 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 ...

  9. *387. First Unique Character in a String (linkedhashmap + string) debug manunally instead of using leetcode

    The ability to debug and overall thinking need to improve Given a string, find the first non-repeati ...

随机推荐

  1. GDPR或使全球域名whois信息被隐藏

    什么是GDPR? GDPR 全称为 General Data Protection Regulation,是欧盟 2016 年 4 月通过的一项通用数据保护条例(或称“一般数据保护法案”),是 199 ...

  2. Linux 本地调试Hadoop

    将Hadoop部署后,可以使用java api进行访问,但是并不能像安装完mysql后用python自带的mysql api连接那么简单. Hadoop/share目录下有Hadoop所有的jar包, ...

  3. 日期插件My97DatePicker

    因为项目中需要选中日期,所以就找到了My97DatePicker这个插件,用法非常的简单,但是因为各个公司的要求不同,我们公司使用js拼代码,然后渲染到页面上的,所以遇到了一点问题… 1.My97Da ...

  4. js事件在不同浏览器之间的差异

    目录: 1. 介绍 2. 不同浏览器之间的差异 2.1 添加事件的方法 2.2 事件对象event 2.3 event中的属性/方法 3. 总结 1. 介绍 javascript与HTML之间的交互是 ...

  5. windows系统下,安装多个版本的jdk,java -version

    问题描述: 开始安装了 jdk8 后来装了jdk9,可以为项目配置不同的jdk,相安无事: 今天发现软件需要jdk8的环境,结果我的java -version始终是jdk9.0.1: 解决办法:使ja ...

  6. fwrite的文件缓冲同步到磁盘

    这是个小细节. 用fwrite写文件的时候,我发现刷新文件夹,对应文件大小一直是0. 网上有一篇博客写得比较完善http://blog.csdn.net/sctq8888/article/detail ...

  7. HDU 5687 字典树入门

    Problem C Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total ...

  8. Linux用root强制踢掉已登录用户;用fail2ban阻止ssh暴力破解root密码

    Linux用root强制踢掉已登录用户   首先使用w命令查看所有在线用户: [root@VM_152_184_centos /]# w 20:50:14 up 9 days, 5:58, 3 use ...

  9. 编写3ds max插件时遇到的问题总结

    本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/ 这几天在给公司的美术编写3ds max 2009使用的插件,遇到了一些问题,在此记录一下解 ...

  10. 【51nod-1521】一维战舰

    爱丽丝和鲍博喜欢玩一维战舰的游戏.他们在一行有n个方格的纸上玩这个游戏(也就是1×n的表格). 在游戏开始的时候,爱丽丝放k个战舰在这个表格中,并不把具体位置告诉鲍博.每一只战舰的形状是 1×a 的长 ...