题目描述:

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. quickSort - 编程细节

    quicksort 快速排序 有几个细节比较重要 今天听到一个清华的大佬的话,他曾经是NOI, 在大一就得到了我梦寐以求的ACM金奖,他这样说, 他们在打NOI的时候,每天要求做10道题, 连续做60 ...

  2. 解题报告:hdu1003 Max Sum - 最大连续区间和 - 计算开头和结尾

    2017-09-06 21:32:22 writer:pprp 可以作为一个模板 /* @theme: hdu1003 Max Sum @writer:pprp @end:21:26 @declare ...

  3. Python学习札记(二十) 函数式编程1 介绍 高阶函数介绍

    参考: 函数式编程 高阶函数 Note A.函数式编程(Functional Programming)介绍 1.函数是Python内建支持的一种封装,我们通过一层一层的函数调用把复杂任务分解成简单的任 ...

  4. RabbitMQ入门(6)——远程过程调用(RPC)

    在RabbitMQ入门(2)--工作队列中,我们学习了如何使用工作队列处理在多个工作者之间分配耗时任务.如果我们需要运行远程主机上的某个方法并等待结果怎么办呢?这种模式就是常说的远程过程调用(Remo ...

  5. unity屏幕坐标转世界坐标结果为(0,0,0)

    代码: wv转出来一直为(0,0,0),卡了好久,问别人说要转化的屏幕坐标Z不能为0 阿西吧!特此记录

  6. Spring Boot总结

    一.Spring Boot 入门 1.Spring Boot 简介 简化Spring应用开发的一个框架: 整个Spring技术栈的一个大整合: J2EE开发的一站式解决方案: 2.微服务 2014,m ...

  7. 第九天 1-8 实战:安装nginx服务器

    简介:使用nginx源码包,安装nginx服务器,并对其进行配置后,测试其成果.模仿百度主页! 第一步:检查依赖关系[root@xiaogan ~]# rpm -q zlib-devel pcre-d ...

  8. HDU 2492 BIT/逆序数/排列组合

    Ping pong Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  9. Ubuntu 下Python 环境问题

    问题描述: 原先使用Anaconda环境,若卸载后仍不能恢复到系统默认的Python环境. 解决方案: shell 寻找缓存路径,python的扩展/home/tom/anaconda/bin/pyt ...

  10. vue组件间传值

    父传子 1.父组件:在子组件容器内绑定数据 <router-view :unusedOrderNum="num1" :usedOrderNum="num2" ...