最近一位采访者要我在字符串中实现第一个非重复字符,我使用两个不同的循环实现了hashmap。虽然时间复杂度是O(n)+ O(n),但他让我解决在一个循环中。有人告诉我该怎么做?
以下是我的实施:
import java.util.HashMap;
import java.util.Map;
public class firstnonrepeating {
public static void main(String[] args) {
String non = "nnjkljklhihis";
Map<Character, Integer> m = new HashMap<Character, Integer>();
for (int i = 0; i < non.length(); i++) {
if (m.get(non.charAt(i)) != null) {
m.put(non.charAt(i), m.get(non.charAt(i)) + 1);
} else {
m.put(non.charAt(i), 1);
}
}
for (int i = 0; i < non.length(); i++) {
if (m.get(non.charAt(i)) == 1) {
System.out.println("First Non Reapeating Character is "
+ non.charAt(i));
break;
} else {
if (i == non.length() - 1)
System.out.println("No non repeating Character");
}
}
}
}
答案 0 :(得分:2)
String non = "nnnjkljklhihis";
Map<String,LinkedHashSet<Character>> m = new HashMap<String,LinkedHashSet<Character>>() ;
m.put("one", new LinkedHashSet<Character>());
m.put("else", new LinkedHashSet<Character>());
m.put("all", new LinkedHashSet<Character>());
for (int i = 0; i < non.length(); i++) {
if (m.get("all").contains(non.charAt(i))) {
m.get("one").remove(non.charAt(i));
m.get("else").add(non.charAt(i));
} else {
m.get("one").add(non.charAt(i));
m.get("all").add(non.charAt(i));
}
}
if(m.get("one").size()>0){
System.out.println("first non repeatant : "+m.get("one").iterator().next());
}
答案 1 :(得分:0)
我将如何做到这一点:
import java.util.HashMap;
import java.util.Map;
public class Main
{
public static void main(String[] args)
{
String characters = "nnjkljklhihis";
Character firstNonRepeatingChar = getFirstNonRepeatingCharacter(characters);
if(firstNonRepeatingChar == null)
{
System.out.println("No non repeating characters in " + characters);
}
else
{
System.out.println("The first non repeating character is " + firstNonRepeatingChar);
}
}
private static Character getFirstNonRepeatingCharacter(String characters)
{
Map<Integer, Character> m = new HashMap<Integer, Character>();
for(int i = 0; i < characters.length(); i++)
{
Character currentChar = characters.charAt(i);
if(i > 0)
{
Character previousChar = m.get(i-1);
if(!previousChar.equals(currentChar))
{
return currentChar;
}
}
m.put(i, currentChar);
}
return null;//No non repeating character found
}
}
答案 2 :(得分:0)
这与奥萨马的答案相同,以更现代的方式重写。
public static Optional<Character> getFirstNonRepeatingCharacter(String characters) {
HashMap<Character, Consumer<Character>> map = new HashMap<>();
LinkedHashSet<Character> set = new LinkedHashSet<>();
for(char c: characters.toCharArray()) {
map.merge(c, set::add, (_1, _2) -> set::remove).accept(c);
}
return set.stream().findFirst();
}
答案 3 :(得分:0)
另一个可能的解决方案:
public class FirstNonRepeatingCharacterInString {
public static void main(String[] args) {
Character character = firstNonRepeatingCharacter("nnjkljklhihis");
System.out.println("First Non repeating character : " + character != null ? character : null);
}
private static Character firstNonRepeatingCharacter(String arg) {
char[] characters = arg.toCharArray();
Map<Character, Character> set = new LinkedHashMap<>();
// cost of the operation is O(n)
for (char c : characters) {
if (set.containsKey(c)) {
set.remove(c);
} else {
set.put(c, c);
}
}
//here we are just getting the first value from collection
// not iterating the whole collection and the cost of this operation is O(1)
Iterator<Character> iterator = set.keySet().iterator();
if (iterator.hasNext()) {
return iterator.next();
} else {
return null;
}
}
}
答案 4 :(得分:0)
给出一个字符串,找到它的第一个非重复字符:
public class Test5 {
public static void main(String[] args) {
String a = "GiniSoudiptaGinaProtijayi";
Map<Character, Long> map = a.chars().mapToObj(
ch -> Character.valueOf((char)ch)
).collect(Collectors.groupingBy(Function.identity(),
LinkedHashMap:: new,
Collectors.counting()
));
System.out.println(map);
//List<Character> list = map.entrySet().stream().filter( entry -> entry.getValue() == 1 )
//.map(entry -> entry.getKey()).collect(Collectors.toList());
//System.out.println(list);
Character ch = map.entrySet().stream()
.filter( entry -> entry.getValue() == 1L )
.map(entry -> entry.getKey()).findFirst().get();
System.out.println(ch);
}
}