Algorithm

Algorithm/프로그래머스

[알고리즘] 콜라츠 추측 Java

import java.util.ArrayList; import java.util.List; class Solution { public String solution(String s) { String answer = ""; String[] split = s.split(" ", -1); List list = new ArrayList(); for (String s1 : split) { String[] world = s1.toLowerCase().split(""); for (int i = 0; i < world.length; i++) { if (i % 2 == 0) { world[i] = world[i].toUpperCase(); } } list.add(String.join("", world)); } answer..

Algorithm/프로그래머스

[알고리즘] 콜라츠 추측 Java

class Solution { public int solution(long num) { int answer = 0; while(num > 1) { num = num % 2 == 0 ? num / 2 : (num * 3) + 1; answer++; if(answer >= 500) { answer = -1; break; } } return answer; } }

Algorithm/프로그래머스

[알고리즘] 하샤드 수 Java

class Solution { public boolean solution(int x) { boolean answer = true; int y = x; int sum = 0; while(x > 0) { sum += x % 10; x /= 10; } answer = y % sum == 0 ? true : false; return answer; } }

Algorithm/프로그래머스

[알고리즘] 문자열 내 p와 y의 개수 Java

import java.util.Arrays; class Solution { boolean solution(String s) { String str = s.toLowerCase(); long p = Arrays.stream(str.split("")).filter(i -> i.equals("p")).count(); long y = Arrays.stream(str.split("")).filter(i -> i.equals("y")).count(); return p == y; } }

Algorithm/프로그래머스

[알고리즘] 자릿수 더하기 Java

public int solution(int n) { int answer = 0; String[] split = String.valueOf(n).split(""); for (String s : split) { answer += Integer.parseInt(s); } return answer; } 다른 사람의 풀이 public class Solution { public int solution(int n) { int answer = 0; while (n != 0) { answer += n % 10; n /= 10; } return answer; } }

Algorithm/프로그래머스

[알고리즘] 숨어있는 숫자의 덧셈 (2) Java

class Solution { public int solution(String my_string) { int answer = 0; String[] split = my_string.split("[^0-9]", -1); for (String s : split) { if (!s.equals("")) { answer += Integer.parseInt(s); } } return answer; } } 다른 사람의 풀이 class Solution { public int solution(String my_string) { int answer = 0; String[] str = my_string.replaceAll("[a-zA-Z]", " ").split(" "); for(String s : str){ if(!s.eq..

Algorithm/프로그래머스

[알고리즘] 외계행성의 나이 Java

class Solution { public String solution(int age) { String answer = ""; String s = String.valueOf(age); String[] split = s.split(""); for (int i = 0; i < split.length; i++) { int a = Integer.parseInt(split[i]) + 97; char ch = (char) a; answer += ch; } return answer; } }

Algorithm/프로그래머스

[알고리즘] 배열 자르기 Java

import java.util.Arrays; class Solution { public int[] solution(int[] numbers, int num1, int num2) { int[] answer = Arrays.copyOfRange(numbers, num1, num2+1); return answer; } }

rw-
'Algorithm' 카테고리의 글 목록 (2 Page)