목록Java (133)
코딩응급실
import java.util.*; class Solution { public int solution(int n) { /* 처음 위치 0 에서 1 칸을 앞으로 점프한 다음 순간이동 하면 (현재까지 온 거리 : 1) x 2에 해당하는 위치로 이동할 수 있으므로 위치 2로 이동됩니다. 이때 다시 순간이동 하면 (현재까지 온 거리 : 2) x 2 만큼 이동할 수 있으므로 위치 4로 이동합니다. 이때 1 칸을 앞으로 점프하면 도착하므로 건전지 사용량이 2 만큼 듭니다. */ // 0 1 2 3 4 5 int ans = 0; while (n > 0) { ans += n % 2; // 5%2=1, 2%2=0, 1%2=1 (1이면 한 칸 움직인거고, 0이면 순간이동) n /= 2; // '2'=5/2, '1'=2/2..
import java.util.*; class Solution { public int[] solution(int n, String[] words) { int[] answer = new int[2]; ArrayList usedWords = new ArrayList(); for (int i=0; i 0 && words[i-1].charAt(words[i-1].length()-1) != words[i].charAt(0)) { answer[0] = i % n + 1; answer[1] = i / n + 1; break; } // 이전에 부른 단어를 또 부르진 않았는가 if (usedWords.contains(words[i])) { answer[0] = i % n + 1; answer[1] = i / n + 1;..
import java.util.*; class Solution { public int solution(String s){ Stack stack = new Stack(); for (char word : s.toCharArray()) { if (!stack.isEmpty() && word == stack.peek()) { stack.pop(); } else { stack.push(word); } } return stack.size() > 0 ? 0 : 1; } public static void main(String[] args) { String s = "baabaa"; Solution sol = new Solution(); int result = sol.solution(s); System.out.printl..
import java.util.*; class Solution { public int solution(int n) { int[] fibo = new int[n+1]; fibo[0] = 0; fibo[1] = 1; for(int i = 2; i
실패 코드다... 시간초과가 나버렸다. import java.util.*; class Solution { public int solution(int n) { int answer; int i = 1; String N = Integer.toBinaryString(n); N = N.replaceAll("0", ""); int nLen = N.length(); while (true) { int nextBig = n + i++ ; String NB = Integer.toBinaryString(nextBig); NB = NB.replaceAll("0", ""); int nbLen = NB.length(); if (nbLen == nLen) { answer = nextBig; break; } } return answ..
class Solution { public int solution(int n) { int answer = 0; int left = 1, right = 1; int sum = 1; while(left
import java.util.*; class Solution { public int[] solution(String s) { int[] answer = new int[2]; // 배열의 크기를 2로 설정합니다. String str = s; int cnt = 0; int zeroCnt = 0; while (str.length() != 1) { String temp = ""; for (int i=0; i
import java.util.*; class Solution { boolean solution(String s) { Stack stack = new Stack(); for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == '(') { stack.push('('); } else if (s.charAt(i) == ')') { if (stack.isEmpty()) { return false; } stack.pop(); } } return stack.isEmpty(); } public static void main(String[] args) { String s = "()()"; Solution sol = new Solution(); System.out.printl..