목록코딩테스트/프로그래머스 (53)
Jam's story
public static int solution(int n) { if(n%Math.sqrt(n)==0) { return 1; }else { return 2; } } public static int solution(int n) { Double x=Math.sqrt(n); if(x==x.intValue()) { return 1; }else { return 2; } public static int solution(int n) { return Math.sqrt(n)%1==0? 1:2; } int -> String 1. Integer.toString(int 값) 2.Integer.valueOf(int값) String -> int 1.Integer.valueOf(String값) Double -> int .intVa..
answer배열은 정답을 반환할 배열 answer[0]은 처음 나오는 문자이니 무조건 -1 이중 for문을 사용해서 검사 answer[i]에는 i와 j의 거리를 넣기 stack은 마지막으로 나온 값(j의 값)을 저장하기 위해서 그래서 stack size를 체크해서 똑같은 값이 2개 이상이면, 마지막 값을 pop() 이나 peek()해옴 stack size가 0이라면 -1 import java.util.*; class Solution { public static int[] solution(String s) { char[] arr=s.toCharArray(); int[] answer = new int[arr.length]; answer[0]=-1; Stack can=new Stack(); for (int i..
public static int solution(int n, int t) { int answer = 0; if((1
public class 문자열정렬하기 { public static String solution(String my_string) { String answer = ""; my_string= my_string.toLowerCase(); char[] arr= my_string.toCharArray(); Arrays.sort(arr); answer=new String(arr); System.out.println(answer); return answer; } my_string.toLowerCase().toCharArray() 로 한번에 쓸 수 있다 다른풀이 어제 이 방법으로 풀고싶었는데, 생각이 나지 않았다! 오늘 다시 풀어보니 생각이 났당 ㅋㅋㅋ import java.util.Arrays; public class..

package lv0; public class 잘라서배열로저장하기 { public static String[] solution(String my_str, int n) { int len=(int) Math.ceil((double)my_str.length()/n); int strlen=my_str.length(); System.out.println("len: "+len); System.out.println("my_str: "+strlen); String[] answer = new String[len]; for (int i = 0, j=0; jmy_str.length()) { System.out.println(i); i-=n; System.out.println(i+", "+strlen); answer[j]=m..

String문자열을 String 배열로 쪼개기 String[] str=A.split(" "); 문자열.split(" "); String문자열을 char 배열로 쪼개기 문자열.toCharArray(); 내풀이 package lv0; public class 문자열밀기 { public static int solution(String A, String B) { int answer = 0; int findIdx=0; char[] arrA=A.toCharArray(); char[] arrB=B.toCharArray(); String[] arA=A.split(" "); String[] arB=B.split(" "); for (char c : arrA) { if(c==arrB[0]) { findIdx=A.indexOf..
class Solution { public int solution(int M, int N) { int answer = 0; if (M
첫번째 간격과 두번째 간격이 같다면 등차수열이니 if문에서는 등차수열을 구해주고 else문에서는 등비수열을 구해준다. public int solution(int[] common) { int answer = 0; if((common[1]-common[0] )==(common[2]-common[1] )) { answer=common[common.length-1]+(common[1]-common[0] ); }else { answer=common[common.length-1]*(common[1]/common[0] ); } return answer; } 다른사람풀이 common[1] - common[0]를 변수에 저장하여 간결성 높이기 class Solution { public int solution(int[] ..