Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 백준
- 소수찾기 java
- aop
- S3
- multimodule testcontainers
- kotest testcontainers
- OptimisticLock
- spring aop
- DI
- RefreshToken
- interface
- 알고리즘
- 우아한 테크러닝
- 형상관리
- 멀티모듈 테스트컨테이너
- ObjectOptimisticLockingFailureException
- 낙관적 락 재시도
- spring DI
- jpa
- Spring Cloud Gateway
- java
- 낙관적 락 롤백
- springsecurity
- redissonlock aop
- Invalid property 'principal.username' of bean class
- @transactional
- netty
- TestContainers
- ObjectOptimisticLockingFailureException 처리
- AccessToken
Archives
- Today
- Total
조급하면 모래성이 될뿐
[프로그래머스] 올바른 괄호 본문
문제 링크 : https://programmers.co.kr/learn/courses/30/lessons/12909
코드
class Solution {
boolean solution(String s) {
boolean answer = true;
char[] arrayS = s.toCharArray();
int result = 0 ;
for ( char parenthesis : arrayS ) {
if ( parenthesis == '(' ) {
result++;
}else if ( parenthesis == ')' ) {
result--;
}
if ( result < 0 ) return false;
}
if ( result > 0 ) return false;
return answer;
}
}
나의 풀이
이 전에 유사한 문제를 풀어 봤던 경험이 있어서 어렵지 않게 풀어냈다.
입력받은 문자열 s를 분리한 후에
'('인 경우에는 result를 ++ 해주고
')'인 경우에는 result를 -- 해주었다.
result가 음수가 되는 경우는 '(' 열리기 전에 ')' 단힌 경우이므로 바로 false를 return 하였고
모든 문자열 처리가 끝난 이후에 0보다 큰 경우도 '('가 더 많다는 의미가 되므로 false를 return 하였다.
result가 0 인 경우는 열린 만큼 닫은 것이기 때문에 true를 return 한다.
제출 결과
반응형
'Algorithm > Programmers' 카테고리의 다른 글
[프로그래머스]숫자의 표현 (0) | 2020.01.04 |
---|---|
[프로그래머스] 다음 큰 숫자 (0) | 2019.12.12 |
[프로그래머스] 가장 큰 정사각형 찾기 (0) | 2019.12.12 |
[프로그래머스] 라면공장 (0) | 2019.12.12 |
[프로그래머스] 타겟 넘버 (0) | 2019.12.11 |