프로그래머스 ( 0 떼기 )
public class problem389 {
public class Solution {
public String solution(String n_str) {
int idx = 0;
// 0을 만날때까지
while (n_str.charAt(idx) == '0') {
idx++;
}
// 0으로만 이루어진 문자열일 경우
if (idx == n_str.length()) {
return "0";
}
return n_str.substring(idx);
}
}
}
프로그래머스 (배열 만들기 3)
import java.util.ArrayList;
import java.util.List;
public class problem390 {
class Solution {
public int[] solution(int[] arr, int[][] intervals) {
List<Integer> result = new ArrayList<>();
for (int i = 0; i < intervals.length; i++) {
int num1 = intervals[i][0]; // start 인덱스
int num2 = intervals[i][1]; // end 인덱스
for (int j = num1; j <= num2; j++) {
result.add(arr[j]);
}
}
int[] answer = new int[result.size()];
for (int i = 0; i < answer.length; i++) {
answer[i] = result.get(i);
}
return answer;
}
}
}
프로그래머스 (가까운 1찾기)
public class problem391 {
class Solution {
public int solution(int[] arr, int idx) {
int answer = -1;
// idx 부터 순회하여 찾는다.
for (int i = idx; i < arr.length; i++) {
if (arr[i] == 1) {
answer = i;
break;
}
}
return answer;
}
}
}
프로그래머스 (문자열 잘라서 정렬하기)
import java.util.Arrays;
public class problem392 {
class Solution {
public String[] solution(String myString) {
String[] answer = myString.split("x");
int count = 0;
// 빈 문자열일경우
// 예시 "xabcxdefx"
for (String str : answer) {
if (!str.isEmpty()) {
count++;
}
}
// 빈 문자열을 제외하고 저장
String[] result = new String[count];
int index = 0;
for (String str : answer) {
if (!str.isEmpty()) {
result[index++] = str;
}
}
Arrays.sort(result);
return result;
}
}
}
프로그래머스 (수열과 구간 쿼리 3)
import java.util.Arrays;
public class problem393 {
class Solution {
public int[] solution(int[] arr, int[][] queries) {
// arr 배열의 변경이 answer 배열에 영향을 주지 않기 때문에 복사
int[] answer = Arrays.copyOf(arr, arr.length);
for (int[] q : queries) {
int start = q[0]; // start 인덱스
int end = q[1]; // end 인덱스
int temp = answer[start];
answer[start] = answer[end];
answer[end] = temp;
}
return answer;
}
}
}