*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-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的更多相关文章
- 【LeetCode】387. First Unique Character in a String
Difficulty:easy More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/first-unique-cha ...
- LeetCode 387. First Unique Character in a String (字符串中的第一个唯一字符)
题目标签:String, HashMap 题目给了我们一个 string,让我们找出 第一个 唯一的 char. 设立一个 hashmap,把 char 当作 key,char 的index 当作va ...
- 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 ...
- 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 ...
- leetcode修炼之路——387. First Unique Character in a String
最近公司搬家了,有两天没写了,今天闲下来了,继续开始算法之路. leetcode的题目如下: Given a string, find the first non-repeating characte ...
- 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 ...
- [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 ...
- [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 ...
- 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 ...
随机推荐
- Codeforces - 625B 贪心
求最小不重复匹配次数 改最后一个字符最划算 我当时怎么就没看出来.. #include<bits/stdc++.h> using namespace std; string S,T; in ...
- Linux字符设备简单示例
1. Linux字符设备是一种按字节来访问的设备,字符驱动则负责驱动字符设备,这样的驱动通常实现open.close.read和write系统调用.例如:串口.Led.按键等. 2. 通过字符设备文件 ...
- Java 类型转换(int->String)
1 如何将字串 String 转换成整数 int? A. 有两个方法: 1). int i = Integer.parseInt([String]); 或 i = Integer.parseInt([ ...
- win7旗舰版+caffe+vs2013+matlab2014b(无GPU版)
参考网站: http://www.cnblogs.com/njust-ycc/p/5776286.html 无法找到gpu/mxGPUArray.h: No such file or director ...
- 关于box-shadow、border-radius不兼容ie8的解决办法
本来从css3兼容ie9+挺好的,可是总有一些共识要求ie8+,于是就有了我们的苦逼的找解决办法.之前在网上查到一些说用 PIE.htc. But 我就是按照他说的写的没有管用.请教了一下别人才会写了 ...
- 转 oracheck
转 https://dbaplus.cn/news-10-220-1.html ORAchk 之前被称为RACcheck,后来它的检查范围进行了扩展,改名为了ORAchk,它是在数据库系统进行健康检查 ...
- spark RDD运算机制
- Mac使用zsh导致maven命令无效的解决方案
第一步: vim ~/.zshrc 第二步:在.zshrc末尾加上 source ~/.bash_profile: 保存推出了 第三步 source ~/.bash_profile
- C语言判别输入的东东
梗概:现在很多用C语言写出来的作业,都是用户输入后,电脑对应操作的.其实这样有没有漏洞呢? 这样的管理系统,相信大家也不陌生,我们这里不是谈它的功能和怎样实现..我们就谈谈最后一行.[输入序号].其实 ...
- python单元测试框架-unittest(四)之用例综合框架管理
简述为何如要框架? 前面测试用例与执行都是写在一个文件,当用例数量不断增加的时候,用例的执行与管理变得非常麻烦,因此需要对用例根据具体的功能模块来使用单独的模块来管理.就像一所学校要根据不同年级进行分 ...