Difficulty:easy

 More:【目录】LeetCode Java实现

Description

https://leetcode.com/problems/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 exist, return -1.

Examples:

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

Note: You may assume the string contain only lowercase letters.

Intuition

Use HashMap( or int[256] ) to store the frequency of each character.

Solution

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

  

Complexity

Time complexity : O(n)

Space complexity : O(1)

 More:【目录】LeetCode Java实现

【LeetCode】387. First Unique Character in a String的更多相关文章

  1. 【LeetCode】387. First Unique Character in a String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. 【leetcode❤python】387. First Unique Character in a String

    #-*- coding: UTF-8 -*- class Solution(object):    def firstUniqChar(self, s):        s=s.lower()     ...

  3. LeetCode之387. First Unique Character in a String

    -------------------------------------------------- 最开始的想法是统计每个字符的出现次数和位置,如下: AC代码: public class Solu ...

  4. LeetCode算法题-First Unique Character in a String(Java实现)

    这是悦乐书的第213次更新,第226篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第81题(顺位题号是387).给定一个字符串,找到它中的第一个非重复字符并返回它的索引. ...

  5. 【LeetCode】1047. Remove All Adjacent Duplicates In String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...

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

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

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

  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. 【学习笔记】大数据技术原理与应用(MOOC视频、厦门大学林子雨)

    1 大数据概述 大数据特性:4v volume velocity variety value 即大量化.快速化.多样化.价值密度低 数据量大:大数据摩尔定律 快速化:从数据的生成到消耗,时间窗口小,可 ...

  2. 内核中dump_stack的实现原理(3) —— 内核函数printk的实现

      参考内核文档: Documentation/printk-formats.txt   在内核中使用dump_stack的时候可以看到如下用法: static inline void print_i ...

  3. LAMP架构性能测试+php优化

     性能测试:1. 首先查看一下服务器的硬件性能free  -m  ---->查看一下内存的情况lscpu或者cat  /proc/cpuinfo     ------à查看一下cpu的情况2.  ...

  4. 【JavaScript】JS知识点总结

    JavaScript知识点总结: javascript简单介绍ECMAScript1.语法2.变量:只能使用var定义,如果在函数的内容使用var定义,那么它是一个局部变量,如果没有使用var它是一个 ...

  5. AXIOS 的请求

    AXIOS 本质上等同于json 传值 1.引用 //引入axios import Axios from 'axios' //将axios挂载到 Vue原型上 Vue.prototype.$https ...

  6. 浅析 fstab 与移动硬盘挂载方法

    本文转自 Steins;Lab,非常详细地介绍了树莓派上 fstab 的配置项. 近期自己的Raspberry Pi出了点问题,总结总结便有了这篇文章. 本文首先记录“移动硬盘挂载”实际发生的问题,然 ...

  7. 使用log4Net输出调试信息

    在上一篇搭建服务器端的项目基础上,使用log4Net进行调试信息输出 http://www.cnblogs.com/fzxiaoyi/p/8439769.html 1.先分析下Photo 自带的服务器 ...

  8. NOIP 2013 积木大赛

    洛谷 P1969 积木大赛 洛谷传送门 JDOJ 2229: [NOIP2013]积木大赛 D2 T1 JDOJ传送门 题目描述 春春幼儿园举办了一年一度的"积木大赛".今年比赛的 ...

  9. 产品上线后,出现BUG的处理流程

    根据bug的大小,如果影响业务逻辑及用户提醒及时处理,如果只是一些状态.文案等等对业务无重大影响可以跟版本迭代走 很严重的bug必然要回滚,想都不要想赶紧去着手安排做. 检查回滚版本是否会丢失数据,如 ...

  10. Taro,实现小程序在样式文件中导入背景图片

    https://taro-docs.jd.com/taro/docs/static-reference.html 注意点是,控制你的图片大小,然后配置完limit后,把dist删掉,重新运行 npm ...