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.

分析:找出字符串中第一次出现且不重复的字符,返回其字符的下标,如果不存在就返回-1.

用hashMap进行求,hashMap中可以通过ey-value的存储位置对和位置,对数据进行标记

  public int firstUniqChar(String s) {
Map<Character,Integer> map=new HashMap<Character,Integer>(); for(int i=0;i<s.length();i++){
char c=s.charAt(i);
if(map.containsKey(c)){
map.put(c,-1);
}else{
map.put(c,1);
}
} for(int i=0;i<s.length();i++){
if(map.get(s.charAt(i))==1){
return i;
}
}
return -1;
}

387. First Unique Character in a String的更多相关文章

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

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

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

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

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

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

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

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

  8. *387. First Unique Character in a String (linkedhashmap + string) debug manunally instead of using leetcode

    The ability to debug and overall thinking need to improve Given a string, find the first non-repeati ...

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

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

随机推荐

  1. Oracle数据字典

    数据字典-简介 Oracle数据字典的名称由前缀和后缀组成,使用下划线"_"连接,其代表的含义如下: ● DBA_:包含数据库实例的所有对象信息. ● V$_:当前实例的动态视图, ...

  2. SCVMM中Clone虚拟机失败显示Unsupported Cluster Configuration状态

    在SCVMM进行虚拟机的Clone,虽然失败了,但是Clone出虚拟机却显示在SCVMM控制台的虚拟机的列表中,并且状态是Unsupported Cluster Configuration.无法修复, ...

  3. 前端编辑工具之VSCode

      因为前段时间看了瞬息之间的一篇文章编辑器背后的程序观, 里面只提到了Visual studio. 我想想可能是因为非.Net开发者,所以不知道Visual sutdio code这个工具.来看看V ...

  4. AC日记——二叉堆练习3 codevs 3110

    3110 二叉堆练习3  时间限制: 3 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 给定N(N≤500,000)和N个整 ...

  5. Aptana+spket搭建EXTJS开发环境

    1.下载 aptana studio.spket-1.6.23.ext-4.2.1.883.jsb2 其中:ext-4.2.1.883.jsb2这个比较重要 2.将spket-1.6.23解压后放到a ...

  6. POJ 2125 Destroying the Graph 二分图最小点权覆盖

    Destroying The Graph Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8198   Accepted: 2 ...

  7. servlet过滤器使用

    servlet的过滤器夹在用户和servlet之间,可以截获并预处理或者打断{用户到servlet的请求或servlet到用户的响应},但本身并不能处理和响应用户请求. filter类必须实现Filt ...

  8. lombok在IntelliJ IDEA下的使用

    lombok是一款可以精减java代码.提升开发人员生产效率的辅助工具,利用注解在编译期自动生成setter/getter/toString()/constructor之类的代码.代码越少,意味着出b ...

  9. [LeetCode] Longest Absolute File Path 最长的绝对文件路径

    Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...

  10. 如何设置Oracle客户端与服务器的字符集一致

    查看 Oracle 服务器字符集 select userenv('language') from dual; 设置Oracle客户端字符集 添加环境变量NLS_LANG 值与服务器的Oracle服务器 ...