public class StringtoInt { public static void main(String[] args) {
/* * 문자열 s에 나타나는 문자를 큰것부터 작은 순으로 정렬해 새로운 문자열을 리턴하는 함수, solution을 완성해주세요. s는 영문 * 대소문자로만 구성되어 있으며, 대문자는 소문자보다 작은 것으로 간주합니다. * * 제한 사항 str은 길이 1 이상인 문자열입니다. */
String s = "Zbcdefg";
//String 변수안에 담긴 문자열들을 .split()을 통해 한글자씩 배열로 분리 String[] str = s.split("");
int ok_give = 0; //체육복을 빌려줄수있는 학생 수 int duplicate = 0; // 중복 처리 // 잃어버린 학생의 수 int lost_length = lost.length; // 여벌 체육복이 있는 학생의 수 int reserve_length = reserve.length;
for(int i=0; i<reserve.length; i++) { duplicate=0; int reserve_plus = reserve[i]+1; int reserve_minus = reserve[i]-1; for(int j=0; j<lost.length; j++){ int lost_index = lost[j]; if(lost_index == reserve_plus) { ok_give++; duplicate++; } if(lost_index == reserve_minus) { ok_give++; duplicate++; } if(duplicate == 2){ ok_give--; } } } int answer = 0; int ok = 0; if(lost.length < ok_give) { ok = ok_give - lost.length; answer = n-lost.length+ok_give-ok;
// 전체 학생수 - 잃어버린 학생의 수 + 여벌의 체육복을 가져와서 빌려줄수있는 학생의 수 + 중복 제거 }else{ answer = n-lost.length+ok_give;
// 전체 학생수 - 잃어버린 학생의 수 + 여벌의 체육복을 가져와서 빌려줄수있는 학생의 수 } return answer; } }
코드실행은 성공했으나
체점을 하니 65점이나왔다.
도중에 테스트케이스를 뭘 놓친건지..
테스트케이스를 추가해서 다시 풀어봐야겠다..
다른 사람의 풀이법
import java.util.Arrays;
classSolution{
publicintsolution(int n, int[] lost, int[] reserve) {
int answer = n - lost.length; Arrays.sort(lost); Arrays.sort(reserve); // 여벌 체육복을 가져온 학생이 도난당한 경우for(int i=0; i<lost.length; i++) {