The ability to debug and overall thinking need to improve


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.

Note: You may assume the string contain only lowercase letters.

Solution: using linkedHashMap<> : have the insertion order!

class Solution {
public int firstUniqChar(String s) {
Map<Character, Integer> map = new LinkedHashMap<Character, Integer>();
for(int i = 0; i<s.length(); i++){
Character c = s.charAt(i);
if(map.containsKey(c)){
map.put(c, 2);
}else {
map.put(c,1);
}
}
char temp = '#';
for(Map.Entry<Character, Integer> entry : map.entrySet()){
System.out.println(entry.getKey() + " " + entry.getValue());
if(entry.getValue() == 1){
temp = entry.getKey();
break;
}
}
for(int i = 0; i<s.length(); i++){
if(s.charAt(i) == temp) return i;
}
return -1;
}
}

*387. First Unique Character in a String (linkedhashmap + string) debug manunally instead of using leetcode的更多相关文章

  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 (字符串中的第一个唯一字符)

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

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

  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. CPU的CAS操作

    https://blog.csdn.net/qq_35492857/article/details/78471032 https://www.cnblogs.com/gdjdsjh/p/5076815 ...

  2. /var 目录下文件系统

    /var    :日志文件/var/log:各种系统日志存放地*/var/log/message :系统信息默认日志文件 (非常重要)按周自动轮循/var/log/secure  :记录登入系统信息文 ...

  3. 【ACM】会场安排问题

    会场安排问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 学校的小礼堂每天都会有许多活动,有时间这些活动的计划时间会发生冲突,需要选择出一些活动进行举办.小刘的工 ...

  4. 文献综述一:基于UML技术的商品管理系统设计与实现

    一.基本信息 标题:基于UML技术的商品管理系统设计与实现 时间:2018 出版源:福建电脑 文件分类:uml技术的研究 二.研究背景 使用 UML 技术对商品管理系统进行了分析与研究,使用户对商品信 ...

  5. 使用codesmith无法连接mysql问题

    最近研究codesmith的用法,遇到了如题的问题,记录一下解决的方法. 1.问题描述: 在codesmith中选择MySQLSchemaProvider并连接数据库时,会报以下错误: Test fa ...

  6. 利用ssh传输文件-服务器之间传输文件

    利用ssh传输文件   在linux下一般用scp这个命令来通过ssh传输文件. 1.从服务器上下载文件scp username@servername:/path/filename /var/www/ ...

  7. Murano Weekly Meeting 2016.06.07

    Meeting time: 2016.June.7 1:00~2:00 Chairperson:  Kirill Zaitsev, from Mirantis Meeting summary: 1.A ...

  8. Neutron命令测试4

    jolin@jolin:~$ route -nKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface ...

  9. 位于XDB的服务器localhost要求用户名和密码,端口占用

    问题现象: 从MyEclipse启动部署在tomcat上的web程序后,出现如下问题: 然后访问tomcat主页(http://localhost:8080/),弹出如下对话框: 问题原因: 机器上安 ...

  10. Unicode汉字编码表以及参考源码分享

    1 Unicode编码表  Unicode只有一个字符集,中.日.韩的三种文字占用了Unicode中0x3000到0x9FFF的部分  Unicode目前普遍采用的是UCS-2,它用两个字节来编码一个 ...