在字符串中存储和打印连续字符?
Storing and printing consecutive characters in a String?
任务是将连续的字符存储在一个列表中。 Input/Output 的主要内容如下所示:
public static void main(String[] args) {
List<String> blocks = blocks("Hello faaantastic world");
System.out.println(blocks); // => ["ll", "aaa"]
System.out.println(blocks("aaabccdeeeefaaa")); // => ["aaa", "cc", "eeee", "aaa"]
System.out.println(blocks("This is an example")); // => []
System.out.println(blocks("Another example ...")); // => [" ", "..."]
System.out.println(blocks("")); // => []
我的方法是这样的:
public static LinkedList<String> blocks(String s) {
LinkedList<String> list = new LinkedList<>();
String word = "";
for(char c : s.toCharArray()) {
if (c == ??) {
}
}
return list;
String字用于存储连续的字母。我找到了 toCharArray,用于拆分字符串的每个字符。但我对 if-block 有疑问。如果我执行 c == c+1 来检查 I 和 I 的以下字符,它不起作用...我不知道如何解决这个问题。有人能帮我吗?现在尝试解决它 2 天...
还有其他方法可以编写此代码,但要充分利用您所做的,只需缓存 prevChar 以便您可以将 c 与其进行比较。如何初始化 prevChar 很棘手,要么选择文本中不可能的内容,要么添加另一个 bool 变量以指示您正在处理第一个字符,或者查看数组以确保 prevChar 与第一个字符不同。
在这种情况下,最好使用 String::charAt
方法来检索输入字符串中的字符并将它们与前一个字符进行比较。
public static LinkedList<String> blocks(String s) {
LinkedList<String> list = new LinkedList<>();
String word = "" + s.charAt(0);
for(int i = 1, n = s.length(); i < n; i++) {
if (s.charAt(i) == s.charAt(i - 1)) {
word += s.charAt(i);
} else {
if (word.length() > 1) {
list.add(word);
}
word = "" + s.charAt(i);
}
}
if (word.length() > 1) {
list.add(word);
}
return list;
}
测试
System.out.println(blocks("Hello faaantastic worlddd"));
输出
[ll, aaa, ddd]
可以使用正则表达式查找重复字符的较短解决方案 (.)+
:
public static List<String> findConsecutive(String s) {
return Pattern.compile("(.)\1+").matcher(s).results() // Stream<MatchResult>
.map(mr -> mr.group(0)) // 0 group contains repeated characters
.collect(Collectors.toList());
}
测试
System.out.println(findConsecutive("Hello faaantastic worlddd"));
输出
[ll, , aaa, ddd]
任务是将连续的字符存储在一个列表中。 Input/Output 的主要内容如下所示:
public static void main(String[] args) {
List<String> blocks = blocks("Hello faaantastic world");
System.out.println(blocks); // => ["ll", "aaa"]
System.out.println(blocks("aaabccdeeeefaaa")); // => ["aaa", "cc", "eeee", "aaa"]
System.out.println(blocks("This is an example")); // => []
System.out.println(blocks("Another example ...")); // => [" ", "..."]
System.out.println(blocks("")); // => []
我的方法是这样的:
public static LinkedList<String> blocks(String s) {
LinkedList<String> list = new LinkedList<>();
String word = "";
for(char c : s.toCharArray()) {
if (c == ??) {
}
}
return list;
String字用于存储连续的字母。我找到了 toCharArray,用于拆分字符串的每个字符。但我对 if-block 有疑问。如果我执行 c == c+1 来检查 I 和 I 的以下字符,它不起作用...我不知道如何解决这个问题。有人能帮我吗?现在尝试解决它 2 天...
还有其他方法可以编写此代码,但要充分利用您所做的,只需缓存 prevChar 以便您可以将 c 与其进行比较。如何初始化 prevChar 很棘手,要么选择文本中不可能的内容,要么添加另一个 bool 变量以指示您正在处理第一个字符,或者查看数组以确保 prevChar 与第一个字符不同。
在这种情况下,最好使用 String::charAt
方法来检索输入字符串中的字符并将它们与前一个字符进行比较。
public static LinkedList<String> blocks(String s) {
LinkedList<String> list = new LinkedList<>();
String word = "" + s.charAt(0);
for(int i = 1, n = s.length(); i < n; i++) {
if (s.charAt(i) == s.charAt(i - 1)) {
word += s.charAt(i);
} else {
if (word.length() > 1) {
list.add(word);
}
word = "" + s.charAt(i);
}
}
if (word.length() > 1) {
list.add(word);
}
return list;
}
测试
System.out.println(blocks("Hello faaantastic worlddd"));
输出
[ll, aaa, ddd]
可以使用正则表达式查找重复字符的较短解决方案 (.)+
:
public static List<String> findConsecutive(String s) {
return Pattern.compile("(.)\1+").matcher(s).results() // Stream<MatchResult>
.map(mr -> mr.group(0)) // 0 group contains repeated characters
.collect(Collectors.toList());
}
测试
System.out.println(findConsecutive("Hello faaantastic worlddd"));
输出
[ll, , aaa, ddd]