Algorithm (402) 썸네일형 리스트형 [프로그래머스] 문자열 내 p와 y의 개수 / Stream , chars() , filter() Level. 1 문제 풀이 class Solution { boolean solution(String s) { s = s.toLowerCase(); boolean answer = true; int count = 0; for(int i=0; i 'P'== e).count() == s.chars().filter( e -> 'Y'== e).count(); } } Java의 기본 문자 어레이를 chars()를 통해 스트림으로 변환하고, filter()를 통해 원하는 자료만 필터링하여 한줄코드로 해결했다. chars() chars()는 CharSequence 인터페이스로부터 파생한 String 클래스의 새로운 메서드이다. chars()는 기본적으로 IntStream을 반환하여, 문자어레이를 스트림으로 만들 때 사용.. [프로그래머스] 삼총사 / DFS Level.1 문제 풀이 class Solution { static boolean[] visited = new boolean[13]; static int ans = 0; public void dfs(int[] number, int idx, int sum, int count){ if(count == 3){ if(sum == 0) ans++; return; } for(int i=idx; i [프로그래머스] 시저암호 / isLowerCase Level. 1 문제 풀이 class Solution { public String solution(String s, int n) { String answer = ""; n = n%26; for(int i=0; i [프로그래머스] 문자열을 정수로 바꾸기 / valueOf , int와 Integer Level. 1 문제 풀이 class Solution { public int solution(String s) { int answer = 0; Boolean sign = true; //answer = Integer.valueOf(s); for(int i=0; i< s.length(); i++){ char c = s.charAt(i); if(c == '-') sign = false; else if(c != '+') answer = answer * 10 + (c-'0'); } return sign==true?answer:answer*-1; } } Integer의 valueOf(), parseInt()를 사용하면 한 줄에 해결할 수 있으나, API를 사용하지 않고 알고리즘을 통해 문제를 해결해보았다. valu.. [프로그래머스] 푸드 파이트 대회 - reverse() Level. 1 문제 풀이 #include #include #include #include using namespace std; string solution(vector food) { string answer = ""; string str1 = ""; string str2; for(int i=1; i [프로그래머스] 숫자짝꿍 - map Level. 1 문제 풀이 #include #include #include using namespace std; string solution(string X, string Y) { string answer = ""; map Xmap; map Ymap; for(int i=0; i 0){ minVal--; answer += to_string(i); } } if(answer == "") return "-1"; if(answer[0] == '0') return "0"; return answer; } 해결과정 자료구조 map을 사용해 key값으로 0~9를 주고 문자열에 들어있는 숫자의 개수를 각각 세어주었다. answer는 겹치는 숫자중 가장 큰 값이어야 하므로, 9부터 0까지 공통된 숫자를 탐색한다. minVa.. [프로그래머스] 가운데 글자 가져오기 - substring Level. 1 문제 풀이 class Solution { public String solution(String s) { int size = s.length(); String answer = size%2==0? s.substring(size/2-1,size/2+1) : s.substring(size/2,size/2+1); return answer; } } String substring() 1. substring(int index) - 입력받은 인자값을 index로, 해당 위치를 포함해 이후의 모든 문자열을 리턴한다. 2. substring(int beginIndex, int endIndex) - beginIndex 위치에서 endIndex 바로 전 위치까지의 값을 리턴한다. 다른 사람 풀이 class St.. [프로그래머스]문자열 내림차순으로 배치하기 Level. 1 문제 풀이 import java.util.*; class Solution { public String solution(String s) { String answer = ""; char[] arr = s.toCharArray(); Arrays.sort(arr); for(int i=0; i 이전 1 ··· 36 37 38 39 40 41 42 ··· 51 다음