String bin_num1 = ""
bin_num1 = Integer.toBinaryString(n);
if (String.charAt(j) == '1') ...
Character.isAlphabetic(String.charAt(i)) // 문자 판별
Character.isDigit(String.charAt(i-2)) // 숫자 판별
String.substring(start, end) // start부터 end-1까지 파싱
String.substring(start) // start부터 끝까지 파싱
: 그 문자를 문자열 앞에 더해준뒤 정렬하기
arr.add(strings[i].charAt(n)+strings[i]);
public class Java0707_1 {
public String[] solution(int n, int[] arr1, int[] arr2) {
String[] answer = new String[n];
String bin_num1 = "", bin_num2 = "";
for (int i = 0; i < n; i++) {
bin_num1 = Integer.toBinaryString(arr1[i]); // Int형 십진수 -> String형 이진수
bin_num2 = Integer.toBinaryString(arr2[i]);
bin_num1=while_fun(bin_num1, n); // 글자수에 맞게 앞에 '0'추가
bin_num2=while_fun(bin_num2, n);
String res = "";
for (int j = 0; j < n; j++) {
String str="";
str = (bin_num1.charAt(j) == '1' || bin_num2.charAt(j)=='1') ? "#":" ";
// String.charAt() -> String의 한문자씩 접근
res += str;
}
answer[i] = res;
}
return answer;
}
public String while_fun(String bin_num1, int n) {
while (bin_num1.length() != n)
bin_num1 = "0" + bin_num1;
return bin_num1;
}
}
public class Java0707_2 {
public int solution(String dartResult) {
int answer = 0;
boolean star = false;
for(int i=dartResult.length()-1;i>=0;i--) {
int score =0;
if(Character.isAlphabetic(dartResult.charAt(i))){
if(i-2>=0 && Character.isDigit(dartResult.charAt(i-2)))
// 점수가 두자릿수 이면
score=Integer.parseInt(dartResult.substring(i-2, i)); // i-2부터 i-1까지 파싱
else score = dartResult.charAt(i-1) - '0';
// char형 - '0' : 아스키 연산을 토대로 char -> int변환
if(dartResult.charAt(i)=='T') { score= (int)Math.pow(score, 3);}
else if (dartResult.charAt(i)=='D') score = (int)Math.pow(score, 2);
if(star) {
score*=2;
star = false;
}
if(i+1<dartResult.length() && dartResult.charAt(i+1)=='*') {
score*=2;
star = true;
}else if (i+1<dartResult.length() && dartResult.charAt(i+1)=='#')
score *=-1;
}
answer +=score;
}
return answer;
}
}
import java.util.ArrayList;
import java.util.Arrays;
public class Java0707_3 {
public String[] solution(String[] strings, int n) {
String[] answer = {};
ArrayList<String> arr = new ArrayList<String>();
for(int i=0;i<strings.length;i++)
arr.add(strings[i].charAt(n)+strings[i]);
// n번쨰 문자를 기준으로 정렬해야 하기에 맨 앞에 더해주기
answer = arr.toArray(new String[arr.size()]);
// ArrayList -> 배열
Arrays.sort(answer);
for(int i=0; i< answer.length;i++)
answer[i] = answer[i].substring(1);
// 아까 더했던 문자 제외하고 할당
return answer;
}
}
public String solution(String s) {
String answer = "";
char[] arr = s.toCharArray();
Arrays.sort(arr);
StringBuilder sb = new StringBuilder(new String(arr));
// String의 경우 new StringBuilder(String)
// char[]의 경우 new StringBuilder(new String(char[])
answer = sb.reverse().toString();
return answer;
}
public String solution(String s) {
String answer = "";
Character[] arr = new Character[s.length()];
for(int i=0; i<arr.length; i++) arr[i] = s.charAt(i);
Arrays.sort(arr,Collections.reverseOrder());
// 이때 arr는 char[] 이 아닌 Character[] 이어야 함
for(int i=0;i<arr.length;i++) answer+=arr[i];
return answer;
}
public String solution(String s) {
String answer = "";
String[] arr = s.split("");
Arrays.sort(arr,Collections.reverseOrder());
for(int i=0;i<arr.length;i++) answer+=arr[i];
return answer;
}
// 최소공배수와 최대공약수
package Programmers.lv1;
public class Java0707_5 {
public int gcd(int a, int b) {
if(a==0) return b;
return gcd(b%a,a);
}
public int[] solution(int n, int m) {
int[] answer = new int[2];
answer[0] = gcd(n,m);
answer[1] = n*m/answer[0];
return answer;
}
}
주의) n은 10,000,000,000이하인 자연수, long 타입
public class Java0707_6 {
public int[] solution(long n) {
String n_str = ""+n;
//문자열 s를 ""와 n을 더해줌으로 n을 문자열로 만들 수 있다.
int[] answer = new int[n_str.length()];
for(int i=0; i<n_str.length();i++)
answer[i] = n_str.charAt((n_str.length()-1)-i)-'0';
// char - '0' 함으로써 숫자로 변환
return answer;
}
}