์
์ถ๋ ฅ ์
my_string | num1 | num2 | result |
---|---|---|---|
"hello" | 1 | 2 | "hlelo" |
"I love you" | 3 | 6 | "I l veoyou" |
์
์ถ๋ ฅ ์ ์ค๋ช
๋์ ํ์ด
package ํ๋ก๊ทธ๋๋จธ์ค;
public class IndexChange {
public static String solution(String my_string, int num1, int num2) {
String answer = "";
StringBuilder str = new StringBuilder(my_string);
char temp = str.charAt(num1);
str.setCharAt(num1, str.charAt(num2));
str.setCharAt(num2, temp);
answer+= str;
return answer;
}
public static void main(String[] args) {
solution("I love you",3,6);
}
}
๋์ ์๊ฐ
String ํด๋์ค๋ ๋ถ๋ณํ๊ธฐ๋๋ฌธ์, ํ ๋ฒ ์์ฑ๋ ๋ฌธ์์ด์ ๋ณ๊ฒฝํ ์ ์๋ค. ๋ฐ๋ผ์,
๋ฌธ์์ด์ ๊ฐ๋ณ์ ์ผ๋ก ๋ณ๊ฒฝํ ์ ์๋ StringBuilder
ํด๋์ค์ ๋ฌธ์์ด์ ์ฐ๋ฉด ์ถ๊ฐ, ์ญ์ , ์์ ์ด ์ฉ์ดํ๋ค.
์์ ๋ณ์ temp์ ๋งค๊ฐ๋ณ์๋ก ๋์ด์จ num1์ ๋ฃ๊ณ str.setCharAt()
์ index num1 ๋ฒ์งธ ๊ฐ์ str.charAt(num2)
๊ฐ์ผ๋ก ๋์ฒดํ ๋ค, ์์ ์ ์์น์๋ temp๊ฐ์ ๋ฃ์ด ์ธ๋ฑ์ค ์์น๋ฅผ ๊ตํํ๋ ๋ฐฉ๋ฒ์ ์ฌ์ฉํ์๋ค.
์
์ถ๋ ฅ ์
numbers | direction | result |
---|---|---|
[1, 2, 3] | "right" | [3, 1, 2] |
[4, 455, 6, 4, -1, 45, 6] | "left" | [455, 6, 4, -1, 45, 6, 4] |
์
์ถ๋ ฅ ์ ์ค๋ช
๋์ ํ์ด
package ํ๋ก๊ทธ๋๋จธ์ค;
import java.util.Arrays;
public class Rotation {
public static int[] solution(int[] numbers, String direction) {
int temp = 0;
int[] newArr = new int[numbers.length];
if(direction.equals("right")) {
temp = numbers[numbers.length-1]; //temp์ ๋ง์ง๋ง ๊ฐ์ ๋์
for(int i = 1; i < numbers.length; i++) {
newArr[i] = numbers[i-1];
}
newArr[0] = temp;
}else {
temp = numbers[0];
for(int i = 0; i<numbers.length-1; i++) {
newArr[i] = numbers[i+1];
}
newArr[numbers.length-1] = temp;
}
return newArr;
}
public static void main(String[] args) {
int[] numbers = {1,2,3};
solution(numbers,"right");
}
}
๋์ ์๊ฐ
temp๋ผ๋ ์์ ๋ณ์๋ฅผ ์์ฑํ์ฌ, ํด๋น ๊ฐ์ numbers์ ๋ง์ง๋ง index ๊ฐ์ ๋์
๊ทธ๋ฆฌ๊ณ for๋ฌธ์ ๋๋ ค ์ํํธ ๋ ๊ฐ์ ๋์
ํ๊ณ , newArr[0] = temp๊ฐ์ ๋ฃ๋ ๋ฐฉ๋ฒ์ ์ฌ์ฉ, ๋ฐ๋๋ก ์ผ์ชฝ์ผ๋ก ์ํํธ๋ ์์ ๋ณ์ temp์ numbers์ ์ฒ์ index๊ฐ์ ๋์
ํ๊ณ ,
numbers.length-1
ํฌ๊ธฐ ๋งํผ ๋ฐ๋ณต์ ๋๋ฆฌ๋๋ฐ, ์ด๋ ๋ง์ง๋ง index์ temp๊ฐ์ ๋ฃ์ด
์ํํธ ๋ ๊ฐ์ ๊ตฌํ๋ค.
class Solution {
public int[] solution(int[] numbers, String direction) {
int length = numbers.length;
int[] rotated = new int[length];
if (direction.equals("right")) { // ์๊ณ๋ฐฉํฅ ํ์
for (int i = 0; i < length; i++) {
rotated[(i+1)%length] = numbers[i];
}
} else { // ๋ฐ์๊ณ๋ฐฉํฅ ํ์
for (int i = 0; i < length; i++) {
rotated[i] = numbers[(i+1)%length];
}
}
return rotated;
}
}
์ ๊ทผ ๋ฐฉ๋ฒ์ ๋์ ๊ฐ์ง๋ง, ํ์ด๋ฅผ ๋ดค์ ๋ ๋คํต์๋ฅผ ํ๋ ์ป์ด ๋ง๋ ๋ฏํ ๊ธฐ๋ถ์ด์๋ค...
๋
ผ๋ฆฌ์ ์ผ๋ก ๋ดค์ ๋, ์ด๊ฒ ์ด๋ป๊ฒ ์ด๋ ๊ฒ ๋๋๊ฑฐ์ง... ํ์ง๋ง, ์ด๊ฑธ ์ด๋ ๊ฒ ํ์์ด????
right
1%3 == 1
2%3 == 2
3%3 == 0
rotated[1] = numbers[0];
rotated[2] = numbers[1];
rotated[0] = numbers[2];
๊ฒฐ๊ณผ๋ ์ค๋ฅธ์ชฝ์ผ๋ก shiftํ ๊ฒฐ๊ณผ๊ฐ ๋์ด
left
rotated[0] = numbers[1];
rotated[1] = numbers[2];
rotated[2] = numbers[0];
๊ฒฐ๊ณผ๋ ์ผ์ชฝ์ผ๋ก shiftํ ๊ฒฐ๊ณผ๊ฐ ๋์ด
์
์ถ๋ ฅ ์
array | result |
---|---|
[1,8,3] | [8,1] |
[9,10,11,8] | [11,2] |
์
์ถ๋ ฅ ์ ์ค๋ช
๋์ ํ์ด
package ํ๋ก๊ทธ๋๋จธ์ค;
public class BigNum {
public static int[] solution(int[] array) {
int a = 0;
int[] answer = new int[2];
for(int i = 0; i< array.length; i++) {
a = array[i] > a ? array[i] : a;
}
answer[0] = a;
for(int i = 0; i<array.length; i++) {
if(array[i]==a) {
answer[1]= i;
break;
}
}
return answer;
}
public static void main(String[] args) {
int[] array = {9, 10, 11, 8};
solution(array);
}
}
๋์ ํ์ด
๋จผ์ , ํด๋น Array
์์ ๊ฐ์ฅ ํฐ ์๋ฅผ ์ฐพ๊ธฐ ์ํด์ ์ฌ๋ฌ๊ฐ์ง ๋ฐฉ๋ฒ์ด ์๊ฒ ์ง๋ง,
๊ฐ์ฅ ์์์ ์ธ ๋ชจ๋ ์๋ฅผ ๋น๊ตํด์ ๊ฐ์ฅ ํฐ ์๋ฅผ ์ฐพ์๋ด๋ ๋ฐฉ๋ฒ์ด๋ค.
์ผํญ์ฐ์ฌ์๋ฅผ ์ฌ์ฉํด์ array[i] ๊ฐ a ์ด๊ธฐ๊ฐ(0)๋ณด๋ค ํฌ๋ฉด a์ array[i]๋ฅผ ๋์
, ์๋๋ฉด a ์๊ธฐ ์์ ์ ๋์
๊ฐ์ฅ ํฐ ์๊ฐ ์๋ index ์ญ์ ๊ฐ์ฅ ํฐ ๊ฐ์ด ๋ค์ด๊ฐ ์ธ๋ฑ์ค์ ๊ฐ์ ๋๋ฅผ ๊ฒ์ถํ๋ ๋ฐฉ๋ฒ์ ์ฌ์ฉ
public static int[] solution(int[] array) {
int max = array[0];
int maxIndex = 0;
for (int i = 1; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
maxIndex = i;
}
}
return new int[]{max, maxIndex};
}
๋ค์๋ณด์๋ GPT์ ํ์ด๋ ๋๋๋ค
intํ ๋ณ์ max์ array[0] ๊ฐ์ ๋ฃ๋ ์๊ฐ์ ์ด๋ป๊ฒ ํ๋๊ฑฐ์ง...
Index ๋ณ์๋ฅผ ์ก๊ณ , for๋ฌธ์ ๋๋ ค array[i] ๊ฐ max ๋ณด๋ค ํด๋ ๋ง,
max๊ฐ๊ณผ, maxIndex๊ฐ์ ์ฌ๋ ค์ฃผ๋ ๋ฐฉ๋ฒ์ ...
return ๊ฐ์ new int[] {max, maxIndex}
ํ๋ฉด... ๋ฆฌํด์ด ๋๋๊ฒ๋ ์ ๊ธฐ..
์
์ถ๋ ฅ ์
age | result |
---|---|
23 | "cd" |
51 | "fb" |
100 | "baa" |
์
์ถ๋ ฅ ์ ์ค๋ช
๋์ ํ์ด
package ํ๋ก๊ทธ๋๋จธ์ค;
public class AlienPlanet {
public static String solution(int age) {
String answer = "";
String numStr = Integer.toString(age);
String[] str = new String[numStr.length()];
// ๊ฐ ์๋ฆฌ์๋ฅผ ๋ฌธ์์ด๋ก ๋ณํํ์ฌ ๋ฐฐ์ด์ ๋ฃ๊ธฐ
for (int i = 0; i < str.length; i++) {
str[i] = Character.toString(numStr.charAt(i));
if(str[i].equals("0")) {
str[i] = "a";
}else if(str[i].equals("1")) {
str[i] = "b";
}else if(str[i].equals("2")) {
str[i] = "c";
}else if(str[i].equals("3")) {
str[i] = "d";
}else if(str[i].equals("4")) {
str[i] = "e";
}else if(str[i].equals("5")) {
str[i] = "f";
}else if(str[i].equals("6")) {
str[i] = "g";
}else if(str[i].equals("7")) {
str[i] = "h";
}else if(str[i].equals("8")) {
str[i] = "i";
}else {
str[i] = "j";
}
answer+= str[i];
}
return answer;
}
public static void main(String[] args) {
solution(23);
}
}
๋์ ์๊ฐ
์ซ์๋ฅผ ๋ฌธ์๋ก ๋ณํํด์, str๋ฐฐ์ด์ ๋ฃ๋๊ฑฐ ๊น์ง ๊ด์ฐฎ์๋ ์๊ฐ๊ฐ์๋ฐ,
for๋ฌธ ์ if๋ฌธ์ ์ซ์ 0~9๊น์ง ๋ฐ๋ณต ํด์ผํ๋ค๋ ์ ์์, ์ข์ ๋ฐฉ๋ฒ์ ์๋์๋๊ฑฐ ๊ฐ๋ค. ์ ๋ต์ ๋ง์ท๊ธด ํ์ง๋ง, ์ ๋ต์ ๋ง์ถ์ ๋ถ๋ค ์ค์ ํจ์ฌ ๊น๋ํ ์ฝ๋์ ์ดํด๊ฐ ์ ๋๋ ์ฝ๋๊ฐ ๋ง์๋ค.
๊ฐ์ ๋ ์ฝ๋ (feat. chatGPT)
class Solution {
public String solution(int age) {
String answer = "";
while (age > 0) {
int remainder = age % 9;
char digitChar = (char)('a' + remainder);
answer = digitChar + answer;
age /= 9;
}
return answer;
}
}
์ด๊ธฐ์ String[] alpha = new String[]{"a","b","c","d","e","f","g","h","i","j"};
์ ๊ฐ์ด ๋ฐฐ์ด์ ๋ฐ๋ก ์ ์ธํ์ง์๊ณ , 10์ง์๋ฅผ 9์ง์๋ก ๋ณํํ๋ ๊ณผ์ ( age % 9 )๊ณผ ์์คํค ์ฝ๋ ํ
(char)('a' + remainder
) = ์์คํค ์ฝ๋ ํ ์ ์ํ๋ฒณ ์์๋ก ์ฆ๊ฐํ๋ ์๋ฆฌ๋ฅผ ์ด์ฉํ์ฌ, anwer = digitChar + answer;
์ญ์ ์ด๊ธฐ์ ์ ์ฅ๋ answer๊ฐ์ digitChar ๋ค๋ก ๋ณด๋ด์ด ์ต์ข
์ ์ผ๋ก ์ํ๋ ๊ฒฐ๊ณผ๊ฐ์ ์ป๋ ๋ฐฉ๋ฒ์ด๋ค. while ๋ฌธ์ age /= 9; ์ ๊ฒฐ๊ณผ๊ฐ 0์ด๋ฉด while๋ฌธ์ ๋น ์ ธ๋์จ๋ค.
์
์ถ๋ ฅ ์
n | result |
---|---|
6 | 1 |
10 | 5 |
4 | 2 |
์
์ถ๋ ฅ ์ ์ค๋ช
6๋ช ์ด ๋ชจ๋ ๊ฐ์ ์์ ๋จน๊ธฐ ์ํด ํ ํ์ ์์ผ์ผ ํผ์๊ฐ 6์กฐ๊ฐ์ผ๋ก ๋ชจ๋ ํ ์กฐ๊ฐ์ฉ ๋จน์ ์ ์์ต๋๋ค.
10๋ช ์ด ๋ชจ๋ ๊ฐ์ ์์ ๋จน๊ธฐ ์ํด ์ต์ 5ํ์ ์์ผ์ผ ํผ์๊ฐ 30์กฐ๊ฐ์ผ๋ก ๋ชจ๋ ์ธ ์กฐ๊ฐ์ฉ ๋จน์ ์ ์์ต๋๋ค.
4๋ช ์ด ๋ชจ๋ ๊ฐ์ ์์ ๋จน๊ธฐ ์ํด ์ต์ 2ํ์ ์ํค๋ฉด ํผ์๊ฐ 12์กฐ๊ฐ์ผ๋ก ๋ชจ๋ ์ธ ์กฐ๊ฐ์ฉ ๋จน์ ์ ์์ต๋๋ค.
๋์ ํ์ด
package ํ๋ก๊ทธ๋๋จธ์ค;
public class PizzaEat {
public static int solution(int n) {
int answer = 0;
int pizcnt = 1;
while(true) {
if((6*pizcnt) % n == 0) {
answer = (6 * pizcnt) / 6;
break;
}
pizcnt++;
}
return answer;
}
public static void main(String[] args) {
solution(4);
}
}
๋์ ์๊ฐ
์ ์ผ ๋จผ์ ๋ค์๋ ์๊ฐ์ด, ์ต์ ๊ณต๋ฐฐ์
์ ์ต๋ ๊ณต์ฝ์
๊ด๋ จ๋ ๋ฌธ์ ๊ฐ์ ๋๋์ด ๋ค์๋ค. ๋๋ ๊ทธ๋ฅ ์ง๊ฐ์ ์ผ๋ก ์ด๋ ๊ฒ, ์ด๋ ๊ฒํ๋ฉด ๋๊ฒ ๋ค ๋ผ๊ณ ์๊ฐํ์ง๋ง, ๊ฐ๋ฐ์๋ฅผ ๊ฟ๊พธ๋ ์ฌ๋์ผ๋ก์จ ์ ์ด๋ ๊ฒ ๋๋๊ฐ? ๋ฅผ ์์์ผ ํ๊ธฐ ๋๋ฌธ์ ์ฐพ์๋ณธ ๋ด์ฉ์ ์์ฑํ๋ค.
public class PizzaShop {
public static void main(String[] args) {
System.out.println(solution(7)); // ์ถ๋ ฅ: 7
}
public static int gcd(int a, int b) {
while (b != 0) {
int temp = a % b;
a = b;
b = temp;
}
return a;
}
public static int solution(int n) {
int gcdValue = gcd(n, 6);
int lcmValue = (n * 6) / gcdValue;
int numberOfPizzas = lcmValue / 6;
return numberOfPizzas;
}
}
while (b != 0) {
int temp = a % b;
a = b;
b = temp;
}
while (b != 0)
: b
๊ฐ 0์ด ์๋ ๋์ ๋ฐ๋ณต๋ฌธ์ด ์คํ, b
๊ฐ 0์ด๋ฉด ๋ฐ๋ณต๋ฌธ์ ์ข
๋ฃint temp = a % b
: a
๋ฅผ b
๋ก ๋๋ ๋๋จธ์ง๋ฅผ temp
๋ณ์์ ์ ์ฅa
= b
: b
์ ๊ฐ์ a
์ ํ ๋นb = temp
: ์ด์ ์ ๊ตฌํ ๋๋จธ์ง์ธ temp
๊ฐ์ b
์ ํ ๋น๋ฐ๋ณต๋ฌธ์ด ์งํ๋ ๋๋ง๋ค
a
์b
์ ๊ฐ์ด ๋ฐ๋๊ณ , ๋ ์์ ๋๋จธ์ง๊ฐ ์ ์ ์์์ง, ์ด ๊ณผ์ ์ ๋ฐ๋ณตํ์ฌb
๊ฐ 0์ด ๋๋ฉด, ๊ทธ ์์ ์a
๊ฐ์ ๋ ์์ ์ต๋ ๊ณต์ฝ์๊ฐ ๋จ
ex) a = 24, b = 18 ์ผ๋,
์ฒซ ๋ฒ์งธ ๋ฐ๋ณต:
temp = 24 % 18 = 6
a = 18
b = 6
๋ ๋ฒ์งธ ๋ฐ๋ณต:
temp = 18 % 6 = 0
a = 6
b = 0