hibernate
2019-11-07
2019-10-18
12시간제를 24시간제로 시간증가하여 변환
목표
자연수 N을 오름차순과 내림차순으로 각각 정렬하여 두 값을 합산한 결과를 반환하시요
조건
- time의 값은 오전과 오후가 각각 “AM”, “PM” 으로 표시하며 12시간제로 “시:분:초” 의 형태를 가짐
- 시분초는 한자리 수라도 두자리로 표시
- N <= 200,000
예시
| time | n | result |
|---|---|---|
| AM 12:01:00 | 1 | 00:01:01 |
| PM 12:01:00 | 2 | 12:01:02 |
| PM 09:01:40 | 50 | 21:02:10 |
| AM 11:01:00 | 3700 | 00:02:40 |
코드
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Solution {
//p = 시간 , n = 증가할 sec
public String solution(String time, int n) throws IllegalArgumentException {
String answer = null;
Matcher matcher = Pattern.compile("(\\w+) (\\d+):(\\d+):(\\d+)").matcher(time);
if(matcher.find()){
boolean isPm = matcher.group(1).toUpperCase().equals("PM");
int hour = Integer.parseInt(matcher.group(2));
int min = Integer.parseInt(matcher.group(3));
int sec = Integer.parseInt(matcher.group(4)) + n;
min += sec/60;
sec %= 60;
hour += min/60;
min %= 60;
if(isPm && hour!=12){
hour += 12;
if(hour > 24){
hour %= 24;
}
}
if(!isPm && hour==12){
hour -= 12;
}
answer = String.format("%02d:%02d:%02d",hour,min,sec);
return answer;
}
throw new IllegalArgumentException("time format is illegal");
}
}
숫자를 정렬,역정렬하여 합산
목표
자연수 N을 오름차순과 내림차순으로 각각 정렬하여 두 값을 합산한 결과를 반환하시요
조건
- N >= 1 || N <= 1,000,000,000
예시
| N | process | result |
|---|---|---|
| 2613 | 1236 + 6321 | 7557 |
| 33285 | 23358 + 85332 | 108690 |
코드
import java.util.*;
public class Solution {
public int solution(int N) {
int answer = -1;
char[] splitNum = String.valueOf(N).toCharArray();
Arrays.sort(splitNum);
answer = Integer.parseInt(new String(splitNum));
String reverseNum = "";
for(int i=splitNum.length-1; i>=0; i--){
reverseNum += splitNum[i];
}
answer += Integer.parseInt(reverseNum);
return answer;
}
}
쌍이 아닌 배열값 검사
목표
cards 배열에서 쌍이 아닌 개체값 을 반환하시요.조건
- cards.length = N * 2 - 1
- N >= 1,000,000
- cards 개체값 >= 100,000,000
예시
| cards | answer |
|---|---|
| [1,3,2,2,5,5,1] | 3 |
| [7,2,3,9,1,2,5,3,9,7,1] | 5 |
코드
import java.util.*;
import java.util.Arrays;
class Solution {
public int solution(int[] cards) throws NoSuchElementException{
// cards.length = 짝수
// cards = 쌍으로 값이 존재
Map<Integer,Integer> checkMap = new HashMap<>();
for(int i=0;cards.length>i; i++){
if(checkMap.containsKey(cards[i])){
checkMap.put(cards[i],2);
}else{
checkMap.put(cards[i],1);
}
}
for (Integer card : checkMap.keySet()){
if (checkMap.get(card).equals(1)) {
return card;
}
}
throw new NoSuchElementException("not found unpaired card");
}
}
정보를 저장하는 구조, Data Structure
자료구조 (Data Structure)

자료 구조란,
기본 자료형 (Primitive Data Structure)
생략
비기본 자료형, 사용자 정의 자료형 (Non-Primitive Data Structure)
선형 자료구조 (Linear Data Structure)
Arrays
Linked List
Stack
Queue
비선형 자료구조 (Non-Linear Data Structure)
Tree
Graphs
Written 10월 18, 2019 by sonjm
No comments2019-07-17
시간 문자열 조정하기
목표
“PM 12:01:00” 형태의 문자열 time 과 증가될 초인 sec를 합쳐 24시간 표기법으로 전환한다
“PM 11:01:00”, 10 => “23:01:10”
“AM 05:01:00”, 100 => “05:02:40”
조건
- sec 의 범위는 0 ~ 10,000,000
- 24시에 도달한 경우, 날짜는 관계없이 0시부터
코드
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Solution {
public String solution(String time, int sec) {
Matcher matcher = Pattern.compile("(\\w+) (\\d+):(\\d+):(\\d+)").matcher(time);
if(matcher.find()){
int h = Integer.parseInt(matcher.group(2));
int m = Integer.parseInt(matcher.group(3));
int s = Integer.parseInt(matcher.group(4)) + sec;
if(matcher.group(1).equalsIgnoreCase("pm")){
h += 12;
}
m += s/60; s %= 60;
h += m/60; m %= 60;
h %= 24;
return String.format("%02d:%02d:%02d", h, m, s);
}
return "date foramt error";
}
}
Written 7월 17, 2019 by sonjm
No comments
피드 구독하기:
글 (Atom)

