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 ...
随机推荐
- 使用VelocityTracker来完成MotionEvent移动速率计算
先看效果图 关键代码(此处记录单点): switch (event.getAction()){ case MotionEvent.ACTION_DOWN: if (veloctiy==null) { ...
- Uninstall from GAC In C# code
How do I uninstall the GAC from my C# application. I am not able to uninstall, the particular exe an ...
- BZOJ 1078: [SCOI2008]斜堆
1078: [SCOI2008]斜堆 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 770 Solved: 422[Submit][Status][ ...
- socket编程基础
socket编程 什么是socket 定义 socket通常也称作套接字,用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过套接字向网络发出请求或者应答网络请求. socket起源于Unix ...
- eclipse安装和配置
一.下载eclipse eclipse下载页 (选择"Eclipse IDE for Java EE Developers",适用于web和android开发) 我用的是luna的 ...
- <<< java异常The import java.util cannot be resolved
异常:The import java.util cannot be resolved 原因:这是由于你的项目buildpath不对 解决方案:右键项目-------buildpath--------最 ...
- jQuery5~7章笔记 和 1~3章的复习笔记
JQery-05 对表单和表格的操作及其的应用 JQery-06 jQuery和ajax的应用 JQery-07 jQuery插件的使用和写法 JQery-01-03 复习 之前手写的笔记.实在懒得再 ...
- Win7如何删除需要管理员权限才能删除的文件夹
在Windows 7系统运行中.往往会遇到想要删除某个文件夹时,系统提示:文件夹访问被拒绝 你需要权限来执行此操作,如何才能删除此类文件夹呢? ------------------ --------- ...
- C#中Trim()、TrimStart()、TrimEnd()的用法
string s = " from dual union all "; s = s.Trim().TrimEnd("union all".To ...
- R中的<-和=赋值符号的细致区别
<-创建的变量的作用范围可以在整个顶层环境,而=仅仅在一个局部环境. 但要<-创建的变量如果是在函数实参传递的时候创建的,其的作用范围可以在整个顶层环境,有一个前提条件:对应的形参在函数内 ...