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. 人生物语——哲海拾贝

         如今的这个社会,物欲横流.纸醉金迷.浮躁不安是这个时代的主旋律,在这样一个浮华年代的大染缸里,每个人内心都有那么一颗浮躁不安分的种子,或许它才开始发芽,或许它已经占据了你的心灵,人生当中追求 ...

  2. C# Net 合并int集合为字符串,如:输入1,2,3,4,8 输出1~4,8

    C# Net 合并int集合为字符串,如:输入1,2,3,4,8 输出1~4,8 粘贴代码使用: /// <summary> /// 合并int集合,如1,2,3,4,8 输出1~4,8 ...

  3. (原)pytorch中使用TensorRT

    转载请注明出处: https://www.cnblogs.com/darkknightzh/p/11332155.html 代码网址: https://github.com/darkknightzh/ ...

  4. 一文搞定PGA_LGA_BGA

    概述 什么是LGA.PGA.BGA类型的封装?众所周知,CPU封装的类型主要为三种:LGA,PGA,BGA,其中LGA封装是最常见的,intel处理器都是采用这种类型的封装,而PGA封装则是AMD常用 ...

  5. 第一部分day03-元组、字典、字符串

    -----元组-----元组查询 a = (1,2,3,4) print(a[1:2]) #(2,) 购物车练习(列表方法练习) product_list=[ ['Mac',9000], ['kind ...

  6. https工具类

    import org.apache.commons.lang.StringUtils; import javax.net.ssl.*; import java.io.*; import java.ne ...

  7. JS里==和===区别

    面试常问的一个基础问题 == 和 ===区别是什么? 这里简单描述下 "==="叫做严格运算符,"=="叫做相等运算符 而且 == 会对数据做隐式转换,而=== ...

  8. Python学习笔记8 pip换源

    换pip源 安装好 python 后,有一个问题就是 pip 安装的默认下载地址在国外.要换一下,不然蜗牛速度,怎么追赶世界?(瞎扯一下,活跃气氛~) Ubuntu下,进入终端(ctrl + alt ...

  9. 第二阶段冲刺(个人)——two

    今天的计划: 测试登录功能并优化. 昨天做了什么呢?  修改登录界面. 遇到的困难:一些标签运用不好,过程进度慢,改了又改.

  10. 阿里巴巴java开发手册 注释规约