File tree Expand file tree Collapse file tree 3 files changed +68
-0
lines changed
longest-repeating-character-replacement Expand file tree Collapse file tree 3 files changed +68
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ *
3+ * @param {string } s
4+ * @param {number } k
5+ * @return {number }
6+ */
7+ var characterReplacement = function ( s , k ) {
8+ let start = 0 ;
9+ let maxLength = 0 ;
10+ let maxCharCount = 0 ;
11+ let count = { } ;
12+
13+ for ( let end = 0 ; end < s . length ; end ++ ) {
14+ const char = s [ end ] ;
15+ count [ char ] = ( count [ char ] || 0 ) + 1 ;
16+
17+ // 현재 윈도우 안에서 가장 많이 나온 문자 수 갱신
18+ maxCharCount = Math . max ( maxCharCount , count [ char ] ) ;
19+
20+ // 바꿔야 하는 문자 수가 k보다 많으면 → 왼쪽 포인터 이동
21+ if ( end - start + 1 - maxCharCount > k ) {
22+ count [ s [ start ] ] -- ;
23+ start ++ ;
24+ }
25+
26+ // 현재 윈도우 길이로 최대 길이 갱신
27+ maxLength = Math . max ( maxLength , end - start + 1 ) ;
28+ }
29+
30+ return maxLength ;
31+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * https://leetcode.com/problems/reverse-bits/
3+ * @param {number } n - a positive integer
4+ * @return {number } - a positive integer
5+ */
6+ var reverseBits = function ( n ) {
7+ let result = 0 ;
8+
9+ for ( let i = 0 ; i < 32 ; i ++ ) {
10+ // result를 왼쪽으로 1칸 밀기
11+ result <<= 1 ;
12+
13+ // n의 마지막 비트를 result의 오른쪽 끝에 추가
14+ result |= n & 1 ;
15+
16+ // n을 오른쪽으로 1칸 밀기
17+ n >>>= 1 ;
18+ }
19+
20+ // >>> 0을 하면 부호 없는 32비트 정수로 반환됨
21+ return result >>> 0 ;
22+ } ;
Original file line number Diff line number Diff line change @@ -34,4 +34,19 @@ var reverseList = function (head) {
3434reverseList(head)에서 head는 리스트 전체의 진입점.
3535head 하나만 알고 있어도, .next를 따라가면서 전체 노드들을 순차적으로 접근할 수 있기 때문에 리스트 전체를 다룰 수 있음
3636
37+ // 노드 구조 정의
38+ function ListNode(val, next = null) {
39+ this.val = val;
40+ this.next = next;
41+ }
42+
43+ // 리스트 만들기
44+ const node3 = new ListNode(3); // 마지막 노드
45+ const node2 = new ListNode(2, node3); // node2 → node3
46+ const head = new ListNode(1, node2); // head → node2 → node3
47+
48+ // 확인
49+ console.log(head.val); // 1
50+ console.log(head.next.val); // 2
51+ console.log(head.next.next.val); // 3
3752 */
You can’t perform that action at this time.
0 commit comments