--------------------------------------------------

最开始的想法是统计每个字符的出现次数和位置,如下:

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的更多相关文章

  1. 【LeetCode】387. First Unique Character in a String

    Difficulty:easy  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/first-unique-cha ...

  2. 【LeetCode】387. First Unique Character in a String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  3. LeetCode 387. First Unique Character in a String (字符串中的第一个唯一字符)

    题目标签:String, HashMap 题目给了我们一个 string,让我们找出 第一个 唯一的 char. 设立一个 hashmap,把 char 当作 key,char 的index 当作va ...

  4. 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 ...

  5. leetcode修炼之路——387. First Unique Character in a String

    最近公司搬家了,有两天没写了,今天闲下来了,继续开始算法之路. leetcode的题目如下: Given a string, find the first non-repeating characte ...

  6. 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 ...

  7. [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 ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. jQuery1.9之后使用on()绑定 动态生成元素的 事件无效

    来自互联网: 需要绑定a的父级元素(此元素必须为静态元素,不是后来动态生成的),然后设定on()方法的selector参数才行: $('p').on('mouseenter', 'a', functi ...

  2. How To Set Up an OpenVPN Server on Ubuntu 14.04

    Prerequisites The only prerequisite is having a Ubuntu 14.04 Droplet established and running. You wi ...

  3. 无法加载父级样式或设置IIS的asp站点启用父路径

    打开IIS 1.单击站点,在"IIS"区域中找到ASP图标,双击. 2.找到"启用父路径"项目,将对应的值设置为"TRUE"即可.   顶

  4. Deepin 15.3 编译同步SudaMod 2.0

    1.设置hosts,方便访问国外站点 $ wget https://raw.githubusercontent.com/racaljk/hosts/master/hosts -qO /tmp/host ...

  5. Python【第三章】:python 面向对象 (new)

    面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类 是一个模板,模板中包装了多个“函数”供使用(可以讲多函数中公用的变量封装到对象中) 对象,根据模板创建的实例(即:对象),实 ...

  6. Java内存与垃圾回收调优

     Java(JVM)内存模型 正如你从上面的图片看到的,JVM内存被分成多个独立的部分.广泛地说,JVM堆内存被分为两部分——年轻代(Young Generation)和老年代(Old Generat ...

  7. python:mysql+pycharm+Django环境搭建

    1.安装mysql-python 环境:OS X Yosemite10.10.2 + Python2.7 首先网上搜了下mysql-python,说要先安装mysql客户端,然后改配置文件,可是各种改 ...

  8. Google Maps API V3 之 图层

    Google官方教程: Google 地图 API V3 使用入门 Google 地图 API V3 针对移动设备进行开发 Google 地图 API V3 之事件 Google 地图 API V3 ...

  9. rpm---linux软件安装与管理

    linux的安装命令选项太多,整理一下,方便后期查找. 汇总: install: rpm -ivh 包全名 安装 upgrade: rpm -Uvh 包全名 升级 erase: rpm -e 包名 删 ...

  10. Discuzx系统 CSS 编码规范,CSS属性书写顺序

    1. 属性写在一行内,属性之间.属性名和值之间以及属性与“{}”之间须有空格,例如:.class { width: 400px; height: 300px; }     2. 属性的书写顺序:    ...