Algorithm88 [Lv. 1] x만큼 간격이 있는 n개의 숫자 class Solution { public long[] solution(int x, int n) { long[] answer = new long[n]; long num = x; for(int i = 0; i < answer.length; i++){ answer[i] = num; num += x; } return answer; } } Algorithm/프로그래머스 2023. 4. 21. [Lv. 1] 나머지가 1이 되는 수 찾기 class Solution { public int solution(int n) { int answer = 0; for(int i = 2; i < n; i++){ if(n % i == 1){ answer = i; break; } } return answer; } } Algorithm/프로그래머스 2023. 4. 21. [Lv. 1] 자릿수 더하기 public class Solution { public int solution(int n) { int answer = 0; while(n>0){ answer += n % 10;//가장 끝자리가 나오게 됨 -> 123이면 3 n /= 10;//가장 마지막 자리수는 더해주었으니 나누는 방식으로 제거 -> 123이면 12로 만들어줌 } return answer; } } Algorithm/프로그래머스 2023. 4. 20. [Lv. 0] 두 수의 연산값 비교하기 class Solution { public int solution(int a, int b) { int answer = 0; String s = a + "" + b; if(Integer.parseInt(s) > 2*a*b) answer = Integer.parseInt(s); else answer = 2*a*b; return answer; } } Algorithm/프로그래머스 2023. 4. 20. [Lv. 0] 다음에 올 숫자 class Solution { public int solution(int[] common) { int answer = 0; if(common[2]-common[1] == common[1]-common[0]) answer = common[common.length - 1] + (common[1]-common[0]); else answer = common[common.length-1] * common[2]/common[1]; return answer; } } Algorithm/프로그래머스 2023. 4. 18. [Lv. 0] 연속된 자연수의 합 class Solution { public int[] solution(int num, int total) { int[] answer = new int[num]; int a = (2 * total / num -num +1)/2; for(int i = 0; i < num ; i++){ answer[i] = a; a++; } return answer; } } 연속된 자연수라는 말에서 공차가 1인 등차수열로 풀 수 있겠다는 아이디어가 떠올랐고 등차수열의 합공식을 활용해 a1, 즉 첫번째 숫자를 구해 num 만큼 구하는 방법을 사용해 보았다 Algorithm/프로그래머스 2023. 4. 17. [Lv. 0] 종이 자르기 class Solution { public int solution(int M, int N) { int answer = 0; answer = (M - 1) * N + (N - 1); return answer; } } Algorithm/프로그래머스 2023. 4. 17. [Lv. 0] 문자열 밀기 ✏️ 작성한 코드 class Solution { public int solution(String A, String B) { int answer = 0; String newA = A; for(int i = 0; i < A.length(); i++){ if(newA.equals(B)) return answer; String push = newA.substring(newA.length()-1); newA = push + newA.substring(0, newA.length()-1); answer++; } return -1; } } 문자열이 아예 같다면 바로 0을 rtn 아니라면 한칸씩 밀면서 비교해보다가 같아질때 몇 번 밀었는지 answer를 rtn Algorithm/프로그래머스 2023. 4. 14. [Lv. 0] 잘라서 배열로 저장하기 class Solution { public String[] solution(String my_str, int n) { int cnt = (my_str.length() + n - 1) / n; String[] answer = new String[cnt]; for (int i=0; i= my_str.length()) { end = my_str.length(); } else { end = start + n; } answer[i] = my_str.substring(start, end); } return answer; } } cnt -> +n -1을 하면 나누어 떨어질 때와 그렇지 않을 때 상관 없이 모두 필요한 개수가 나오게 됨 start + n >= my_str.length()라면 가장 마지막 배열의 시작 지.. Algorithm/프로그래머스 2023. 4. 13. [Lv. 0 ] 7의 개수 ✏️ 작성한 코드 class Solution { public int solution(int[] array) { int answer = 0; for(int i = 0; i < array.length; i++){ String num = Integer.toString(array[i]); String[] arr = num.split(""); for(int j = 0; j < arr.length; j++){ if(arr[j].equals("7")) answer++; } } return answer; } } 처음엔 위에처럼 주어진 배열 요소 하나하나를 7과 비교하려다가 영 비효율적이라고 생각이 들어서 class Solution { public int solution(int[] array) { int answer =.. Algorithm/프로그래머스 2023. 4. 11. [Lv. 0] 소인수분해 ✏️ 작성한 코드 class Solution { public int[] solution(int n) { LinkedHashSet set = new LinkedHashSet(); for (int i = 2; i Algorithm/프로그래머스 2023. 4. 10. [Lv. 0] 이진수 더하기 class Solution { public String solution(String bin1, String bin2) { String answer = ""; int num1 = Integer.parseInt(bin1, 2); int num2 = Integer.parseInt(bin2, 2); int sum = num1+num2; answer = Integer.toBinaryString(sum); return answer; } } parseInt가 단순 String 타입을 int 값으로 parse해주는 건줄 알았는데 parseInt(String 문자열, int 진수) 을 넣어주면 진수인 문자열 값을 십진수 int타입으로 반환해준다. bin1, bin2는 이진수이기 때문에 2를 넣어줬다. - toBinary.. Algorithm/프로그래머스 2023. 4. 7. 이전 1 2 3 4 5 6 7 8 다음