티스토리 뷰
728x90
특정 문자 제거하기 replace 메서드
기존 코드
class Solution {
public String solution(String my_string, String letter) {
String answer = "";
for(char c : letter.toCharArray()){
my_string = my_string.replace(String.valueOf(c), ""); // c를 뺀 문자열 반환
}
return my_string;
}
}
바꾼 코드
class Solution {
public String solution(String my_string, String letter) {
// 기존 풀이는 길었는데 이렇게 간단히 수정 가능.
// replace 메서드 자체가 문자를 치환하는 것이니...
return my_string.replace(letter, "");
}
}
배열 자르기
import java.util.*;
class Solution {
public int[] solution(int[] numbers, int num1, int num2) {
// +1 하는 이유는 copyOfRange는 시작 인덱스 부터 끝 인덱스 까지 배열을 복사
// 즉 num1 = 1, num2 = 3 이면 1~3 3번 앞 인덱스 까지 인데
// 문제에서 요구하는 것은 3번 인덱스 까지 출력 해야 하기 때문에
return Arrays.copyOfRange(numbers, num1, num2+1);
}
}
728x90
'프로그래머스' 카테고리의 다른 글
10일, 11일차.. 진행 기록 (0) | 2023.12.22 |
---|---|
2차원으로 배열 만들기 (0) | 2023.12.16 |
배열 뒤집기 (0) | 2023.11.20 |
프로그래머스 인트 -> 스트링 (0) | 2023.11.20 |
23-11-04 프로그래머스 6일차 (0) | 2023.11.04 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- React
- commit 에러
- th:selected
- reactApp
- 씹어먹는 C 언어
- selectbox
- 다중체크박스 처리
- C언어
- C
- CheckBox
- reactStart
- @reqeustBody
- optional
- App
- 다른데서 react
- JPA
- 제약조건
- @Builder
- 체크박스
- 서버전송
- react 시작 오류
- 셀프로젝트
- findFirstBy
- 받아오기
- SCP
- ID
- mircrosoft visual studio
- 아이디
- 셀렉트박스
- @RequestParam
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
글 보관함