Leetcode 68: превышен лимит памяти.JAVA

Программисты JAVA общаются здесь
Ответить
Anonymous
 Leetcode 68: превышен лимит памяти.

Сообщение Anonymous »

Я использую Java для решения Leetcode 68: проблема выравнивания текста (https://leetcode.com/problems/text-just ... scription/)
Учитывая, что 3 тестовых примера проходят хорошо, но для следующего текстового случая
words = ["спросить","не","что","ваша","страна","может","сделать","для","вас","спросить","что","вы","можете","сделать","для","вашей","страны"]
maxWidth =16
Я получаю сообщение об ошибке "Превышен лимит памяти". Может кто-нибудь помочь мне отладить эту проблему, не меняя структуру моего кода:
class Dsa{
int availspaces;
List words;
int minSpacesReq;

public Dsa(int maxWidth){
this.availspaces = maxWidth;
this.words = new ArrayList();
this.minSpacesReq = 0;
}

@Override
public String toString(){
return "words: "+ this.words + ", availspaces: "+ availspaces + ", min spaces: " + minSpacesReq;
}
}

class Solution {
public List fullJustify(String[] words, int maxWidth) {
List mappings = new ArrayList();
// each line -> list of words fit in a line, available space

int currentLine = 0;
Dsa currentMapping = null;

for(String currentWord: words){

if(currentLine !=0 && (currentMapping.availspaces - currentMapping.minSpacesReq >= currentWord.length())){
// if(currentLine !=0 && (currentMapping.availspaces - 1 >= currentWord.length())){
currentMapping.words.add(currentWord);
currentMapping.availspaces -= currentWord.length();
currentMapping.minSpacesReq = currentMapping.words.size();
}else{
// System.out.println("word:"+ currentWord);
currentMapping = new Dsa(maxWidth);
currentMapping.words.add(currentWord);
currentMapping.availspaces = maxWidth-currentWord.length();
currentMapping.minSpacesReq = 1;

mappings.add(currentMapping);
currentLine++;
}

}

/*for(Dsa mapping: mappings){
System.out.println(mapping.toString());
}*/

List result = new ArrayList();

for(Dsa mapping: mappings){
StringBuilder sb = new StringBuilder();
int minSpaces = mapping.words.size()-1;
int availableSpaces = mapping.availspaces;
boolean isLastLine = false;

/*if(currentLine-- == 1 || currentLine == 0){
isLastLine = true;
}*/
if(mappings.indexOf(mapping) == mappings.size() - 1){
isLastLine = true;
}

String lastWord = mapping.words.get(minSpaces);
int wordcount = mapping.words.size();
int currentWordCount = 0;

if(minSpaces != 0){

int mod = availableSpaces%minSpaces;
int quotient = availableSpaces/minSpaces;

if(isLastLine){
for(String word: mapping.words){
sb.append(word);
availableSpaces--;
if(availableSpaces >0){
sb.append(" ");
}
}
while(availableSpaces != 0){
sb.append(" ");
availableSpaces--;
}
}
else{
if(mod == 0){
//quotient number of spaces after each word until availspaces expire
for(String word: mapping.words){
sb.append(word);
int quot = quotient;
currentWordCount++;
if(currentWordCount != wordcount){
while(quot > 0){
sb.append(" ");
quot--;
}
}
}
}else{
for(String word: mapping.words){
int spacestoadd = quotient ;
if(mod != 0){
spacestoadd += 1;
mod--;
}
sb.append(word);
currentWordCount++;
if(currentWordCount != wordcount){
while(spacestoadd >0){
sb.append(" ");
spacestoadd--;
}
}
}
}
}
}else{
sb.append(mapping.words.get(0));
while(availableSpaces >0 ){
sb.append(" ");
availableSpaces--;
}
}

result.add(sb.toString());
}
return result;
}
}


Подробнее здесь: https://stackoverflow.com/questions/798 ... t-exceeded
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»