sides | result |
---|---|
[1,2,3] | 2 |
[3,6,2] | 2 |
[199,72,222] | 1 |
์
์ถ๋ ฅ ์
๋์ ํ์ด
import java.util.Arrays;
class Solution {
public int solution(int[] sides) {
int answer = 0;
Arrays.sort(sides);
if(sides[2] < (sides[0]+sides[1])) {
answer = 1;
}else {
answer = 2;
}
return answer;
}
}
๋์ ์ ๊ทผ
๋งค๊ฐ๋ณ์๋ฅผ ๋ฐฐ์ด๋ก ๋ฐ๊ณ , ๊ทธ ๋ฐฐ์ด ๊ฐ์ Arrays ํด๋์ค์ sort๋ฉ์๋๋ฅผ ์จ์ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌ์ ์ํจ๋ค. ๊ทธ๋ ๊ฒ ํ๋ฉด ์์ฐ์ค๋ฝ๊ฒ sides[0], sides[1], sides[2]์ ๊ฐ์ด ํ ๋น๋๊ธฐ ๋๋ฌธ์ ์ฐ์ฐ๊ฒฐ๊ณผ์ ๋ฐ๋ผ return๊ฐ์ ๊ฒฐ์
n | result |
---|---|
20 | 6 |
100 | 9 |
์
์ถ๋ ฅ ์ ์ค๋ช
์ ์ถ๋ ฅ ์ #1
์ ์ถ๋ ฅ ์ #2
๋์ ํ์ด
class Solution {
public int solution(int n) {
int answer = 0;
for(int i = 1; i<=n; i++) {
if( n % i == 0) { // 1,2,4,5,10,20
answer++;
}
}
return answer;
}
}
๋์ ์๊ฐ
n์ ์์์์ ์๊ฐํ์ ๋ , ๋ฐ๋ก ๋ค์๋ ์๊ฐ์ n์ 1๋ถํฐ n๊น์ง ๋๋์์ ๋ ๋์จ ๋๋จธ์ง๊ฐ 0์ด๋ฉด ๊ฒฐ๊ตญ ์ด๊ฒ์ด ์์์์ด๋ ๊ฐ๊ตฌ๋ ๋ผ๊ณ ์๊ฐ์ด ๋ฆ.
๊ทธ ๋ answer๋ฅผ ์นด์ดํฐ๋ผ๊ณ ํ์ ๋, ๋๋ ๋๋จธ์ง๊ฐ 0์ผ๋ ์นด์ดํฐ๋ฅผ 1์ฉ ์ฆ๊ฐํ์ฌ ๋ฆฌํด
my_string | n | result |
---|---|---|
"hello" | 3 | "hhheeellllllooo" |
์
์ถ๋ ฅ ์ ์ค๋ช
๋์ ํ์ด
class Solution {
public String solution(String my_string, int n) {
String answer = "";
String[] arr = my_string.split(""); // ๋งค๊ฐ๋ณ์๋ฅผ ๋ฐ์์ ๋ฐฐ์ด์ ๋ฃ๋ ์์
for(int i = 0; i<arr.length; i++) {
for(int j = 0; j<n; j++) {
answer += arr[i];
}
}
return answer;
}
}
๋์ ์๊ฐ
๋งค๊ฐ๋ณ์ String my_string์ ์ชผ๊ฐ์ด ๋ด์ String ๋ฐฐ์ด arr์ ์ก๋๋ค.
์ด์ค for๋ฌธ์ ๋๋ ค ์์ชฝfor๋ฌธ์ ๋งค๊ฐ๋ณ์n๋ฒ ๋ฐ๋ณต์ผ๋ก ๋๋ฆฌ๊ณ answer์ arr[i]๋ฅผ ๋ํด์ค๋ค. ๋ฐ๊นฅ์ชฝ for๋ฌธ์ arr์ ํฌ๊ธฐ๋งํผ ๋ฐ๋ณต์ ๋๋ ค์ค๋ค
ideal
class Solution {
public String solution(String my_string, int n) {
String answer = "";
String[] str = my_string.split("");
for(int i=0; i<my_string.length(); i++){
answer += str[i].repeat(n);
}
return answer;
}
}
๋ฐฐ์ด์ repeat() method
๋ฅผ ์ด์ฉํ๋ฉด ์ด์ค for๋ฌธ์ ์ฐ์ง์๊ณ ๋ ์ฝ๊ฒ ์ ๊ทผํ ์ ์๋ค.
price | result |
---|---|
150,000 | 142,500 |
580,000 | 464,000 |
์
์ถ๋ ฅ ์ ์ค๋ช
๋์ ํ์ด
class Solution {
public int solution(int price) {
if(price >= 500000){
return (int) (price - (price*0.2)); //(0.8* price)
}else if(price >= 300000){
return (int)(price - (price*0.1)); //(0.9* price)
}else if(price >= 100000){
return (int)(price - (price*0.05)); //(0.95* price)
}else {
return (int)price;
}
}
}
๋์ ์๊ฐ
discount ํ ๊ฐ๊ฒฉ์ intํ์ผ๋ก ์บ์คํ ํ๋๊ฒ ํฌ์ธํธ
message | result |
---|---|
"happy birthday!" | 30 |
"l love you~" | 22 |
์
์ถ๋ ฅ ์ ์ค๋ช
message
์ ๊ธ์ ์๊ฐ 15๊ฐ๋ก ์ต์ ๊ฐ๋ก 30cm์ ํธ์ง์ง๊ฐ ํ์ํฉ๋๋ค.message
์ ๊ธ์ ์๊ฐ 11๊ฐ๋ก ์ต์ ๊ฐ๋ก 22cm์ ํธ์ง์ง๊ฐ ํ์ํฉ๋๋ค.๋์ ํ์ด
class Solution {
public int solution(String message) {
int answer = 0;
String[] letter = message.split("");
for(int i = 0; i<letter.length;i++){
answer++;
}
return answer*2;
}
}
์
์ถ๋ ฅ ์
array | result |
---|---|
[1, 2, 7, 10, 11] | 7 |
[9, -1, 0] | 0 |
๋์ ํ์ด
import java.util.Arrays;
class Solution {
public int solution(int[] array) {
int answer = 0;
Arrays.sort(array);
answer = array[(array.length)/2];
return answer;
}
}
๋์ ์๊ฐ
๋งค๊ฐ๋ณ์๋ฅผ ๋ฐฐ์ด๋ก ์ฃผ์ด์ก์ ๋, Array.sort() ๋ฉ์๋๋ฅผ ์ด์ฉํ์ฌ ์ ๋ ฌ์ ์ํจ๋ค. ์ ๋ ฌ ์ํจ ๊ฐ์์ /2 ๋ฅผ ํด์ฃผ๋ฉด Index ๋ฒํธ์ ์ค์๊ฐ์ ์ ๋ณํ ์ ์๋ค.
์
์ถ๋ ฅ ์
s1 | s2 | result |
---|---|---|
["a", "b", "c"] | ["com", "b", "d", "p", "c"] | 2 |
["n", "omg"] | ["m", "dot"] | 0 |
๋์ ํ์ด
import java.util.HashSet;
import java.util.Set;
class Solution {
public int solution(String[] s1, String[] s2) {
int count = 0;
Set<String> set = new HashSet<>();
for(int i = 0; i<s1.length; i++) {
set.add(s1[i]);
count++;
}
for(int i = 0; i<s2.length; i++) {
set.add(s2[i]);
count++;
}
return count-set.size();
}
}
๋์ ์๊ฐ
๋งค๊ฐ๋ณ์๊ฐ ๋ฐฐ์ด s1, s2 ๋ก ์ฃผ์ด์ง๊ธฐ ๋๋ฌธ์ ์ด ๋ ๋ฐฐ์ด์ ๊ฐ๊ฐ for๋ฌธ์ ๋๋ ค ๋ฐฐ์ด์์ ์๋ ๊ฐ์ ์นด์ดํ ํ์ฌ ๋ํ๊ณ , setํด์ ์ค๋ณต์ ์ ๊ฑฐํ ๊ฐ์ ์นด์ดํ ํ์ฌ ๋นผ๋ ๋ฐฉ๋ฒ์ ์ด์ฉํ์๋ค.
๋ค๋ฅธ ํ์ด
class Solution {
public int solution(String[] s1, String[] s2) {
int answer = 0;
for(String str1 : s1){
for(String str2 : s2){
if(str1.equals(str2)){
answer++;
break;
}
}
}
return answer;
}
}
for-each ๊ตฌ๋ฌธ์ผ๋ก s1,s2๋ฐฐ์ด์ ๋๋ ค ๋๋ฐฐ์ด์ ์ธ๋ฑ์ค ๊ฐ์ ๋น๊ตํ๋ ๋ฐฉ๋ฒ์ ์ด์ฉ
equals
๋ฉ์๋๋ฅผ ์ฌ์ฉ
n | result |
---|---|
1234 | 10 |
930211 | 16 |
์
์ถ๋ ฅ ์ ์ค๋ช
๋์ ํ์ด
class Solution {
public int solution(int n) {
int answer = 0;
String s1 = Integer.toString(n);
String[] arr = s1.split("");
for(int i = 0; i<arr.length; i++){
answer += Integer.parseInt(arr[i]);
}
return answer;
}
}
๋์ ์๊ฐ
๋ฌธ์ ๋ฅผ ๋ณด์๋ง์ int
ํ ํ์
๋ณ์๋ฅผ str
ํ์ผ๋ก ๋ณํ์ ์์ผ์ ๋ฌธ์ ํ๋์ฉ ์๋ผ ๋ฐฐ์ด์ ๋ฃ์ด์ผ๊ฒ ๋ค๊ณ ์๊ฐ, ๊ทธ๋ฆฌ๊ณ for๋ฌธ์ ๋๋ ค str
ํ ๋ณ์๋ฅผ ๋ค์ int
๋ก ํ๋ณํํ์ฌ ๋ํ์ฌ ๋ฆฌํด.
ํต์ฌ ๊ฐ๋
String s = Integer.toString(n); //int n ๋งค๊ฐ๋ณ์
int answer = Integer.parseInt(s);
String[] arr = s1.split("");