본문 바로가기

Algorithm

(401)
[프로그래머스] 삼총사 / 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
[프로그래머스] 두 정수의 합 - Math클래스 Level. 1 문제 풀이 class Solution { public long solution(int a, int b) { long answer = 0; int max = Math.max(a,b); int min = Math.min(a,b); for(int i = min; i (b - a + 1) * (a + b ) / 2 ​