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 | 29 |
30 | 31 |
Tags
- bit manipulation
- database
- greedy
- 자바
- geometry
- Binary Tree
- array
- simulation
- Class
- Binary Search
- 파이썬
- Counting
- Tree
- 구현
- sorting
- 코딩테스트
- hash table
- dynamic programming
- string
- Data Structure
- Stack
- SQL
- Math
- Number Theory
- implement
- 코테
- two pointers
- Matrix
- java
- Method
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 1758. Minimum Changes To Make Alternating Binary String 본문
코딩테스트 풀이/JAVA
[LeetCode/Easy] 1758. Minimum Changes To Make Alternating Binary String
무지맘 2023. 6. 19. 13:021. Input
1) String s
2. Output
1) s를 0과 1이 번갈아가며 나타나는 이진 문자열로 바꾸는데 필요한 최소 변환 횟수를 반환
3. Constraint
1) 1 <= s.length <= 10^4
2) s는 0과 1로 이루어진 문자열이다.
4. Example
Input: s = "0100" -> Output: 1
설명: “1010”으로 바꾸는 데 필요한 변환은 4번, “0101”로 바꾸는 데 필요한 변환은 1번이므로 1을 반환한다.
5. Code
1) 첫 코드(2023/06/19)
class Solution {
public int minOperations(String s) {
char[] c = s.toCharArray();
int c1 = 0, c2 = 0;
for(int i=0 ; i<c.length ; i++){
if(i%2==0){
if(c[i]!='0') c1++;
else c2++;
} else{
if(c[i]!='1') c1++;
else c2++;
}
}
return Math.min(c1, c2);
}
}
- 99%, 77%
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[백준 온라인 저지] 1948. 임계경로 (0) | 2023.06.20 |
---|---|
[백준 온라인 저지] 1516. 게임 개발 (0) | 2023.06.19 |
[LeetCode/Easy] 1668. Maximum Repeating Substring (0) | 2023.06.19 |
[백준 온라인 저지] 2252. 줄 세우기 (0) | 2023.06.14 |
[LeetCode/Easy] 1629. Slowest Key (0) | 2023.06.13 |