ํฐ์คํ ๋ฆฌ ๋ทฐ
Algorithm/Programmers
[Java] ํ๋ก๊ทธ๋๋จธ์ค - ์์ ์ต๋ํ
๊ฐ๋ฐ๊ฐ๊ตด๐ธ 2022. 7. 3. 19:24[๋ฌธ์ ]
https://programmers.co.kr/learn/courses/30/lessons/67257
[ํ์ด]
์กฐํฉ๊ณผ ๋ฐฑํธ๋ํน์ ์ด์ฉํ์ฌ ํ์ดํ์์ต๋๋ค.
์ฐ์ ํ์ํ ์๋ฃ๋ค์ ์ธํ ํฉ๋๋ค.
- ArrayList<String> exp๋ก ์ค๋ณต์ ์ ๊ฑฐํ๋ฉฐ ์ฐ์ฐ์๋ฅผ ๋ฃ์ด์ฃผ์์ต๋๋ค.
- (Set ์๋ฃํ์ ์ด์ฉํ๋ฉด ํธ๋ฆฌํ์ง๋ง, ํ์ด ๋น์์๋ ์๊ฐ์ ๋ชปํ์ต๋๋ค.ใ ใ )
- expression.charAt(i) < 48๋ฅผ ํตํด ์ฐ์ฐ์๋ฅผ ๊ตฌ๋ถํ์์ต๋๋ค.
- ( ์์คํค ์ฝ๋ ์ด์ฉ )
- ์ฐ์ฐ์๋ค์ numbers์ " "์ผ๋ก ๊ตฌ๋ถํ์ฌ ์ ์ฅํ๊ณ , ์ซ์๋ค์ operator์ ์ ์ฅํ์์ต๋๋ค.
- ์ฐ์ฐ์ : “*+-” , ์ซ์ : “10 20 300”
๋ค์์ผ๋ก setNumber()๋ฅผ ํตํด ํ์ด๋ฅผ ์งํํ์์ต๋๋ค.
- ์ฐ์ฐ์๋ค์ ์ฐ์ ์์๊ฐ ๋๋ ์์์์ ๊ตฌํด์ calcuate()๋ฅผ ํตํด ํด๋น ์ฐ์ฐ์๋ฅผ ๋ชจ๋ ์ํ์์ผ ๊ณ์ฐํ ๊ฒฐ๊ณผ๋ฅผ numbers์ ์ ์ฅํ๊ณ operator์ ๊ณ์ฐํ ์ฐ์ฐ์ ์ธ์ ์ฐ์ฐ์๋ค์ ์๋ก ์ ์ฅํ์ฌ setNumber() ์ฌ๊ท๋ฅผ ํตํด ๋ชจ๋ ์ฐ์ฐ์์ผฐ์ต๋๋ค.
[์ฝ๋]
import java.util.*;
class Solution {
static ArrayList<String> exp = new ArrayList<String>();
static long max = Long.MIN_VALUE;
public long solution(String expression) {
long answer = 0;
StringBuffer sb = new StringBuffer();
sb.append(expression);
String operator = "";
for(int i=0;i<expression.length();i++) {
if(expression.charAt(i) < 48) {
String s = "" + expression.charAt(i);
operator += s;
if(!exp.contains(s)) {
exp.add(s);
}
sb.replace(i, i+1, " ");
}
}
String numbers = sb.toString();
boolean[] visit = new boolean[exp.size()];
setNumber("", visit, 0, numbers, operator);
answer = max;
return answer;
}
// ์์์ ๊ตฌํ๊ธฐ
public static void setNumber(String str, boolean[] visit, int cnt, String numbers, String operator) {
if(cnt >= exp.size()) {
Long absNum = Math.abs(Long.parseLong(numbers));
if(max < absNum) {
max = absNum;
}
return;
}
for(int i=0;i<exp.size();i++) {
if(!visit[i]) {
visit[i] = true;
String[] nums = numbers.split(" ");
String[] opers = operator.split("");
String newNums = "";
String newOpers = "";
for(int j=0;j<nums.length-1;j++) {
if(opers[j].equals(exp.get(i))) {
String n = calculate(nums[j], nums[j+1], opers[j]);
nums[j+1] = n;
} else {
newNums = newNums + nums[j] + " ";
newOpers += opers[j];
}
}
newNums = newNums + nums[nums.length-1];
setNumber(str + exp.get(i), visit, cnt+1, newNums, newOpers);
visit[i] = false;
}
}
}
public static String calculate(String a, String b, String o) {
long result = 0L;
if(o.equals("+")) {
result = Long.parseLong(a) + Long.parseLong(b);
} else if(o.equals("-")) {
result = Long.parseLong(a) - Long.parseLong(b);
} else if(o.equals("*")) {
result = Long.parseLong(a) * Long.parseLong(b);
} else {
result = Long.parseLong(a) / Long.parseLong(b);
}
return Long.toString(result);
}
}
'Algorithm > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] ํ๋ก๊ทธ๋๋จธ์ค - ๋น๋ฐ์ง๋ (0) | 2022.07.07 |
---|---|
[Java] ํ๋ก๊ทธ๋๋จธ์ค - ๋ณด์ ์ผํ (0) | 2022.07.03 |
[Java] ํ๋ก๊ทธ๋๋จธ์ค - ํคํจ๋ ๋๋ฅด๊ธฐ (0) | 2022.07.03 |
[Java] ํ๋ก๊ทธ๋๋จธ์ค - ์ถ์ ํธ๋ํฝ (0) | 2022.06.13 |
[Java] ํ๋ก๊ทธ๋๋จธ์ค - ์๋ฌผ์ ์ ์ด์ (0) | 2022.06.13 |
๊ณต์ง์ฌํญ
์ต๊ทผ์ ์ฌ๋ผ์จ ๊ธ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ
- Total
- Today
- Yesterday
๋งํฌ
TAG
- ํ๋ก๊ทธ๋๋จธ์ค
- TDD
- http
- ์ด์์ฒด์
- fp
- ์๋ฐ์คํฌ๋ฆฝํธ
- ํจ์ํ ํ๋ก๊ทธ๋๋ฐ
- ์นด์นด์ค ์ธํด
- ์ ์ญ ๋ณ์
- ํฌํฌ์ธํฐ
- ์๊ณ ๋ฆฌ์ฆ
- ๋ฐฑ์ค
- Baekjoon
- ๋ฐฑ์ค node.js
- ๋คํธ์ํฌ
- ๋ ์์ปฌ ํ๊ฒฝ
- ์๋ฐ
- ํ๋กํผํฐ
- git
- 2019 ์นด์นด์ค ๊ฐ๋ฐ์ ๊ฒจ์ธ ์ธํด
- ํ๋กํ ์ฝ
- ์ด๋ถํ์
- ์ฝ๋ฉํ ์คํธ
- JavaScript
- ๋์์ธ ํจํด
- ๊ฐ์ฒด์งํฅ ํ๋ก๊ทธ๋๋ฐ
- ๋ฐฑ์ค javascript
- ๋ค์ด๋๋ฏน ํ๋ก๊ทธ๋๋ฐ
- map
- ๋ชจ๋ ์๋ฐ์คํฌ๋ฆฝํธ deep dive
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
๊ธ ๋ณด๊ดํจ