问题描述


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. (C/C++) 指向函數的指標

    最近再跟指標做朋友, 正好遇到函數與指標. 其實函數也在程式內也是有屬於自己的位址 所以指標一樣能指向函數, 在此釐清自己的觀念以及記錄下來. #include <stdio.h> #in ...

  2. 基于wavesurfer.js的超大音频的渐进式请求实现

    最近在对超大音频的渐进式请求实现上面消耗了不少时间,主要是因为一对音频的基本原理不太理解,二刚开始的时候太依赖插件,三网上这块的资料找不到只能靠自己摸索.由于交互复杂加上坑比较多,我怕描述不清,这里主 ...

  3. markdown页面内跳转

    分两步 第一步 实际语法比较简单,在需要跳转的位置添加锚点,语法如下: <span id="jump">跳转到的地方</span> 第二步 在需要点击跳转的 ...

  4. ThreadLocal系列(二)-InheritableThreadLocal的使用及原理解析

    ThreadLocal系列之InheritableThreadLocal的使用及原理解析(源码基于java8) 上一篇:ThreadLocal系列(一)-ThreadLocal的使用及原理解析 下一篇 ...

  5. LARTC

    大牛的博客 howto ,however, is simplify. another space ip link list ip address show ip route show route -n ...

  6. 修改docker的port端口映射

    原以为修改docker的端口映射只是一条命令就可以搞定,结果发现没那么简单,记录一下以防忘记.1. 查看需要修改的容器,记住container iddocker ps -a2. 停止容器docker ...

  7. VS生成Map文件

    一.右键项目属性->链接器->启用增量链接:关闭,选择  否 (/INCREMENTAL:NO) 二.右键项目属性->链接器->调试-> 生成调试信息:是 (/DEBUG ...

  8. ECharts概念学习系列之ECharts官网教程之自定义构建 ECharts(图文详解)

    不多说,直接上干货! 官网 http://echarts.baidu.com/tutorial.html#%E8%87%AA%E5%AE%9A%E4%B9%89%E6%9E%84%E5%BB%BA%2 ...

  9. JVM的类加载时机

    类加载过程中每个步骤的顺序 我们已经知道,类加载的过程包括:加载.连接.初始化,连接又分为:验证.准备.解析,所以说类加载一共分为5步:加载.验证.准备.解析.初始化. 其中加载.验证.准备.初始化的 ...

  10. i.mx6 Android5.1.1 servicemanager本地服务

    接在之前的 i.mx6 Android5.1.1 初始化流程之init进程 i.mx6 Android5.1.1 初始化流程之init.rc解析 servicemanager是由init创建的本地服务 ...