https://school.programmers.co.kr/learn/courses/30/lessons/67257


[제한 사항]
[입출력 예]
| expression | result |
|---|---|
| "100-200*300-500+20" | 60420 |
| "506-32" | 300 |
경우의 수가 많지 않을때는 직접 모든 경우의 수를 나열하는 것이 빠를 수 있음.
StringTokenizer st = new StringTokenizer(expression, "-+*", true); while(st.hasMoreTokens()) { list.add(st.nextToken()); }
📌계산하는 부분에서 i -= 2를 하는 이유
ex) 10 + 20 - 100
여기서 연산자 +를 만난 부분이 i(=1번째 인덱스)이라고 하면 remove i - 1을 3번 해서 결과를 i - 1에 넣어주면
30 - 100이 되고, 이때 i는 여전히 1번째 인덱스이다. 위 for문을 돌아가서 i++ 을 해주기 때문에 따라서 i - 2를 해주고, 다시 30부터 가리키도록 설정하는 것이다.for(String opS : op){ for(int i = 0; i < calList.size(); i++){ if(calList.get(i).equals(opS)){ long resultTmp = calculate(calList.remove(i-1), calList.remove(i-1), calList.remove(i-1)); calList.add(i-1, String.valueOf(resultTmp)); i -= 2; } } }
전체 경우를 DFS로 고려하여 풀이.
import java.util.*;
class Solution {
String[][] operator = {{"*", "+", "-"}, {"*", "-", "+"},
{"+", "*", "-"}, {"+", "-", "*"},
{"-", "*", "+"}, {"-", "+", "*"}};
public long solution(String expression) {
StringTokenizer st = new StringTokenizer(expression, "*+-", true);
ArrayList<String> list = new ArrayList<>();
while(st.hasMoreTokens()){
list.add(st.nextToken());
}
long max = 0;
for(String op[] : operator){
long result = calculate(new ArrayList<>(list), op);
if(max < result){
max = result;
}
}
return max;
}
public long calculate(ArrayList<String> calList, String op[]){
for(String opS : op){
for(int i = 0; i < calList.size(); i++){
if(calList.get(i).equals(opS)){
long resultTmp = calculate(calList.remove(i-1), calList.remove(i-1), calList.remove(i-1));
calList.add(i-1, String.valueOf(resultTmp));
i -= 2;
}
}
}
return Math.abs(Long.parseLong(calList.get(0)));
}
public long calculate(String a, String c, String b){
long al = Long.parseLong(a);
long bl = Long.parseLong(b);
if(c.equals("+")){
return al + bl;
} else if(c.equals("-")){
return al - bl;
} else{
return al * bl;
}
}
}
import java.util.*;
class Solution {
static char opers [] = {'+', '-', '*'};
static int check [] = new int [3];
static ArrayList<Long> Numbers = new ArrayList<>();
static ArrayList<Character> Ops = new ArrayList<>();
static ArrayList<Long> Answer = new ArrayList<>();
public long solution(String expression) {
long answer = 0;
String add_num = ""; // charAt으로 100 = 1 + 0 + 0
for(int ex = 0; ex < expression.length(); ex++) {
if(expression.charAt(ex) >= '0' && expression.charAt(ex) <= '9') {
add_num = add_num + expression.charAt(ex);
}
else {
Numbers.add(Long.parseLong(add_num)); // 숫자 넣기
Ops.add(expression.charAt(ex)); // 연산자 집어넣어주기
add_num = ""; // 문자열을 초기화 해줘야 다음 숫자를 받는다
}
}
Numbers.add(Long.parseLong(add_num)); // 맨 마지막 숫자 넣기.
dfs(0, new char [3]);
answer = Collections.max(Answer);
return answer;
}
public static void dfs(int count, char[] p) {
if(count == 3) {
ArrayList<Long> tmp_Numbers = new ArrayList<>(Numbers);
ArrayList<Character> tmp_Ops = new ArrayList<>(Ops);
for(int pp = 0; pp < p.length; pp++) {
for(int i = 0; i < tmp_Ops.size(); i++) {
// 연산자 배열안에 있는게 지금 우선순위에 있는거랑 같다면
if(tmp_Ops.get(i) == p[pp]) {
Long tmp_result = Cal(tmp_Numbers.remove(i), tmp_Numbers.remove(i), p[pp]);
tmp_Numbers.add(i, tmp_result);
tmp_Ops.remove(i); // 연산자 제거
i--;
}
}
}
Answer.add(Math.abs(tmp_Numbers.get(0))); // 절대값 변환.
return; // 종료.
}
for(int i = 0; i < opers.length; i++) {
if(check[i] == 0) { // 아직 방문을 하지 않는 곳이면
check[i] = 1; // 방문 체크
p[count] = opers[i];
dfs(count+1, p);
check[i] = 0; // 체크 해제
}
}
}
public static Long Cal(Long a, Long b, char c) {
long jump_this_ops = 0;
if(c == '+')
return a + b;
else if(c == '-')
return a - b;
else if(c == '*')
return a * b;
else
return jump_this_ops;
}
}