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笔记(5)类__方法与继承
方法 类方法@classmethod,实例方法,静态方法@staticmethod,特殊方法,__init__ 形式上的区别:调用是通过类和实例进行,不能直接调用,有自己的特殊参数,如__init__ ...
- 使用腾讯开发平台获取QQ用户数据资料
<今天是七夕:祝大家七夕嗨皮,前可么么哒,后可啪啪啪> Tips:本篇博客将教你如何使用腾讯开发平台获取QQ用户资料 ----------------------------------- ...
- (转) java定时器的几种用法
package com.lid; import java.util.Calendar; import java.util.Date; import java.util.Timer; import ja ...
- windows下使用pip安装python的第三方lxml库
lxml是Python语言里和XML以及HTML工作的功能最丰富和最容易使用的库.lxml库的安装和python其他第三方库的安装方法是一样的,只是可能由于一些细节上的失误导致安装失败. 工具 Pyt ...
- OSI参考模型及各层功能,TCP与UDP的区别
OSI参考模型:ISO/IEC 7498标准定义了网络互联的7层结构模型,即开放系统互连参考模型. OSI参考模型定义了开放系统的层次结构.层次之间的相互关系,以及各层所包括的可能的服务.OSI的服务 ...
- MlLib--逻辑回归笔记
批量梯度下降的逻辑回归可以参考这篇文章:http://blog.csdn.net/pakko/article/details/37878837 看了一些Scala语法后,打算看看MlLib的机器学习算 ...
- d3 document
https://github.com/d3/d3/wiki/API--%E4%B8%AD%E6%96%87%E6%89%8B%E5%86%8C https://github.com/d3/d3/wik ...
- linux下的apache配置文件详解
.Apache的配置由httpd.conf文件配置,因此下面的配置指令都是在httpd.conf文件中修改. 站点的配置(基本配置) (1) 基本配置: ServerRoot "/mnt/s ...
- C++11特性:decltype关键字
decltype简介 我们之前使用的typeid运算符来查询一个变量的类型,这种类型查询在运行时进行.RTTI机制为每一个类型产生一个type_info类型的数据,而typeid查询返回的变量相应ty ...
- 浅谈Android中layout_weight
引言 在开发android过程中,我们经常需要对界面进行按比例进行布局,我们一般都会使用layout_属性来进行设置.今天这篇文章我们就来简单介绍下layout_weight的使用和布局原理.随着做项 ...