250x250
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
- Calendar
- 큐
- dfs
- Properties
- 힙덤프
- NIO
- map
- deque
- alter
- math
- 리소스모니터링
- List
- BFS
- javascript
- priority_queue
- set
- date
- Java
- union_find
- 스택
- 스프링부트
- spring boot
- JPA
- html
- CSS
- sql
- scanner
- GC로그수집
- string
- Union-find
Archives
- Today
- Total
매일 조금씩
프로그래머스 level1 : 문자열 나누기 - Java 본문
728x90
반응형
1. String[] 으로 만들어서 x와 비교, xcnt, noxcnt 같아지면 answer++
class Solution {
public int solution(String s) {
int answer = 0;
int xcnt = 0;
int noxcnt = 0;
String[] c = s.split("");
String x = c[0];
int xidx = 0;
for(int i = 0; i < c.length; i++){
if(xcnt == 0){
xidx = i;
x = c[i];
}
if(x.equals(c[i])){
xcnt++;
}else{
noxcnt++;
}
if(xcnt == noxcnt){
answer++;
xcnt = 0;
noxcnt = 0;
}
}
if(xcnt != noxcnt){
answer++;
}
return answer;
}
}
2. toCharArray()로 char[]만들어서 x와 비교, x를 set하면서 answer++
class Solution {
public int solution(String s) {
int answer = 0;
int same = 0;
int diff = 0;
char x = ' ';
boolean setx = false;
for(char c: s.toCharArray()){
if(!setx){
x = c;
setx = true;
answer++;
}
if(x == c) same++;
else diff++;
if(same == diff){
setx = false;
same = 0;
diff = 0;
}
}
return answer;
}
}
728x90
반응형
'알고리즘' 카테고리의 다른 글
프로그래머스 level1 : 기사단원의 무기 - Java (0) | 2023.06.05 |
---|---|
프로그래머스 level1 : 명예의 전당(1) - Java (0) | 2023.06.05 |
프로그래머스 level1 : 가장 가까운 같은 글자 - Java (0) | 2023.06.02 |
프로그래머스 level1 : 둘만의 암호 - Java (0) | 2023.06.02 |
프로그래머스 level1 : 카드 뭉치 - Java (0) | 2023.06.02 |