전체 글200 [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주차 미션 회고 [1주차 미션] 호감 표시 삭제 & 구글 로그인 연동 알게된 점 🤔 호감 삭제 권한에 대한 객체를 확인 할 때 Object check = likeablePersonService.findById(id); System.out.println(check); Optional[LikeablePerson(id=3, createDate=2023-04-05T15:24:52.414144, modifyDate=2023-04-05T15:24:52.414144, fromInstaMember=InstaMember(id=5, createDate=2023-04-05T15:24:40.723778, modifyDate=2023-04-05T15:24:40.723778, username=master, gender=W), fromInstaM.. 오늘의 뚝딱/STUDY 2023. 4. 7. [Lv. 0] 숨어있는 숫자의 덧셈 (2) ✏️ 작성한 코드 class Solution { public int solution(String my_string) { int answer = 0; String[] str = my_string.split("[a-zA-Z]"); for (int i = 0; i < str.length; i++) { answer+= !str[i].isEmpty() ? Integer.parseInt(str[i]) :0; } return answer; } } 정규식을 사용해 알파벳인 부분이 나오기 전까지 배열에 담아주도록 작성해보았다! Algorithm/프로그래머스 2023. 4. 6. 이전 1 ··· 4 5 6 7 8 9 10 ··· 17 다음