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. 前端性能优化 http请求的过程及潜在的优化点

    CS架构:比如我们的代码开发好,打包成apk,发布到平台,那么最终怎么运行到用户的手机上呢,用户首先需要从相关的应用商城下载这个apk包,并且运行这个 apk 包,那么这个 apk 包就会被解压,最后 ...

  2. debug模式不报错,release模式报错

    经常会 char * pMem = new char[icount]; 其中icount为变量,然后对该内存段猛的操作.release编译出来,出现莫名奇妙的错误.但是debug没问题. 后面查了别人 ...

  3. docker 搭建自己的仓库

    1.下载registry镜像 docker pull registry 2.查看端口信息 netstat -ntlp 3.启动registry镜像 docker run -d -p 5000:5000 ...

  4. offsetWidth的bug

    #div1{width:200px;border:1px solid red;} 这个时候如果用 offsetWidth 提取 #div1 的宽  得到的值是 202: 也就是说 offsetWidt ...

  5. Django 购物车模板

    url from django.contrib import admin from django.urls import path, re_path from django.urls import i ...

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

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

  7. Eye sketch - ES

      An interesting painting program, the interface is a blank drawing board, touch the bottom of the r ...

  8. manjaro 安装 tim 后无法输入中文

    cd /opt/deepinwine/tools sudo chmod 777 run.sh vim run.sh 在一开始的注释下输入 export GTK_IM_MODULE="fcit ...

  9. Qt常用类——QWidget

    QWidget类是所有用户界面对象的基类. Widget是用户界面的基本单元:它从窗口系统接收鼠标,键盘和其他事件,并在屏幕上绘制自己. 每个Widget都是矩形的,它们按照Z-order进行排序.

  10. failed to execute /bin/bash: Resource temporarily unavailable的问题处理

    [admin@localhost ~]$ sudo su - scloanLast login: Tue Jun 12 14:06:31 CST 2018 on pts/3su: failed to ...