Integer.parseInt(str) : int 형 반환Integer.valueOf(str) :Integer 객체 반환String.valueOf(num)Integer.toString(num)StringBuffer : 스레드 안전성이 있어서 내부적으로 동기화 키워드가 적용
StringBuilder : 스레드 안전성이 없지만 속도가 빠름→메서드 안에서 객체를 생성하고 쓰고 버리는 지역 변수면 거의 무관
// StringBuilder
String answer = new StringBuilder(my_string).reverse().toString();
// StringBuffer
StringBuffer sb = new StringBuffer(my_string);
String answer = sb.reverse().toString();
class Solution {
public int solution(String str1, String str2) {
int answer = (str1.contains(str2))? 1 : 2;
return answer;
}
}class Solution {
public String solution(String my_string, int n) {
String answer = "";
for(char c : my_string.toCharArray()){
for(int i = 0; i < n; i++){
answer += c;
}
}
return answer;
}
}class Solution {
public String solution(String my_string, String letter) {
String answer = my_string.replace(letter, "");
return answer;
}
}class Solution {
public String solution(String my_string) {
String answer = "";
for(char c : my_string.toCharArray()){
if(Character.isUpperCase(c)){
answer += Character.toLowerCase(c);
}
else{
answer += Character.toUpperCase(c);
}
}
return answer;
}
}class Solution {
public int[] solution(int numer1, int denom1, int numer2, int denom2) {
int[] answer = new int[2];
int num = numer1 * denom2 + numer2 * denom1;
int den = denom1 * denom2;
int g = gcd(num, den);
answer[0] = num / g;
answer[1] = den / g;
return answer;
}
private int gcd(int a, int b){
while(b != 0){
int tmp = a % b;
a = b;
b = tmp;
}
return a;
}
}class Solution {
public int solution(int[] array) {
int answer = 0;
int[] cnt = new int[1001];
for(int num : array){
cnt[num]++;
}
int maxCnt = 0;
for(int i = 0; i < cnt.length; i++){
if(maxCnt < cnt[i]){
maxCnt = cnt[i];
answer = i;
}
else if(maxCnt == cnt[i]){
answer = -1;
}
}
return answer;
}
}class Solution {
public int solution(String[] babbling) {
int answer = 0;
String[] str = { "aya", "ye", "woo", "ma" };
for(String s : babbling){
for(String st : str){
if(s.contains(st)){
s = s.replace(st, " ");
}
}
s = s.replace(" ", "");
if(s.equals("")) answer++;
}
return answer;
}
}class Solution {
public int solution(int[][] dots) {
if(comp(dots[0], dots[1], dots[2], dots[3]))
return 1;
if(comp(dots[0], dots[2], dots[1], dots[3]))
return 1;
if(comp(dots[0], dots[3], dots[1], dots[2]))
return 1;
return 0;
}
private boolean comp(int[] dot1, int[] dot2, int[] dot3, int[] dot4){
int nx1 = dot1[0] - dot2[0];
int ny1 = dot1[1] - dot2[1];
int nx2 = dot3[0] - dot4[0];
int ny2 = dot3[1] - dot4[1];
return (ny1 * nx2) == (ny2 * nx1);
}
}class Solution {
public int solution(int[][] lines) {
int answer = 0;
for(int i = -100; i <= 100; i++){
int cnt = 0;
for(int j = 0; j < lines.length; j++){
int a = lines[j][0], b = lines[j][1];
if(a <= i && b > i) cnt++;
}
if(cnt >= 2){
answer++;
}
}
return answer;
}
}class Solution {
public int[][] solution(int n) {
int[][] answer = new int[n][n];
int[] dr = {0, 1, 0, -1};
int[] dc = {1, 0, -1, 0};
int dir = 0, r = 0, c = 0;
for(int i = 1; i <= n * n; i++){
answer[r][c] = i;
int nr = r + dr[dir], nc = c + dc[dir];
if(nr < 0 || nc < 0 || nr >= n || nc >= n || answer[nr][nc] != 0){
dir = (dir + 1) % 4;
nr = r + dr[dir];
nc = c + dc[dir];
}
r = nr;
c = nc;
}
return answer;
}
}import java.util.HashMap;
import java.util.Map;
class Solution {
public int solution(int a, int b, int c, int d) {
int answer = 0;
Map<Integer, Integer> map = new HashMap<>();
int[] nums = {a, b, c, d};
for(int num : nums){
map.put(num, map.getOrDefault(num, 0) + 1);
}
if(map.size() == 1){
answer = a * 1111;
}
else if(map.size() == 2){
int p = 0, q = 0;
for(Integer key : map.keySet()){
if(map.get(key) == 3){
p = key;
if(q != 0)
answer = (int)Math.pow((10 * p + q), 2);
}
else if(map.get(key) == 2){
if(p == 0) p = key;
else{
q = key;
answer = (p + q) * Math.abs(p - q);
}
}
else if((map.get(key) == 1)){
q = key;
if(p != 0)
answer = (int)Math.pow((10 * p + q), 2);
}
}
}
else if(map.size() == 3){
int p = 0, q = 0, r = 0;
for(Integer key : map.keySet()){
if(map.get(key) == 2){
p = key;
}
else if((map.get(key) == 1)){
if(q == 0) q = key;
else{
r = key;
answer = q * r;
}
}
}
}
else{
answer = Math.min(a, Math.min(b, Math.min(c, d)));
}
return answer;
}
}
// 개선
import java.util.*;
class Solution {
public int solution(int a, int b, int c, int d) {
Map<Integer, Integer> map = new HashMap<>();
for (int n : new int[]{a, b, c, d}) map.put(n, map.getOrDefault(n, 0) + 1);
List<Integer> keys = new ArrayList<>(map.keySet());
keys.sort((k1, k2) -> map.get(k2) - map.get(k1));
switch (map.size()) {
case 1:
return 1111 * a;
case 2:
int p = keys.get(0), q = keys.get(1);
if (map.get(p) == 3) {
return (int) Math.pow(10 * p + q, 2);
} else {
return (p + q) * Math.abs(p - q);
}
case 3:
return keys.get(1) * keys.get(2);
default:
return Collections.min(map.keySet());
}
}
}class Solution {
public int solution(int[][] board) {
int answer = 0;
int n = board.length;
int[][] b = new int[n][n];
int[] dx = {-1, 1, 0, 0, -1, 1, 1, -1}, dy = {0, 0, -1, 1, -1, 1, -1, 1};
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(board[i][j] == 1){
b[i][j] = 1;
for(int k = 0; k < 8; k++){
int nx = dx[k] + i;
int ny = dy[k] + j;
if(nx < 0 || ny < 0 || nx >= n || ny >= n) continue;
b[nx][ny] = 1;
}
}
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(b[i][j] == 0) answer++;
}
System.out.println();
}
return answer;
}
}class Solution {
public int[] solution(int num, int total) {
int[] answer = new int[num];
int mid = total / num;
int mid_idx = (num % 2 == 0)? (num / 2 - 1) : (num / 2);
for(int i = 0; i < num; i++){
answer[i] = mid + (i - mid_idx);
}
return answer;
}
}class Solution {
public int solution(int[] common) {
int answer = 0;
if(common[1] - common[0] == common[2] - common[1]){
int d = common[1] - common[0];
answer = common[common.length - 1] + d;
}
else{
int r = common[1] / common[0];
answer = common[common.length - 1] * r;
}
return answer;
}
}class Solution {
public String[] solution(String[] quiz) {
String[] answer = new String[quiz.length];
int idx = 0;
for(String str : quiz){
String[] parts = str.split(" ");
int x = Integer.parseInt(parts[0]);
int y = Integer.parseInt(parts[2]);
int z = Integer.parseInt(parts[4]);
String op = parts[1];
if(op.equals("+")){
if(x + y == z) answer[idx++] = "O";
else answer[idx++] = "X";
}
else{
if(x - y == z) answer[idx++] = "O";
else answer[idx++] = "X";
}
}
return answer;
}
}import java.util.*;
class Solution {
public int[] solution(int[] numlist, int n) {
int[] answer = new int[numlist.length];
ArrayList<Integer> list = new ArrayList<>();
for(int num : numlist) {
list.add(num);
}
Collections.sort(list, (a, b) -> {
int distA = Math.abs(a - n);
int distB = Math.abs(b - n);
if (distA == distB) return b - a;
return distA - distB;
});
for(int i = 0; i < list.size(); i++) {
answer[i] = list.get(i);
}
return answer;
}
}class Solution {
public String solution(String code) {
String answer = "";
int mode = 0;
for(int i = 0; i < code.length(); i++){
if(code.charAt(i) == '1'){
if(mode == 1) mode = 0;
else mode = 1;
}
else if(mode == 1 && i % 2 == 1){
answer += code.charAt(i);
}
else if(mode == 0 && i % 2 == 0){
answer += code.charAt(i);
}
}
if(answer.equals("")) answer = "EMPTY";
return answer;
}
}
// 개선
class Solution {
public String solution(String code) {
StringBuilder answer = new StringBuilder();
int mode = 0;
for(int i = 0; i < code.length(); i++){
if(code.charAt(i) == '1'){
mode = 1 - mode;
}
else if(mode == 1 && i % 2 == 1){
answer.append(code.charAt(i));
}
else if(mode == 0 && i % 2 == 0){
answer.append(code.charAt(i));
}
}
String ans = answer.toString();
if(ans.equals("")) ans = "EMPTY";
return ans;
}
}