题目描述:

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. C# 删除文件以及文件夹

    代码如下: /// <summary> /// 删除文件夹以及文件 /// </summary> /// <param name="directoryPath& ...

  2. 解题报告:poj1083 Moving tables

    2017-09-02 19:49:59 writer:pprp 题意说明: 比较简单的题,一开始被吓到了,后来才发现,其实可以用很简单的方法就可以解决: 就是在这样的房间中如果在i 和 j 中之后的1 ...

  3. codeforces 484B - LubaAndTicket - 贪心

    2017-08-22 10:54:00 writer:pprp 题意如下: 给你6个数组,你的操作可以是更改某一位的数字成为0-9之间任意一个数,要求前三个数字的和与后三个数字的和相等. 问你最少用几 ...

  4. Struts2框架学习第二章——Struts2下的HelloWorld

    本章要点 —  Struts 2的下载和安装 — 纯手工创建一个Web应用 — 纯手工创建一个Struts 2应用 — 实现Struts 2的Action — 配置Struts 2的Action — ...

  5. NSMutableString和NSString区别,及相互转换方法

    NSString是一个不可变的字符串对象.这不是表示这个对象声明的变量的值不可变,而是表示它初始化以后,你不能改变该变量所分配的内存中的值,但你可以重新分配该变量所处的内存空间.而NSMutableS ...

  6. ASP.NET 4.5 MVC 4 无法运行在Windows2008的IIS7.0上显示404的解决方案

    需要在web.config下加上这个 <system.webServer> <modules runAllManagedModulesForAllRequests="tru ...

  7. 解决docker中使用nginx做负载均衡时并发过高时的一些问题

    # 解决docker中使用nginx做负载均衡时并发过高时的一些问题 1.问题产生原因: 由于通过nginx作为负载均衡服务,在访问并发数量达到一定量级时jmeter报错. nginx日志关键信息:a ...

  8. 牛客-https://www.nowcoder.com/acm/contest/96/H

    链接:https://www.nowcoder.com/acm/contest/96/H来源:牛客网 题目描述 今天qwb要参加一个数学考试,这套试卷一共有n道题,每道题qwb能获得的分数为ai,qw ...

  9. 原生javascript-Tab选项卡-面向对象

    分析个人用原生JS获取类名元素的代码: getByClassName:function(className,parent){ var elem = [], node = parent != undef ...

  10. linux-shutdown命令说明

    showdown命令: -k  不是真正关闭电脑,只是警告. -h 关闭后暂停 -r 关闭后重新引导 -c 取消已经运行的关闭操作 -n 不通过init直接关闭 -f 快速重新引导 time 关闭的时 ...