[JAVA 문제 풀이] 328. 동영상 재생기

프로그래머스 (340213)
Stupefyee's avatar
Jun 19, 2025
[JAVA 문제 풀이] 328. 동영상 재생기
notion image
 

내가 작성한 코드

class Solution { public String solution(String video_len, String pos, String op_start, String op_end, String[] commands) { // 각 분:초를 총 초로 변환 int videoLen = convertTimeToSec(video_len); int posTime = convertTimeToSec(pos); int opStart = convertTimeToSec(op_start); int opEnd = convertTimeToSec(op_end); // 시작 위치가 이미 오프닝 구간일 경우 건너 뛰기 if (opStart <= posTime && posTime <= opEnd) { posTime = opEnd; } for (String command : commands) { if (command.equals("prev")) { posTime -= 10; } else { posTime += 10; } // 영상 범위 처리 if (posTime < 0) { posTime = 0; } else if (posTime > videoLen) { posTime = videoLen; } // 이동한 위치가 오프닝 구간일 경우 건너 뛰기 if (opStart <= posTime && posTime <= opEnd) { posTime = opEnd; } } return convertSecToTime(posTime); } // 분:초를 총 초로 변환하는 메서드 private int convertTimeToSec(String time) { String[] arr = time.split(":"); return Integer.parseInt(arr[0]) * 60 + Integer.parseInt(arr[1]); } // 총 초를 분:초로 변환하는 메서드 private String convertSecToTime(int sec) { String min = sec / 60 + ""; String sec2 = sec % 60 + ""; if (min.length() == 1) { min = "0" + min; } if (sec2.length() == 1) { sec2 = "0" + sec2; } return min + ":" + sec2; } }
Share article

stupefyee