문자열 s는 한 개 이상의 단어로 구성되어 있습니다. 각 단어는 하나 이상의 공백문자로 구분되어 있습니다. 각 단어의 짝수번째 알파벳은 대문자로, 홀수번째 알파벳은 소문자로 바꾼 문자열을 리턴하는 함수, solution을 완성하세요.
class Solution {
public String solution(String s) {
StringBuilder answer = new StringBuilder();
String[] str = s.split("");
int idx = 0;
for (int i = 0; i < str.length; i++) {
if (str[i].equals(" ")) {
idx = 0;
} else if (idx % 2 == 0) {
str[i] = str[i].toUpperCase();
idx++;
} else {
str[i] = str[i].toLowerCase();
idx++;
}
answer.append(str[i]);
}
return answer.toString();
}
}
Character
의 toUpperCase(), toLowerCase() 유용해보인다.
class Solution {
public String solution(String s) {
char[] chars = s.toCharArray();
int idx = 0;
for (int i = 0; i < chars.length; i++) {
if (chars[i] == ' ') {
idx = 0;
} else {
chars[i] = (idx++ % 2 == 0 ? Character.toUpperCase(chars[i]) : Character.toLowerCase(chars[i]));
}
}
return String.valueOf(chars);
}
}
자연수 N이 주어지면, N의 각 자릿수의 합을 구해서 return 하는 solution 함수를 만들어 주세요.
예를들어 N = 123이면 1 + 2 + 3 = 6을 return 하면 됩니다.
N의 범위 : 100,000,000 이하의 자연수
public class Solution {
public int solution(int n) {
int answer = 0;
while (true) {
answer += n % 10;
n /= 10;
if(n == 0) {
break;
}
}
return answer;
}
}
Math.log의 사용법 신박하다👀
public class Solution {
public int solution(int n) {
int answer = 0;
int len = (int)Math.log10(n)+1;
for(int i = 0; i < len; i++) {
answer += n%10;
n = n/10;
}
return answer;
}
}
자연수 n을 뒤집어 각 자리 숫자를 원소로 가지는 배열 형태로 리턴해주세요. 예를들어 n이 12345이면 [5,4,3,2,1]을 리턴합니다.
n은 10,000,000,000이하인 자연수입니다.
class Solution {
public int[] solution(long n) {
String str = String.valueOf(n);
int len = str.length();
int[] answer = new int[len];
for (int i = 0; i < str.length(); i++) {
String temp = String.valueOf(str.charAt(len - 1 -i));
answer[i] = Integer.parseInt(temp);
}
return answer;
}
}
String.valueOf()
대신 ""
로 간단 해결! 기본적인 개념인데 이렇게 쓸 생각은 또 못해봤네..
class Solution {
public int[] solution(long n) {
String a = "" + n;
int[] answer = new int[a.length()];
int cnt=0;
while(n>0) {
answer[cnt] = (int)(n % 10);
n /= 10;
System.out.println(n);
cnt++;
}
return answer;
}
}
함수 solution은 정수 n을 매개변수로 입력받습니다. n의 각 자릿수를 큰것부터 작은 순으로 정렬한 새로운 정수를 리턴해주세요. 예를들어 n이 118372면 873211을 리턴하면 됩니다.
n은 1이상 8000000000 이하인 자연수입니다.
import java.util.Arrays;
class Solution {
public long solution(long n) {
String str = String.valueOf(n);
char[] charArray = str.toCharArray();
Arrays.sort(charArray);
char[] answer = new char[charArray.length];
for (int i = 0; i < charArray.length; i++) {
answer[i] = charArray[charArray.length - 1 - i];
}
return Long.parseLong(String.valueOf(answer));
}
}
import java.util.*;
class Solution {
public long solution(long n) {
String[] list = String.valueOf(n).split("");
Arrays.sort(list);
StringBuilder sb = new StringBuilder();
for (String aList : list) {
sb.append(aList);
}
return Long.parseLong(sb.reverse().toString());
}
}
임의의 양의 정수 n에 대해, n이 어떤 양의 정수 x의 제곱인지 아닌지 판단하려 합니다.
n이 양의 정수 x의 제곱이라면 x+1의 제곱을 리턴하고, n이 양의 정수 x의 제곱이 아니라면 -1을 리턴하는 함수를 완성하세요.
n은 1이상, 50000000000000 이하인 양의 정수입니다.
class Solution {
public long solution(long n) {
long answer = (long) Math.sqrt(n);
if (Math.pow(answer, 2) == n) {
return (long) Math.pow(answer + 1, 2);
} else {
return -1;
}
}
}
한줄로도 표현이 가능했네👀
class Solution {
public long solution(long n) {
if (Math.pow((int)Math.sqrt(n), 2) == n) {
return (long) Math.pow(Math.sqrt(n) + 1, 2);
}
return -1;
}
}