일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- ㅒ
- 티스토리 쿠키 삭제
- resttemplate
- d
- /etc/network/interfaces
- Kernighan의 C언어 프로그래밍
- 데비안
- 서버설정
- 친절한SQL튜닝
- 이터레이터
- 코드로배우는스프링부트웹프로젝트
- 알파회계
- 구멍가게코딩단
- GIT
- baeldung
- 스프링 시큐리티
- 자바편
- network configuration
- 목록처리
- 자료구조와 함께 배우는 알고리즘 입문
- 처음 만나는 AI 수학 with Python
- 네트워크 설정
- 처음 만나는 AI수학 with Python
- 자료구조와함께배우는알고리즘입문
- 페이징
- 리눅스
- 선형대수
- 코드로배우는스프링웹프로젝트
- iterator
- 스프링부트핵심가이드
- Today
- Total
bright jazz music
숫자 문자열과 영단어 본문
요구사항:
숫자의 일부 자릿수가 영단어로 바뀌어졌거나,
혹은 바뀌지 않고 그대로인 문자열 s가 매개변수로 주어진다.
s가 의미하는 원래 숫자를 return 하도록 solution 함수를 완성하라
class Solution {
public int solution(String s) {
String [] words = {"zero", "one", "two", "three", "four"
,"five", "six", "seven"
,"eight", "nine"};
for(int i = 0 ; i < words.length; i++) {
s = s.replace(words[i], String.valueOf(i));
}
//words배열의 인덱스에 대응하는 숫자값 i를 i로 대체할 것
//반복문이 끝나면 s의 모든 영문자가 숫자로 바뀐다.
return Integer.parseInt(s);
//s를 인트로 바꿔서 반환
}
}
//보다 나은 풀이
class Solution {
public int solution(String s) {
String[] strArr = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
for(int i = 0; i < strArr.length; i++) {
s = s.replaceAll(strArr[i], Integer.toString(i));
//Integer.toString은 Number객체의 값을 String객체의 값으로 변환한다
}
return Integer.parseInt(s);
}
}
Integer.parseInt()
String 값을 정수 형태로 바꿔준다.
자세한 내용은 아래의 공식 문서를 확인할 것.
https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html
Integer (Java Platform SE 8 )
Returns the value obtained by rotating the two's complement binary representation of the specified int value left by the specified number of bits. (Bits shifted out of the left hand, or high-order, side reenter on the right, or low-order.) Note that left r
docs.oracle.com
String.replace(oldChar, newChar)
oldChar를 newChar로 바꿔준다.
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#replace-char-char-
String (Java Platform SE 8 )
Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argum
docs.oracle.com
String.valueOf()
배열에서 해당 값을 반환
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#valueOf-char:A-
String (Java Platform SE 8 )
Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argum
docs.oracle.com
'Algorithm&Data structure' 카테고리의 다른 글
배열을 역순으로 정렬하기 (0) | 2022.05.02 |
---|---|
신고 결과 받기 (0) | 2022.04.19 |
Binary Search (이진검색) (0) | 2021.12.22 |