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 ...
随机推荐
- add添加
s = {1,2,3,4,5,6,} 进行添加数据! s = {1,2,3,4,5,6,} s.add('s')#添加字符串's' s.add('3')#添加字符串'3' s.add(3)#添加3和字 ...
- AOPR破解的密码复制的方法
Advanced Office Password Recovery是一款office密码破解工具,简称AOPR.使用过Advanced Office Password Recovery的用户都知道成功 ...
- VB.net Wcf事件广播(订阅、发布)
这篇东西原写在csdn.net上,最近新开通了博客想把零散在各处的都转移到一处. 一.源起 学WCF有一段时间了,可是无论是微软的WebCast还是其他网上的教程,亦或我购买的几本书中,都没有怎么 ...
- java高新技术-java5的静态导入与编译器语法设置
静态导入 import语句可以导入一个类或某个包中的所有类 import static 语句导入有一个类中的某个静态方法或所有静态方法 使用Math.random() 可以这样做 package co ...
- [Think In Java]基础拾遗4 - 并发
第21章节 并发 1. 定义任务 任务:任务就是一个执行线程要执行的一段代码.(在C语言中就是函数指针指向的某个地址开始的一段代码) [记忆误区]:任务不是线程,线程是用来执行任务的.即任务由线程驱动 ...
- Linux 远程复制文件
Linux 远程复制文件 如果想把机器A上面的dir目录下面的所有文件复制到机器B的dir目录下,我们可以使用nc命令来完成 在机器A的dir目录下面执行: tar -czf - * | nc -l ...
- SpringMVC常用注解的用法
1. @PathVariable 当使用@RequestMapping URI template 样式映射时, 即 someUrl/{paramId}, 这时的paramId可通过 @Pathvari ...
- jQuery动态增删改查表格信息,可左键/右键提示
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 【原创】风讯DotNetCMS V1.0~V2.0 SQL注入漏洞
文章作者:rebeyond 注:文章首发I.S.T.O信息安全团队,后由原创作者友情提交到乌云-漏洞报告平台.I.S.T.O版权所有,转载需注明作者. 受影响版本:貌似都受影响. 漏洞文件:use ...
- Java编程中的美好
java程序员如何写出"优美"代码,动力节点告诉你怎么办: 1.注释尽可能全面 对于方法的注释应该包含详细的入参和结果说明,有异常抛出的情况也要详细叙述:类的注释应该包含类的功能说 ...