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. Python在Linux环境中安装Thrift

    1.文件下载:thrift-0.11.0.tar.gz 个人网盘下载:链接:https://pan.baidu.com/s/1MXgx8LuN4wk7ssVUD9Wzaw  提取码:xw85  2. ...

  2. centos 7.3 安装 mysqldb 报错 EnvironmentError: mysql_config not found ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

    ERROR: Command errored : command: /usr/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] ...

  3. 201871010121 王方 《面向对象程序设计(java)》 第二周学习总结

    这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p/11475 ...

  4. SpringBoot开发mockserver及生成swagger接口文档

    通过springboot开发mock server,包含get及post接口,用于练习接口自动化及jmeter很方便 当然,也为后面jenkins持续集成做基础(开发push代码后  → jenkin ...

  5. hive使用beeline配置远程连接

    hive以hadoop集群为基础,提供hdfs的SQL支持: hive一般可以以mysql为元数据存储,默认Derby:hadoop,hive安装自行百度吧: 介绍hive的远程访问: 未配置之前使用 ...

  6. OpenCV 学习笔记(16)open创建无边框的显示窗口

    https://blog.csdn.net/weixin_41794771/article/details/93198098 讲解地址 // 1获取窗口句柄 winName 窗口名字 HWND win ...

  7. piral 基于typescript 的微前端开发框架

    piral有一个微前端开发框架,功能强大,文档比较全,扩展能力也比较好 包含以下特性: 特性 高度模块化 多框架兼容 支持资源文件的拆分 全局状态管理 独立开发和部署 CLI工具 与同类框架的比较 参 ...

  8. 数据库: 安装配置数据库,使用Navicat for MySQL和手机APP 连接测试(如果上一节碰到问题可参考这一节)

       咱就安装上       还有这个                    最终测试请参考上一节 启动MySQL服务主要有以下两种方法: 第一种: 在搜索框中输入“services,msc” ,en ...

  9. css3中的box-sizing常用的属性有哪些?分别有什么作用?

    content-box:默认标准盒模型,总宽=width+padding+border+margin border-box:IE标准,怪异盒模型,总宽=width+margin inherit:从父元 ...

  10. 【转】30种MySQL索引优化的方法

    第一方面:30种mysql优化sql语句查询的方法       1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by涉及的列上建立索引. 2.应尽量避免在 where ...