LeetCode之387. First Unique Character in a String

--------------------------------------------------
最开始的想法是统计每个字符的出现次数和位置,如下:
AC代码:
public class Solution {
public int firstUniqChar(String s) {
Count c[]=new Count[26];
for(int i=0;i<s.length();i++){
int index=s.charAt(i)-'a';
if(c[index]==null) c[index]=new Count(i);
c[index].count++;
}
int min=s.length();
for(int i=0;i<c.length;i++){
if(c[i]!=null && c[i].count==1 && c[i].firstIndex<min) min=c[i].firstIndex;
}
return min==s.length()?-1:min;
}
private class Count{
int count;
int firstIndex;
public Count(int firstIndex){
this.firstIndex=firstIndex;
}
}
}
但是这样子代码略显拖沓,干脆只记录字符的出现次数,然后再从头扫描如果出现次数为1就表明这是要找的位置了,代码如下:
AC代码:
public class Solution {
public int firstUniqChar(String s) {
int c[]=new int[26];
for(int i=0;i<s.length();i++) c[s.charAt(i)-'a']++;
for(int i=0;i<s.length();i++) if(c[s.charAt(i)-'a']==1) return i;
return -1;
}
}
题目来源: https://leetcode.com/problems/first-unique-character-in-a-string/
LeetCode之387. First Unique Character in a String的更多相关文章
- 【LeetCode】387. First Unique Character in a String
Difficulty:easy More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/first-unique-cha ...
- 【LeetCode】387. First Unique Character in a String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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 ...
- [LeetCode&Python] Problem 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 ...
- [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 ...
- Java [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 does ...
随机推荐
- python基础-函数式编程
python基础-函数式编程 高阶函数:map , reduce ,filter,sorted 匿名函数: lambda 1.1函数式编程 面向过程编程:我们通过把大段代码拆成函数,通过一层一层 ...
- POSIX字符集
[. .] 排序元素 [= =] 等价元素 类别 匹配字符 [:alnum:] 数字字符 [:alpha:] 字母字符 [:blank:] 空格与制表符 [:cntrl:] 控制字符 [:di ...
- spring mvc 项目聚合
创建一个maven 项目当父项目 创建时选择默认就可以 创建完成后 修改 pom.xml文件 把 packaging 的war改为pom <modelVersion>4.0.0</ ...
- APiCloud真机调试需要注意的几个问题
具体请看官方文档:http://docs.apicloud.com/Dev-Tools/wifi-debug. APiCloud Android手机真机调试,需要首先在手机上安装官网提供的apploa ...
- ubuntu qtcreator 硬件权限问题
在使用 qtcreator 在 ubuntu(debian.mint 等类同)下做开发时,常用到权限问题,无法直接操作硬件,比如串口等. 办法之一是使用 root 打开 creator,进而进行其他操 ...
- iOS - 系统提醒短音频
Demo下载地址 iPhone端系统Audio资源路径:/System/Library/Audio/UISounds 首先,通过 NSFileManager 获取资源路径下的所有路径(文件夹/文件) ...
- HDU 2204Eddy's爱好(容斥原理)
Eddy's爱好 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- 简要介绍BASE64、MD5、SHA、HMAC几种方法。
加密解密,曾经是我一个毕业设计的重要组件.在工作了多年以后回想当时那个加密.解密算法,实在是太单纯了. 言归正传,这里我们主要描述Java已经实现的一些加密解密算法,最后介绍数字证书. ...
- nginx 499 状态码优化
在grafana界面中发现不少499的状态码,在网上了解到出现499的原因大体都是说服务端处理时间过长,客户端主动关闭了连接. 既然原因可能是服务端处理时间太长了,看一下upstream_resp ...
- 20145212&20145204信息安全系统实验五
一.实验步骤 1.阅读理解源码 进入/arm2410cl/exp/basic/07_httpd目录,使用 vim编辑器或其他编辑器阅读理解源代码. 2.编译应用程序 运行 make 产生可执行文件 h ...