Skip to content

Commit 25b7e8f

Browse files
committed
Merge remote-tracking branch 'origin/main'
2 parents 0cc4f13 + 8f38943 commit 25b7e8f

File tree

14 files changed

+216
-0
lines changed

14 files changed

+216
-0
lines changed

โ€Žcontains-duplicate/JisuuungKim.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def containsDuplicate(self, nums: List[int]) -> bool:
3+
count = {}
4+
5+
for i in nums:
6+
if i in count:
7+
return True
8+
else:
9+
count[i] = 1
10+
11+
return False

โ€Žcontains-duplicate/ayleeee.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Solution:
2+
def containsDuplicate(self, nums: List[int]) -> bool:
3+
return len(set(nums))!=len(nums)
4+

โ€Žcontains-duplicate/froggy1014.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Set์„ ์‚ฌ์šฉํ•œ ์ค‘๋ณต๊ฐ’ ์ œ๊ฑฐ ํ›„ ๊ธธ์ด ๋น„๊ต
2+
function containsDuplicate(nums) {
3+
const numSet = new Set(nums);
4+
return numSet.size !== nums.length;
5+
}
6+
7+
console.log(containsDuplicate([1, 2, 3, 1])); // true
8+
console.log(containsDuplicate([1, 2, 3, 4])); // false
9+
console.log(containsDuplicate([1, 1, 1, 3, 3, 4, 3, 2, 4, 2])); // true
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import java.util.HashSet;
2+
3+
class Solution {
4+
public boolean containsDuplicate(int[] nums) {
5+
HashSet<Integer> numSet = new HashSet<>();
6+
7+
for (int i = 0; i < nums.length; i++) {
8+
9+
if (numSet.contains(nums[i])) {
10+
return true;
11+
}
12+
numSet.add(nums[i]);
13+
}
14+
15+
return false;
16+
}
17+
}

โ€Žcontains-duplicate/toychip.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import java.util.HashSet;
2+
import java.util.Set;
3+
4+
class Solution {
5+
public boolean containsDuplicate(int[] nums) {
6+
Set<Integer> answer = new HashSet<>();
7+
for (int num : nums) {
8+
if (!answer.contains(num)) {
9+
answer.add(num);
10+
} else {
11+
return true;
12+
}
13+
}
14+
return false;
15+
}
16+
}

โ€Žhouse-robber/froggy1014.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var rob = function (nums) {
6+
if (nums.length === 0) return 0;
7+
if (nums.length === 1) return nums[0];
8+
9+
const dp = new Array(nums.length + 1);
10+
dp[0] = 0;
11+
dp[1] = nums[0];
12+
for (let i = 2; i < dp.length; i++) {
13+
dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i - 1]);
14+
}
15+
return dp[dp.length - 1];
16+
};
17+
18+
const nums = [2, 7, 9, 3, 1];
19+
20+
console.log(rob(nums));
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
6+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
7+
var longestConsecutive = function (nums) {
8+
let longest = 0;
9+
10+
let set = new Set(nums);
11+
12+
for (let num of nums) {
13+
if (set.has(num - 1)) {
14+
continue;
15+
}
16+
17+
let count = 1;
18+
let currentNum = num;
19+
20+
while (set.has(currentNum + 1)) {
21+
count++;
22+
currentNum++;
23+
}
24+
longest = Math.max(longest, count);
25+
}
26+
return longest;
27+
};
28+
29+
console.log(longestConsecutive([100, 4, 200, 1, 3, 2])); // 4
30+
console.log(longestConsecutive([0, 3, 7, 2, 5, 8, 4, 6, 0, 1])); // 9
31+
console.log(longestConsecutive([1, 0, 1, 2])); // 3
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
3+
dict_map = {}
4+
for a in nums:
5+
if a in dict_map:
6+
dict_map[a] += 1
7+
else:
8+
dict_map[a] = 1
9+
lst = sorted(dict_map.items(), key=lambda x: x[1], reverse=True) # ๊ณต๋ฐฑ ์ˆ˜์ •
10+
return [item[0] for item in lst[0:k]]
11+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number[]}
5+
*/
6+
var topKFrequent = function (nums, k) {
7+
const map = new Map();
8+
for (let n = 0; n < nums.length; n++) {
9+
map.has(nums[n])
10+
? map.set(nums[n], map.get(nums[n]) + 1)
11+
: map.set(nums[n], 1);
12+
}
13+
14+
return Array.from(map.entries())
15+
.sort(([key1, value1], [key2, value2]) => value2 - value1)
16+
.slice(0, k)
17+
.map((v) => v[0]);
18+
};
19+
20+
console.log(topKFrequent([1, 1, 1, 2, 2, 3], 2));

โ€Žtwo-sum/JisuuungKim.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def twoSum(self, nums: List[int], target: int) -> List[int]:
3+
d = {}
4+
for i in range(len(nums)):
5+
cur = nums[i]
6+
x = target - cur
7+
if x in d:
8+
return [i, d[x]]
9+
else:
10+
d[cur] = i
11+
12+
# ์‹œ๊ฐ„๋ณต์žก๋„ O(n)

โ€Žtwo-sum/ayleeee.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution:
2+
def twoSum(self, nums: List[int], target: int) -> List[int]:
3+
for a in range(len(nums)):
4+
for b in range(a+1,len(nums)):
5+
if nums[a]+nums[b]==target:
6+
return [a,b]

โ€Žtwo-sum/froggy1014.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} target
4+
* @return {number[]}
5+
*/
6+
var twoSum = function (nums, target) {
7+
let map = new Map();
8+
for (let idx = 0; idx < nums.length; idx++) {
9+
const rest = target - nums[idx];
10+
if (map.has(rest)) {
11+
return [map.get(rest), idx];
12+
}
13+
map.set(nums[idx], idx);
14+
}
15+
return [];
16+
};
17+
18+
console.log(twoSum([2, 11, 15, 7], 9));

โ€Žtwo-sum/toychip.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public int[] twoSum(int[] nums, int target) {
3+
for (int i = 0; i < nums.length; i++) {
4+
for (int j = i + 1; j < nums.length; j++) {
5+
if (nums[i] + nums[j] == target) {
6+
return new int[]{i, j};
7+
}
8+
}
9+
}
10+
return null;
11+
}
12+
}

โ€Žvalid-palindrome/Chapse57.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'''
2+
121 ์ด๋ผ๋Š” ์ˆซ์ž์˜ ์˜ค๋ฅธ์กฑ ์ž๋ฆฌ์ˆซ์ž๋ฅผ ์•Œ๊ณ ์‹ถ์œผ๋ฉด
3+
121 %10 =์œผ๋กœ 1์ด๋ผ๋Š” ์ˆซ์ž๋ฅผ ํ™•์ธํ• ์ˆ˜์ž‡๊ณ 
4+
121 /100์œผ๋กœ ์™ผ์ชฝ ์ˆซ์ž๋ฅผ ํ™•์ธ ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ ‘๊ทผ
5+
6+
7+
'''
8+
9+
class Solution(object):
10+
def isPalindrome(self, x):
11+
"""
12+
:type x: int
13+
:rtype: bool
14+
"""
15+
if x <0: return False
16+
div =1
17+
while x >= 10* div:
18+
div *=10
19+
20+
while x:
21+
if x //div != x%10: return False #x //div==์™ผ์ชฝ์ˆซ์ž x%10==์˜ค๋ฅธ์ชฝ์ˆซ์ž
22+
x= (x % div) // 10
23+
div = div/ 100
24+
return True
25+
26+
27+
28+
29+

0 commit comments

Comments
ย (0)