글을 읽는 동안 코드를 삭제했지만, 개념적으로 개발 된 답변이 있습니다.
모든 문자를 입력 할 때 스택에 넣기를 원합니다.
언급 한 독특한 시나리오 14
은 두 문자로 고유합니다.
그래서 마지막 문자가 숫자 인 경우 추적하는 것이 좋습니다.
대략적인 의사입니다. 이 스택을 지원하려면 스택이 모든 문자열이어야합니다.
//unique case for digit
if(s.charAt(0).isDigit()) {
//check to see if the String at the top of a stack is a number by peeking at its first character
if(stack.peek().charAt(0).isDigit()) {
int i = Integer.parseInt(stack.pop()) * 10;
//we want to increment the entire String by 10, so a 1 -> 10
i = i + Character.getNumericValue(s.charAt(0)); //add the last digit, so 10 + 4 = 14
stack.push(Integer.toString(i)); //put the thing back on the stack
}
else {
//handle normally
stack.push(s.substring(0,1));
}
}
tostring을 (를) 대체 할 수 있습니까? – jgr208
14와 1과 4의 차이를 알 수 있도록 구분 기호를 추가 하시겠습니까? – Grice
예 ... yourExpessionString.toCharArray() 및 Characher.isDigit (ch)를 사용하십시오. – StackFlowed