Я использую 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
Leetcode 68: превышен лимит памяти. ⇐ JAVA
Программисты JAVA общаются здесь
1763233892
Anonymous
Я использую Java для решения Leetcode 68: проблема выравнивания текста (https://leetcode.com/problems/text-justification/description/)
Учитывая, что 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;
}
}
Подробнее здесь: [url]https://stackoverflow.com/questions/79821058/leetcode-68-memory-limit-exceeded[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия