Java 字符串中的最后一个重复字符
我正在寻找一种解决方案,以查找字符串中最后一个重复字符,该字符串表示“你今天过得怎么样”,应该给出Java 字符串中的最后一个重复字符,java,collections,Java,Collections,我正在寻找一种解决方案,以查找字符串中最后一个重复字符,该字符串表示“你今天过得怎么样”,应该给出y作为答案。我曾尝试使用链接哈希映射来获取有序插入,但最后一次重复的插入是d String testString = "how are you doing today"; char[] testStringArray = testString.toCharArray(); Map<Character,Integer> map = new LinkedHashMap&
y
作为答案。我曾尝试使用链接哈希映射来获取有序插入,但最后一次重复的插入是d
String testString = "how are you doing today";
char[] testStringArray = testString.toCharArray();
Map<Character,Integer> map = new LinkedHashMap<>();
for( char i : testStringArray) {
if (!(i==' ')) {
if(map.containsKey(i) ) {
map.put(i, map.get(i)+1);
} else {
map.put(i, 1);
}
}
}
System.out.println(map);
List<Entry<Character, Integer>> list = new ArrayList<>(map.entrySet());
ListIterator<Entry<Character, Integer>> it = list.listIterator(list.size());
while(it.hasPrevious()) {
if(it.previous().getValue() > 1)
System.out.println(it.previous().getKey());
}
}
String testString=“你今天过得怎么样”;
char[]testStringArray=testString.tocharray();
Map Map=newlinkedhashmap();
for(字符i:testStringArray){
如果(!(i=''){
if(图1){
map.put(i,map.get(i)+1);
}否则{
图.put(i,1);
}
}
}
系统输出打印项次(map);
List List=newarraylist(map.entrySet());
ListIterator it=list.ListIterator(list.size());
while(it.hasPrevious()){
if(it.previous().getValue()>1)
System.out.println(it.previous().getKey());
}
}
非常感谢您的帮助。您可以使用使用
集合的简单方法。
从字符串的字符数组中读取元素后,继续将它们添加到集合中(使用tocharray()
),如果add()
操作的返回值为false,则表示元素已经添加。保留一个包含“latest last element”的char变量。循环结束后打印即可 您可以使用一个简单的方法,使用集合
。
从字符串的字符数组中读取元素后,继续将它们添加到集合中(使用tocharray()
),如果add()
操作的返回值为false,则表示元素已经添加。保留一个包含“latest last element”的char变量。循环结束后打印即可 此代码从最后一个字符开始扫描,并检查它是否存在于第一个字符之前的剩余子字符串中:
String str = "how are you doing today";
int len = str.length();
int i = len - 1;
for (; i >= 1; i--) {
if (str.substring(0, i).contains(str.charAt(i) + "")) {
break;
}
}
if (i == 0) {
System.out.println("no repeating character");
} else
System.out.println(str.charAt(i));
for (int i = testString.length() - 1; i > 0; --i) {
char ch = testString.charAt(i);
if (testString.lastIndexOf(ch, i - 1) != -1) {
return ch;
}
}
// Handle no repeating character found.
此代码从最后一个字符开始扫描,并检查它是否存在于第一个字符之前的剩余子字符串中:
String str = "how are you doing today";
int len = str.length();
int i = len - 1;
for (; i >= 1; i--) {
if (str.substring(0, i).contains(str.charAt(i) + "")) {
break;
}
}
if (i == 0) {
System.out.println("no repeating character");
} else
System.out.println(str.charAt(i));
for (int i = testString.length() - 1; i > 0; --i) {
char ch = testString.charAt(i);
if (testString.lastIndexOf(ch, i - 1) != -1) {
return ch;
}
}
// Handle no repeating character found.
一个简单的解决方案是向后遍历字符,并使用lastIndexOf
查找该字符的上一个位置:
String str = "how are you doing today";
int len = str.length();
int i = len - 1;
for (; i >= 1; i--) {
if (str.substring(0, i).contains(str.charAt(i) + "")) {
break;
}
}
if (i == 0) {
System.out.println("no repeating character");
} else
System.out.println(str.charAt(i));
for (int i = testString.length() - 1; i > 0; --i) {
char ch = testString.charAt(i);
if (testString.lastIndexOf(ch, i - 1) != -1) {
return ch;
}
}
// Handle no repeating character found.
一个简单的解决方案是向后遍历字符,并使用lastIndexOf
查找该字符的上一个位置:
String str = "how are you doing today";
int len = str.length();
int i = len - 1;
for (; i >= 1; i--) {
if (str.substring(0, i).contains(str.charAt(i) + "")) {
break;
}
}
if (i == 0) {
System.out.println("no repeating character");
} else
System.out.println(str.charAt(i));
for (int i = testString.length() - 1; i > 0; --i) {
char ch = testString.charAt(i);
if (testString.lastIndexOf(ch, i - 1) != -1) {
return ch;
}
}
// Handle no repeating character found.
这是因为LinkedHashMap正在维护加法顺序,并且在给定字符串的d
之前插入y
,当您从最后一个字符串开始迭代时,它首先读取d
为了达到你想要的,我建议
从末尾读取字符串,然后使用count插入LinkedHashMap
从开始迭代LinkedHashMap以获得所需的输出
希望这有帮助。这是因为LinkedHashMap正在维护加法顺序,并且在给定字符串的d
之前插入y
,当您从最后一个字符串开始迭代时,它首先读取d
为了达到你想要的,我建议
从末尾读取字符串,然后使用count插入LinkedHashMap
从开始迭代LinkedHashMap以获得所需的输出
希望这有帮助。这段代码你可以用ArrayList或Set代替LinkedHashMap,没有必要。这是我的密码:
public static Character getLastRepeatingCharacter(String src) {
Character defaultChar = null;
char[] srcCharArr = src.toCharArray();
List<Character> list = new ArrayList<Character>();
final int length = srcCharArr.length;
for(int idx = 0; idx < length; idx++) {
if (srcCharArr[idx] == ' ') {
continue;
}
if (list.contains(srcCharArr[idx])) {
defaultChar = srcCharArr[idx];
}else {
list.add(srcCharArr[idx]);
}
}
if (list.size() == length) {
defaultChar = srcCharArr[length-1];
}
return defaultChar;
}
公共静态字符getLastRepeatingCharacter(字符串src){
字符defaultChar=null;
char[]srcCharArr=src.toCharArray();
列表=新的ArrayList();
最终整数长度=srccharrr.length;
for(int-idx=0;idx
此代码您可以使用ArrayList或Set代替LinkedHashMap,没有必要。这是我的密码:
public static Character getLastRepeatingCharacter(String src) {
Character defaultChar = null;
char[] srcCharArr = src.toCharArray();
List<Character> list = new ArrayList<Character>();
final int length = srcCharArr.length;
for(int idx = 0; idx < length; idx++) {
if (srcCharArr[idx] == ' ') {
continue;
}
if (list.contains(srcCharArr[idx])) {
defaultChar = srcCharArr[idx];
}else {
list.add(srcCharArr[idx]);
}
}
if (list.size() == length) {
defaultChar = srcCharArr[length-1];
}
return defaultChar;
}
公共静态字符getLastRepeatingCharacter(字符串src){
字符defaultChar=null;
char[]srcCharArr=src.toCharArray();
列表=新的ArrayList();
最终整数长度=srccharrr.length;
for(int-idx=0;idx
您发布的代码中有两个错误:1)在LinkedHashMap
中排序,2)在迭代器循环中调用previous()
两次
要从LinkedHashMap
获取所需的顺序,需要插入顺序。但更新映射中已有的值不会更改插入顺序。发件人:
请注意,如果将键重新插入地图,则插入顺序不受影响。(如果调用m.put(k,v),而m.containsKey(k)将在调用之前立即返回true,则将密钥k重新插入到映射m中。)
要解决这个问题,您必须删除条目并再次插入它。而不是:
map.put(i, map.get(i) + 1);
while (it.hasPrevious()) {
if (it.previous().getValue() > 1)
System.out.println(it.previous().getKey());
}
你必须做到:
int old = map.get(i);
map.remove(i);
map.put(i, old + 1);
第二个问题是在向后迭代的迭代器循环中调用两次previous()
。这样做的副作用是在找到所需的条目后再次推进迭代器。您需要缓存previous()
的结果并使用它两次。因此,不是:
map.put(i, map.get(i) + 1);
while (it.hasPrevious()) {
if (it.previous().getValue() > 1)
System.out.println(it.previous().getKey());
}
您需要执行以下操作:
while (it.hasPrevious()) {
Entry<Character, Integer> prev = it.previous();
if (prev.getValue() > 1)
System.out.println(prev.getKey());
}
事实证明,使用Java8Lambdas和streams可以更简单地实现这两种功能。首先,要生成字符出现的直方图(频率图),给定字符串输入
,请执行以下操作:
Map<Character, Long> hist =
input.chars()
.mapToObj(ch -> (char)ch)
.filter(ch -> ch != ' ')
.collect(groupingBy(ch -> ch, counting()));
总而言之,你有以下几点:
Optional<Character> lastRepeat(String input) {
Map<Character, Long> hist =
input.chars()
.mapToObj(ch -> (char)ch)
.filter(ch -> ch != ' ')
.collect(groupingBy(ch -> ch, counting()));
return input.chars()
.mapToObj(ch -> (char)ch)
.filter(ch -> ch != ' ')
.filter(ch -> hist.get(ch) > 1L)
.reduce((a, b) -> b);
}
可选lastRe