# Java solution "basic reverse thinking"

• I am new to Java.My code is very simple and easy to understand.Everyone could understand even without any comments.
When I first saw this problem,I noticed that "IV" equals 4 and "VI" equals 6.Then I wondered if there is a "center-number" in a roman numeral(this "center-number" minus its left and add its right),but then i realized this is nonsense...
After that,I split roman numeral into 2 parts(take "DCCXCII" for example,"DCC" is the adding part.You just add them together."XCII" is the complex part.The biggest roman in this part is that "center-number".It minus its left part and add its right part).As a matter of fact,it will take you tons of time to find this "center-number"...
I kept thinking about how to simplify this algorithm for about 2 hours...Then,I found something interesting:you just need to read roman numeral from right to left.if the current roman is bigger than last roman,you add it.Else, you minus it.Simple as that.Here is my code:

``````class Solution {
private boolean start = true;
private int last = 0;
private int total = 0;
private String romanstring;
private Stack<Character> stack = new Stack<Character>();
public int romanToInt(String s) {
if(start){
romanstring = s;
stackPush(s);
start = false;
}
if(!stack.isEmpty()){
stackPop(stack.pop());
}
}
private void stackPush(String s){
char[] chararr = s.toCharArray();
for(int i = 0;i < chararr.length;i++){
}
}
private void stackPop(char c){
caculate(c);
if(romanstring.length() > 0){
romanstring = romanstring.substring(0, romanstring.length() - 1);
}
romanToInt(romanstring);
}
private void caculate(char c){
int current = 0;
switch(c){
case 'I':
current = 1;
break;
case 'V':
current = 5;
break;
case 'X':
current = 10;
break;
case 'L':
current = 50;
break;
case 'C':
current = 100;
break;
case 'D':
current = 500;
break;
case 'M':
current = 1000;
break;
}
if(current >= last) total += current;
else total -= current;
last = current;
}
}
``````

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