问题描述


Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.

Examples:
s = "leetcode"
return 0. s = "loveleetcode",
return 2.

Java算法实现

public class Solution {
public int firstUniqChar(String s) {
TreeSet<Character>set=new TreeSet<Character>();
Map<Character,Integer>unique=new HashMap<Character, Integer>();
for(int i=0;i<s.length();i++){
Character ch=s.charAt(i);
if(!set.contains(ch)){ //利用Set中每个元素都是唯一的这个属性,记录元素是否出现过
set.add(ch);
unique.put(ch, i);
}
else{
if(unique.containsKey(ch)){
unique.remove(ch);
}
}
} int index=-1;
Object[] values=unique.values().toArray();
if(values.length>0){
index=(int) values[0];
for(int i=1;i<values.length;i++){
if((int)values[i]<index){
index=(int)values[i];
}
}
}
return index;
}
}

Leetcode算法比赛----First Unique Character in a String的更多相关文章

  1. LeetCode算法题-First Unique Character in a String(Java实现)

    这是悦乐书的第213次更新,第226篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第81题(顺位题号是387).给定一个字符串,找到它中的第一个非重复字符并返回它的索引. ...

  2. leetcode笔记11 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 ...

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

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

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

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

  5. LeetCode之387. First Unique Character in a String

    -------------------------------------------------- 最开始的想法是统计每个字符的出现次数和位置,如下: AC代码: public class Solu ...

  6. LeetCode_387. First Unique Character in a String

    387. First Unique Character in a String Easy Given a string, find the first non-repeating character ...

  7. 209. First Unique Character in a String

    Description Find the first unique character in a given string. You can assume that there is at least ...

  8. LeetCode First Unique Character in a String

    原题链接在这里:https://leetcode.com/problems/first-unique-character-in-a-string/ 题目: Given a string, find t ...

  9. LeetCode算法题-Number of Lines To Write String(Java实现)

    这是悦乐书的第319次更新,第340篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第188题(顺位题号是806).我们要将给定字符串S的字母从左到右写成行.每行最大宽度为 ...

随机推荐

  1. [转] 如何在 CentOS7 中使用阿里云的yum源

    [From] https://www.cnblogs.com/lpbottle/p/7875400.html 1. 备份原来的yum源 mv /etc/yum.repos.d/CentOS-Base. ...

  2. Mac下用Launchd实现使用rc.local执行开机启动命令

    其实原理很简单,使用Launchd创建一个开机启动的服务,然后这个服务关联/etc/rc.local文件,那么接下来操作rc.local就和Linux下一样的了. 当然,这种思路还可以直接使用在~/. ...

  3. ADO.NET中DbConnection.GetSchema方法的使用总结

    此方法获取数据库的结构,所以可以用它获取数据库中所有的表 先上代码 string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;U ...

  4. Linux下mysql基础命令(一)

    1, 创建mysqld数据库的管理用户:             要把root用户设置为管理员,我们应该运行下面的命令:    # mysqladmin -u root password 密码 一般情 ...

  5. 四大组件之Activity Task任务栈4种启动模式

    1.启动模式 standard,创建一个新的Activity. singleTop,栈顶不是该类型的Activity,创建一个新的Activity.否则,onNewIntent. singleTask ...

  6. console和chrom-tool

    资料整理自网络 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <t ...

  7. Oracle 12c 创建用户

    本文转载自 http://blog.itpub.net/29047826/viewspace-1453577/,侵删.   12c推出了可插拔库,管理用户也分为两种:本地用户和公用用户.本地用户是指在 ...

  8. 《Think Python》第5章学习笔记

    目录 5.1 整除和取模(Floor division and modulus) 5.2 布尔表达式(Boolean expressions) 5.3 逻辑运算符(Logical operators) ...

  9. Abschlussarbeit:Konstruktion und Implementierung von Dota2 Datenbank Intelligent Verwaltungsplatfom

    1.Die Hintergrund und Bedeutung des Themas Dank nicht ausreichendes Erkenntnisse der Spielplanner un ...

  10. ora-01747:因为表中存在关键字造成的

    ORCLE报错解决(ora-01747:无效的用户.表.列,表.列)  一.ora-01747:无效的用户.表.列,表.列 这个问题出现是因为表中存在关键字造成的,如果想新增数据直接用sql语句,查询 ...