如何在单独的行上输出字符串数组,每行允许 N 个字符
How to output String array on separate lines with N amount of characters allowed per line
我参加了编码面试,并提出了这个问题。给定一串单词,将该字符串分解为单词,并将它们分行输出,每行不超过 N 个字符。给我的样本是“一件红色毛衣”。行长为 8。单词不能被分解,你必须在每行中尽可能多地放置单词。因此,对于给定的示例,“A”和“红色”将适合一条线和“毛衣”。添加到该行的字符超过了允许的长度,因此它将在新行上输出。
我首先将 String 拆分为 String []。然后创建了一个接受 String [] 和行长度的方法。我对边缘情况做了一些假设,然后遍历了 String []。这是我很困惑的地方,我可以用什么来表示单行以及如何在单行上添加子数组。我查找了可以找到所有接近总和的子数组的算法,但它似乎回收了元素,但我无法做到这一点。
这是我在 运行 时间之前得到的:
import java.util.ArrayList;
import java.util.List;
class Solution {
public static void main(String[] args) {
String sentence = "A red sweater.";
String[] words = sentence.split(" ");
Solution newSol = new Solution();
System.out.println(newSol.convertToLines(words, 8));
}
public List<String> convertToLines(String[] myArr, int bound) {
List<String> result = new ArrayList<String>();
if (myArr.length == 0) {
return result;
}
for (int i = 0; i < myArr.length; i++) {
if (myArr[i].length() > bound) {
System.out.println("One or more words is larger than your boundary");
break;
}
//This is where I got into trouble
//This doesn't account for more than 2 elements
//nor how I would output them on separate lines
if (myArr[i].length() + myArr[i].length() <= bound) {
result.add(myArr[i] + " " + myArr[i + 1]);
} else {
result.add(myArr[i]);
}
}
return result;
}
}
我到处搜索这个特定问题,但找不到。我假设有一种模式或我缺少的东西,但经过数小时的混乱,我仍然不知道从这里去哪里。我从来没有编写过类似的代码。虽然来不及面试了,但我还是很想知道这个怎么解决,以便我能从中吸取教训,继续学习。
对于最佳实践,每个单词的限制长度等常量是对常量执行的。
所以首先让我们创建一个常量变量。
final int MAX_WORDS_PER_LINE = 8;
假设您有一个包含句子的字符串数组:
String[] sentences = {"Hello World Im learning.", "Other Word here"};
所以逻辑就是这样。我们将对句子中的每个句子进行迭代。
然后是句子中的每个单词。
因此我们的代码将如下所示。
// We use StringBuilder to save the word that we need to print.
StringBuilder wordToPrint = new StringBuilder();
for (String sentence: sentences) {
// for each sentence we will save the words like this:
String[] words = sentence.split(" ");
// there we will iterate for each word on words
for(String word: words) {
// and check if length of word that we have to print + current word length is more than max words length per line.
if (wordToPrint.length() + word.length() > MAX_WORDS_PER_LINE) {
//So if its true we need to print that word and clear the word to print.
System.out.println(wordToPrint);
wordToPrint.delete(0, wordToPrint.length());
}
// So everytime we need to append an space and the current word.
wordToPrint.append(" ").append(word);
}
}
Gabriel Rouleau 的上述代码在循环中迭代时会跳过很多单词。
例如在字符串“A red sweater”中。它将循环“A”,添加到 line
变量,然后将“red”添加到 line
变量,然后迭代毛衣但不添加到 line 变量。它永远不会被重新访问以添加到下一行。
通过使用原始 for 循环语法进行迭代并在下一行需要重新访问某个单词时递减迭代索引来解决该问题。
String sentence = "A red sweater with some other stuff.";
String[] words = sentence.split(" ");
int bound = 8;
if(words.length == 0) return;
String line = "";
Boolean first = true;
for (int i = 0; i < words.length; i++) {
if(first) {
line = words[i];
first = false;
} else {
if ((line + " " + words[i]).length() < bound) {
line += " " + words[i];
} else {
System.out.println(line);
line = words[i];
first = true;
i--;
}
}
}
System.out.println(line);
此外,此代码修复了原始问题存在的问题(考虑超过 2 个元素)。
代码测试运行成功!
我参加了编码面试,并提出了这个问题。给定一串单词,将该字符串分解为单词,并将它们分行输出,每行不超过 N 个字符。给我的样本是“一件红色毛衣”。行长为 8。单词不能被分解,你必须在每行中尽可能多地放置单词。因此,对于给定的示例,“A”和“红色”将适合一条线和“毛衣”。添加到该行的字符超过了允许的长度,因此它将在新行上输出。
我首先将 String 拆分为 String []。然后创建了一个接受 String [] 和行长度的方法。我对边缘情况做了一些假设,然后遍历了 String []。这是我很困惑的地方,我可以用什么来表示单行以及如何在单行上添加子数组。我查找了可以找到所有接近总和的子数组的算法,但它似乎回收了元素,但我无法做到这一点。
这是我在 运行 时间之前得到的:
import java.util.ArrayList;
import java.util.List;
class Solution {
public static void main(String[] args) {
String sentence = "A red sweater.";
String[] words = sentence.split(" ");
Solution newSol = new Solution();
System.out.println(newSol.convertToLines(words, 8));
}
public List<String> convertToLines(String[] myArr, int bound) {
List<String> result = new ArrayList<String>();
if (myArr.length == 0) {
return result;
}
for (int i = 0; i < myArr.length; i++) {
if (myArr[i].length() > bound) {
System.out.println("One or more words is larger than your boundary");
break;
}
//This is where I got into trouble
//This doesn't account for more than 2 elements
//nor how I would output them on separate lines
if (myArr[i].length() + myArr[i].length() <= bound) {
result.add(myArr[i] + " " + myArr[i + 1]);
} else {
result.add(myArr[i]);
}
}
return result;
}
}
我到处搜索这个特定问题,但找不到。我假设有一种模式或我缺少的东西,但经过数小时的混乱,我仍然不知道从这里去哪里。我从来没有编写过类似的代码。虽然来不及面试了,但我还是很想知道这个怎么解决,以便我能从中吸取教训,继续学习。
对于最佳实践,每个单词的限制长度等常量是对常量执行的。
所以首先让我们创建一个常量变量。
final int MAX_WORDS_PER_LINE = 8;
假设您有一个包含句子的字符串数组:
String[] sentences = {"Hello World Im learning.", "Other Word here"};
所以逻辑就是这样。我们将对句子中的每个句子进行迭代。 然后是句子中的每个单词。
因此我们的代码将如下所示。
// We use StringBuilder to save the word that we need to print.
StringBuilder wordToPrint = new StringBuilder();
for (String sentence: sentences) {
// for each sentence we will save the words like this:
String[] words = sentence.split(" ");
// there we will iterate for each word on words
for(String word: words) {
// and check if length of word that we have to print + current word length is more than max words length per line.
if (wordToPrint.length() + word.length() > MAX_WORDS_PER_LINE) {
//So if its true we need to print that word and clear the word to print.
System.out.println(wordToPrint);
wordToPrint.delete(0, wordToPrint.length());
}
// So everytime we need to append an space and the current word.
wordToPrint.append(" ").append(word);
}
}
Gabriel Rouleau 的上述代码在循环中迭代时会跳过很多单词。
例如在字符串“A red sweater”中。它将循环“A”,添加到 line
变量,然后将“red”添加到 line
变量,然后迭代毛衣但不添加到 line 变量。它永远不会被重新访问以添加到下一行。
通过使用原始 for 循环语法进行迭代并在下一行需要重新访问某个单词时递减迭代索引来解决该问题。
String sentence = "A red sweater with some other stuff.";
String[] words = sentence.split(" ");
int bound = 8;
if(words.length == 0) return;
String line = "";
Boolean first = true;
for (int i = 0; i < words.length; i++) {
if(first) {
line = words[i];
first = false;
} else {
if ((line + " " + words[i]).length() < bound) {
line += " " + words[i];
} else {
System.out.println(line);
line = words[i];
first = true;
i--;
}
}
}
System.out.println(line);
此外,此代码修复了原始问题存在的问题(考虑超过 2 个元素)。
代码测试运行成功!