Skip to content

Commit 671a3f6

Browse files
committed
fix: 로직 수정
1 parent a2e0835 commit 671a3f6

File tree

4 files changed

+13
-32
lines changed

4 files changed

+13
-32
lines changed

contains-duplicate/YeomChaeeun.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ function containsDuplicate(nums: number[]): boolean {
2626
let obj={}
2727

2828
for(let i = 0; i < nums.length; i++) {
29-
console.log('nums >>', nums[i], 'obj >>', obj)
3029
if(obj[nums[i]]) {
3130
return true;
3231
}

longest-consecutive-sequence/YeomChaeeun.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,17 @@ function longestConsecutive(nums: number[]): number {
2828
// =========
2929

3030
let longest = 0;
31-
let temp = 1;
32-
31+
let current = 1;
3332
for(let i = 0; i < nums.length-1; i++) {
34-
if(nums[i] === nums[i + 1]) {
35-
// console.log(nums[i], '===', nums[i+1])
33+
if(nums[i] === nums[i - 1]) {
3634
continue;
3735
} else if(nums[i] + 1 === nums[i + 1] ) {
38-
// console.log(nums[i], '+ 1 =', nums[i+1])
39-
temp += 1;
36+
current += 1;
4037
} else {
41-
// console.log(longest, ' - ', temp)
42-
longest = Math.max(temp, longest);
43-
temp = 1;
38+
longest = Math.max(current, longest);
39+
current = 1;
4440
}
4541
}
4642

47-
// i가 마지막인 경우 for문의 else문을 타지 않으므로 다시 한번 체크함
48-
longest = Math.max(temp, longest);
4943
return longest;
5044
};

top-k-frequent-elements/YeomChaeeun.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,8 @@ function topKFrequent(nums: number[], k: number): number[] {
1212
++obj[num]
1313
}
1414

15-
// 배열로 변경
16-
let entries = Object.entries(obj)
17-
// 정렬
18-
let sort_arr = entries.sort((a, b) => Number(b[1]) - Number(a[1]));
19-
20-
let result = [];
21-
let l = 0;
22-
for(const item of sort_arr) {
23-
if(l == k) break;
24-
result.push(Number(item[0]));
25-
l++;
26-
}
27-
28-
return result;
15+
return Object.entries(obj)
16+
.sort((a, b) => Number(b[1]) - Number(a[1]))
17+
.slice(0, k)
18+
.map(entry => Number(entry[0]));
2919
};

valid-palindrome/YeomChaeeun.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@
44
*/
55
function isPalindrome(s: string): boolean {
66
let word = s.toLowerCase();
7-
const reg = /[\{\}\[\]\/?.,;:|\)*~`!^\-_+<>@\#$%&\\\=\(\'\" ]/g;
8-
7+
const reg = /[^a-z0-9]/g; // removing all non-alphanumeric characters
98
word = word.replace(reg, '');
9+
1010
for(let i = 0; i < word.length; i++) {
11-
for(let j = word.length-i-1; j >= word.length-i-1; j--) {
12-
// console.log(word[i], '===', word[j])
13-
if(word[i] !== word[j]) return false;
14-
}
11+
if(word[i] !== word[word.length-i-1])
12+
return false;
1513
}
1614
return true;
1715
};

0 commit comments

Comments
 (0)