问题描述


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. TX2 之tensorflow环境部署

    刷机jetpack3.3 首先TX2必须是3.3版本的jetpack,因为截止目前nvidia发布的tensorflow只支持3.3版本的jetpack,刷机的具体步骤可以参考NVIDIA Jetso ...

  2. Windows运行常用命令(win+R)

    Windows运行常用命令(win+R) 1.calc: 启动计算器 2.notepad: 打开记事本 3.write: 写字板 4.mspaint: 画图板 5.snippingtool:截图工具, ...

  3. shiro原理及其运行流程介绍

    shiro原理及其运行流程介绍 认证执行流程 1.通过ini配置文件创建securityManager 2.调用subject.login方法主体提交认证,提交的token 3.securityMan ...

  4. Oracle 11g

    Oracle 11g 第一章  Oracle 11g数据库简介 1.1  认识Oracle11g Oracle 11g是Oracle 数据库最新的版本,目前已经被企业广泛的应用. 1.2  Oracl ...

  5. jenkins发送测试报告邮件

     1.安装插件 Email Extension Plugin 2.设置Extended E-mail Notification a."系统管理"--“系统设置”.配置Extende ...

  6. 【杂题】[LibreOJ 2541] 【PKUWC2018】猎人杀【生成函数】【概率与期望】

    Description 猎人杀是一款风靡一时的游戏"狼人杀"的民间版本,他的规则是这样的: 一开始有 n个猎人,第 i 个猎人有仇恨度 wi.每个猎人只有一个固定的技能:死亡后必须 ...

  7. 3Q大战现高潮,360 推出Android "3Q" IM即时通讯,岁末年初3Q大战惊现高潮

    岁末年初3Q大战惊现高潮,360震撼推出Android "3Q" IM即时通讯       看过了QQ和360斗争的开端高潮,当然现在还不能说这场斗争已经结束,在我看来这次的事件未 ...

  8. css中奇怪的地方

    1.border-color      继承内部元素前景色(color:black.可能对元素本身没有效果) 2.border-style:none;//不仅样式没了,border-width也变为0 ...

  9. python-锁机制

    锁 Lock() Lock(指令锁)是可用的最低级的同步指令.Lock处于锁定状态时,不被特定的线程拥有.Lock包含两种状态——锁定和非锁定,以及两个基本的方法. 可以认为Lock有一个锁定池,当线 ...

  10. Spring中使用JMS

    JMS为了Java开发人员与消息代理(message broker)交互和收发消息提供了一套标准API.而且,由于每个message broker都支持JMS,所以我们就不需要学习额外的消息API了. ...