# Memory Limit Exceeded

• I see that my code is not pretty but I still don't understand how could MLE occur when I only declared 2 variables? The for and while loops might inefficiently eat CPU cycles but they should have nothing to do with memory? Can someone help out? Thanks!

``````class Solution {
public:
string intToRoman(int num) {
string roman;
int cnt;
//output Ms
while (num >= 1000 && num < 3999) {
roman += 'M';
num -= 1000;
}

//output Ds
while (num >= 500 && num < 1000) {
cnt ++;
num -= 500;
}
if (cnt == 4) {
roman += "DM";
}
else {
for (int i = 0; i < cnt; i ++) {
roman += 'D';
}
}
cnt = 0;

//output Cs
while (num >= 100 && num < 501) {
cnt ++;
num -= 100;
}
if (cnt == 4) {
roman += "CD";
}
else {
for (int i = 0; i < cnt; i ++) {
roman += 'C';
}
}
cnt = 0;

//output Ls
while (num >= 50 && num < 100) {
cnt ++;
num -= 50;
}
if (cnt == 4) {
roman += "LC";
}
else {
for (int i = 0; i < cnt; i ++) {
roman += 'L';
}
}
cnt = 0;

//output Xs
while (num >= 10 && num < 50) {
cnt ++;
num -= 10;
}
if (cnt == 4) {
roman += "XL";
}
else {
for (int i = 0; i < cnt; i ++) {
roman += 'X';
}
}
cnt = 0;

//output Vs
while (num >= 5 && num < 10) {
cnt ++;
num -= 5;
}
if (cnt == 4) {
roman += "VX";
}
else {
for (int i = 0; i < cnt; i ++) {
roman += 'X';
}
}
cnt = 0;

//output Is
while (num > 0 && num < 5) {
cnt ++;
num -= 1;
}
if (cnt == 4) {
roman += "IV";
}
else {
for (int i = 0; i < cnt; i ++) {
roman += 'I';
}
}
return roman;
}
};``````

• I think your interval might be wrong.
Here is my code.
It is ugly. But I divide the interval like [1,4),[4,5),[5,9)......

public class Solution {
public String intToRoman(int num) {
// Symbol Value
// I 1 -
// V 5
// X 10 -
// L 50
// C 100 -
// D 500
// M 1,000
StringBuffer temp = new StringBuffer("");
while(num!=0){
if(num>=1000){
temp.append(this.charAtValue(num));
num-=1000;
}else if(num>=900 && num<1000){
temp.append(this.charAtValue(num));
num-=900;
}else if(num>=500 && num<900){
temp.append(this.charAtValue(num));
num-=500;
}else if(num>=400 && num<500){
temp.append(this.charAtValue(num));
num-=400;
}else if(num>=100 && num<400){
temp.append(this.charAtValue(num));
num-=100;
}else if(num>=90 && num<100){
temp.append(this.charAtValue(num));
num-=90;
}else if(num>=50 && num<90){
temp.append(this.charAtValue(num));
num-=50;
}else if(num>=40 && num<50){
temp.append(this.charAtValue(num));
num-=40;
}else if(num>=10 && num<40){
temp.append(this.charAtValue(num));
num-=10;
}else if(num>=9 && num<10){
temp.append(this.charAtValue(num));
num-=9;
}else if(num>=5 && num<9){
temp.append(this.charAtValue(num));
num-=5;
}else if(num>=4 && num<5){
temp.append(this.charAtValue(num));
num-=4;
}else{//num>=1 && num<4
temp.append(this.charAtValue(num));
num-=1;
}
}
return temp.toString();
}

``````private StringBuffer charAtValue(int number){
if(number>=1000)return new StringBuffer("M");
else if (number>=900 && number<1000)return new StringBuffer("CM");
else if (number>=500 && number<900)return new StringBuffer("D");
else if (number>=400 && number<500)return new StringBuffer("CD");
else if (number>=100 && number<400)return new StringBuffer("C");
else if (number>=90 && number<100)return new StringBuffer("XC");
else if (number>=50 && number<90)return new StringBuffer("L");
else if (number>=40 && number<50)return new StringBuffer("XL");
else if (number>=10 && number<40)return new StringBuffer("X");
else if (number>=9 && number<10)return new StringBuffer("IX");
else if (number>=5 && number<9)return new StringBuffer("V");
else if (number>=4 && number<5)return new StringBuffer("IV");
else if (number>=1 && number<4)return new StringBuffer("I");
else return new StringBuffer("I");
}
``````

}

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.