๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ’ปTech/โ˜•Java18

[Java] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋‹จ์–ด ๋ณ€ํ™˜ ๐Ÿ“„ ๋ฌธ์ œ ๋‹จ์–ด ๋ณ€ํ™˜ ๐Ÿ’ก ํ’€์ด BFS(๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰) ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜์—ฌ "begin" ๋ฌธ์ž์—ด์—์„œ "target" ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ์ตœ์†Œ ๋‹จ๊ณ„ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. import java.util.*; class Solution { public int solution(String begin, String target, String[] words) { int answer = 0; // ์ตœ์†Œ ๋‹จ๊ณ„ ์ˆ˜๋ฅผ ์ €์žฅํ•  ๋ณ€์ˆ˜ boolean[] visited = new boolean[words.length]; // ๋‹จ์–ด ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ ์ถ”์ ํ•˜๋Š” ๋ฐฐ์—ด Queue queue = new LinkedList(); // BFS ํƒ์ƒ‰์„ ์œ„ํ•œ ํ queue.offer(begin); // ์‹œ์ž‘ ๋‹จ์–ด๋ฅผ ํ์— ์ถ”๊ฐ€ while (!queu.. 2023. 10. 4.
[Java] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๊ฒŒ์ž„ ๋งต ์ตœ๋‹จ๊ฑฐ๋ฆฌ ๐Ÿ“„ ๋ฌธ์ œ ๊ฒŒ์ž„ ๋งต ์ตœ๋‹จ๊ฑฐ๋ฆฌ ๐Ÿ’ก ํ’€์ด BFS(๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰) ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜์—ฌ ๋งต์„ ํ†ต๊ณผ ๊ฐ€๋Šฅํ•œ ๋ฐฉ์•ˆ๋“ค์„ ํƒ์ƒ‰ํ•˜๊ณ , ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ์ฐพ์•„ answer ๋ณ€์ˆ˜์— ์ €์žฅํ•œ ํ›„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. import java.util.*; class Solution { public int solution(int[][] maps) { int answer = 0; int n = maps.length; int m = maps[0].length; // ์ด๋™ ๊ฐ€๋Šฅํ•œ ๋ฐฉํ–ฅ (์ƒ,ํ•˜,์šฐ,์ขŒ) int[][] directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; Queue queue = new LinkedList(); queue.offer(new int[]{0, 0, 1}); // ์‹œ์ž‘ ์œ„์น˜ (0, 0)์—์„œ ์ถœ.. 2023. 9. 28.
[Java] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋„คํŠธ์›Œํฌ ๐Ÿ“„ ๋ฌธ์ œ ๋„คํŠธ์›Œํฌ ๐Ÿ’ก ํ’€์ด DFS(๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰) ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜์—ฌ ๋„คํŠธ์›Œํฌ๋ฅผ ํƒ์ƒ‰ํ•˜๊ณ , ์—ฐ๊ฒฐ๋œ ๊ทธ๋ฃน์„ ์ฐพ์•„ answer ๋ณ€์ˆ˜์— ์ €์žฅํ•œ ํ›„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. class Solution { public int solution(int n, int[][] computers) { boolean[] visited = new boolean[n]; // ์ปดํ“จํ„ฐ ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐฐ์—ด int answer = 0; // ๋„คํŠธ์›Œํฌ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๋Š” ๋ณ€์ˆ˜ for (int i = 0; i < n; i++) { if (!visited[i]) { // ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ์ปดํ“จํ„ฐ๋ผ๋ฉด dfs(i, computers, visited); // DFS๋กœ ์—ฐ๊ฒฐ๋œ ๋ชจ๋“  ์ปดํ“จํ„ฐ๋ฅผ ๋ฐฉ๋ฌธ answer++; // ๋„คํŠธ์›Œํฌ ๊ฐœ์ˆ˜ ์ฆ๊ฐ€ } } return.. 2023. 9. 26.
[Java] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ํƒ€๊ฒŸ ๋„˜๋ฒ„ ๐Ÿ“„ ๋ฌธ์ œ ํƒ€๊ฒŸ ๋„˜๋ฒ„ ๐Ÿ’ก ํ’€์ด ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰ (DFS)์„ ํ™œ์šฉํ•˜์—ฌ ์ฃผ์–ด์ง„ ์ˆซ์ž ๋ฐฐ์—ด์—์„œ ๊ฐ ์ˆซ์ž๋ฅผ ๋”ํ•˜๊ฑฐ๋‚˜ ๋นผ์„œ ํŠน์ • ๋ชฉํ‘œ ์ˆซ์ž(target)๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. class Solution { private int answer = 0; public int solution(int[] numbers, int target) { dfs(numbers, target, 0, 0); return answer; } private void dfs(int[] numbers, int target, int index, int currentSum) { if (index == numbers.length) { // ๋ฐฐ์—ด์˜ ๋๊นŒ์ง€ ๋„๋‹ฌํ•œ ๊ฒฝ์šฐ, ํ˜„์žฌ ํ•ฉ์ด ํƒ€๊ฒŸ๊ณผ ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธํ•˜๊ณ  answer๋ฅผ ์—…๋ฐ์ด.. 2023. 9. 25.
[Java] REST API ํ˜ธ์ถœ ์ฝ”๋“œ JsonObject๋ฅผ ๋„˜๊ฒจ ๊ฒฐ๊ณผ๊ฐ’์„ ์‘๋‹ต๋ฐ›๋Š” REST API ํ˜ธ์ถœ ์ž๋ฐ” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์ด ๋ฉ”์†Œ๋“œ๋กœ ํ•˜๋‚˜ ์ •๋ฆฌํ•ด ๋‘๋ฉด ์‚ฌ์šฉ์— ์šฉ์ดํ•ฉ๋‹ˆ๋‹ค. /** * REST API ํ˜ธ์ถœ * * @param paramUrl * @param jsonObject void */ private void RestCall(String paramUrl,JSONObject jsonObject){ try { URL url = new URL(paramUrl); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("X-Auth-Token", API_KEY); conn.set.. 2019. 11. 19.
[Java] ์›ํ•˜๋Š” ํฌ๋งท์œผ๋กœ ํ˜„์žฌ ๋‚ ์งœ ์ถœ๋ ฅ ์›ํ•˜๋Š” ๋‚ ์งœ ํฌ๋งท(yyyyMMdd,yyyyMMddHH ๋“ฑ)์„ ์ž…๋ ฅํ•˜์—ฌ ํ˜„์žฌ๋‚ ์งœ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. public static String getCurrent(String format) { SimpleDateFormat sdf = new SimpleDateFormat(format); return sdf.format(Calendar.getInstance().getTimeInMillis()); } 2019. 7. 5.
[Java] ์ž๋ฃŒ๊ตฌ์กฐ,์•Œ๊ณ ๋ฆฌ์ฆ˜ ์šฉ์–ด/๊ฐœ๋… ์ •๋ฆฌ ์ž๋ฃŒ๊ตฌ์กฐ,์•Œ๊ณ ๋ฆฌ์ฆ˜ ์šฉ์–ด/๊ฐœ๋… ์ •๋ฆฌ ์ž๋ฃŒ๊ตฌ์กฐ ์ฝ”๋“œ์ƒ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ํšจ์œจ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ๊ตฌ์กฐํ™” ํ•œ ๊ฒƒ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์–ด๋–ค ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•œ ์ ˆ์ฐจ ๋˜๋Š” ๋ฐฉ๋ฒ• ๋ฐฐ์—ด(Array) ๋ฐ์ดํ„ฐ๋ฅผ ๋‚˜์—ดํ•˜๊ณ , ๊ฐ ๋ฐ์ดํ„ฐ๋ฅผ ์ธ๋ฑ์Šค์— ๋Œ€์‘ํ•˜๋„๋ก ๊ตฌ์„ฑํ•œ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ ํ(Queue) ๋จผ์ € ๋„ฃ์€ ๋ฐ์ดํ„ฐ๋ฅผ ๋จผ์ € ๊บผ๋‚ผ ์ˆ˜ ์žˆ๋Š” ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ (FIFO, LILO) ์Šคํƒ(Stack) ๋‚˜์ค‘์— ๋„ฃ์€ ๋ฐ์ดํ„ฐ๋ฅผ ๋จผ์ € ๊บผ๋‚ผ ์ˆ˜ ์žˆ๋Š” ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ (FILO, LIFO) ๋งํฌ๋“œ๋ฆฌ์ŠคํŠธ(LinkedList) ๋–จ์–ด์ ธ ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ํฌ์ธํ„ฐ๋กœ ์—ฐ๊ฒฐํ•ด์„œ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ LinkedList LinkedList๋Š” ๋ฐ์ดํ„ฐ์™€ ๋‹ค์Œ ๋…ธ๋“œ์˜ ์ฃผ์†Œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”(ํฌ์ธํ„ฐ) ๊ตฌ์กฐ ArrayList์™€ ๊ฐ™์ด ๋ฐ์ดํ„ฐ์˜ ์ถ”๊ฐ€,์‚ญ์ œ์‹œ ๋ถˆํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ์˜ ๋ณต์‚ฌ๊ฐ€ ์—†์–ด ๋ฐ์ดํ„ฐ.. 2019. 6. 20.
[Java] ๋ฌธ์ž์—ด ๊ณต๋ฐฑ ์ฒดํฌ ์ฝ”๋“œ private static boolean isBlank(String str) { int strLen; if (str == null || (strLen = str.length()) == 0) { return true; } for (int i = 0; i < strLen; i++) { if ((!Character.isWhitespace(str.charAt(i)))) { return false; } } return true; } 2019. 6. 13.