【LeetCode】387. First Unique Character in a String
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的更多相关文章
- 【LeetCode】387. First Unique Character in a String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode❤python】387. First Unique Character in a String
#-*- coding: UTF-8 -*- class Solution(object): def firstUniqChar(self, s): s=s.lower() ...
- LeetCode之387. First Unique Character in a String
-------------------------------------------------- 最开始的想法是统计每个字符的出现次数和位置,如下: AC代码: public class Solu ...
- LeetCode算法题-First Unique Character in a String(Java实现)
这是悦乐书的第213次更新,第226篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第81题(顺位题号是387).给定一个字符串,找到它中的第一个非重复字符并返回它的索引. ...
- 【LeetCode】1047. Remove All Adjacent Duplicates In String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...
- LeetCode 387. First Unique Character in a String (字符串中的第一个唯一字符)
题目标签:String, HashMap 题目给了我们一个 string,让我们找出 第一个 唯一的 char. 设立一个 hashmap,把 char 当作 key,char 的index 当作va ...
- 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 ...
- leetcode修炼之路——387. First Unique Character in a String
最近公司搬家了,有两天没写了,今天闲下来了,继续开始算法之路. leetcode的题目如下: Given a string, find the first non-repeating characte ...
- 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 ...
随机推荐
- 在小程序中实现 Mixins 方案
摘要: 小程序开发技巧 作者:jrainlau 原文:在小程序中实现 Mixins 方案 Fundebug经授权转载,版权归原作者所有. 在原生开发小程序的过程中,发现有多个页面都使用了几乎完全一样的 ...
- 堆叠降噪自编码器SDAE
https://blog.csdn.net/satlihui/article/details/81006906 https://blog.csdn.net/github_39611196/articl ...
- Flutter gradle采坑
前些日子google推出Flutter1.9版本支持web果断升级 在运行flutter时发现错误,错误提示为 Launching lib/main.dart on Android SDK built ...
- vmware中桥接模式,NAT模式,主机模式的区别
桥接模式 在桥接模式下,VMWare虚拟出来的操作系统就像是局域网中的一台独立的主机(主机和虚拟机处于对等地 位),它可以访问网内任何一台机器.在桥接模式下,我们往往需要为虚拟主机配置IP地址.子网掩 ...
- 如何修改dedecms专题目录默认名称special
专题有一个聚合的效果,一般会比普通的文章页更符合用户需求.如果用dedecms建专题的话,默认的目录是special,怎么修改修改dedecms专题目录名称呢,比如将/special/改为/s/这样更 ...
- 常用dos命令(2)
文件管理 type 显示文本文件的内容. copy 将一份或多份文件复制到另一个位置. del 删除一个或数个文件. move 移动文件并重命名文件和目录.(Windows XP Home Editi ...
- thinkphp5.0 中简单处理微信支付异步通知
public function wx_notify(){ $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; libxml_disable_ent ...
- linux command lynx
[Purpose] Learning linux command lynx [Eevironment] Ubuntu 16.04 terminal apt-get ...
- redhat quay 安装试用
最近redhat 开源了quay 容器镜像管理平台,参考官方文档跑的时候需要订阅,各种不好使,然后就自己基于源码构建了 一个镜像(使用官方的dockerfile,构建出来的太大了1.9G 以及push ...
- ESA2GJK1DH1K升级篇: 远程升级准备工作: 使用TCP客户端连接Web服务器实现http下载数据
一,根目录建一个文件 二,使用浏览器访问 http://47.92.31.46:80/1.txt 或者 http://47.92.31.46/1.txt 三,使用TCP客户端访问文件内容 3 ...