LV.2 ์ ํ๋ฒํธ ๋ชฉ๋ก
1. ๋ฌธ์ : Link
์ ํ๋ฒํธ ๋ชฉ๋ก์์ ํ ์ ํ๋ฒํธ๊ฐ ๋ค๋ฅธ ์ ํ๋ฒํธ์ ์ ๋์ด๊ฐ ๋๋ ๊ฒฝ์ฐ๊ฐ ์์ผ๋ฉด False return
2. ํ์ด
๋ฐฐ์ด์ ์ํํ๋ฉด์ ๋ฌธ์์ด์ ํ๋์ฉ ๋น๊ต
-> ํ ์คํธ ์ผ์ด์ค๋ ๋ชจ๋ ํต๊ณผํ์ง๋ง ํจ์จ์ฑ์์ runtime error
3. ์ฝ๋
def solution(phone_book):
answer = True
if len(phone_book) == 1:
return answer
phone_book.sort(key = len)
cnt = 0
for i in range(len(phone_book)-1):
for k in range(i+1, len(phone_book)):
for j in range(len(phone_book[i])):
if phone_book[i][j] != phone_book[k][j]:
break
else:
์์ )
def solution(phone_book):
answer = True
dic = {}
for p in phone_book:
dic[p] = 1
for p in phone_book:
tmp = ""
for num in p:
tmp += num
if tmp in dic and tmp!=p:
answer = False
return answer
dic ์ 119๊ฐ ์๊ณ
tmp๊ฐ 119, p๊ฐ 11911์ธ๊ฒฝ์ฐ False๊ฐ ๋์ด
์๋ฐ
import java.util.Arrays;
class Solution {
public boolean solution(String[] phoneBook) {
// 1. phoneBook์ sortingํ๋ค.
Arrays.sort(phoneBook);
// 2. 1์ค Loop์ ๋๋ฉฐ ์ ๋ฒํธ๊ฐ ๋ท ๋ฒํธ์ ์ ๋์ด์ธ์ง ํ์ธํ๋ค.
for (int i = 0; i < phoneBook.length - 1; i++)
if (phoneBook[i + 1].startsWith(phoneBook[i]))
return false;
// 3. ์ฌ๊ธฐ๊น์ง ์ค๋ฉด ์ ๋์ด๊ฐ ์๋ค๋ ๊ฒ์ด๋ค.
return true;
}
}
'๐ป Coding Problems Solving > Hash | HashMap' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[LeetCode] First Unique Character in a String (0) | 2023.07.20 |
---|---|
[LeetCode] Group Anagrams (0) | 2023.07.18 |
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ๋ฐฉ๋ฌธ๊ธธ์ด (์๋ฐ java) (0) | 2023.03.12 |
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ์ฃผ์ฐจ ์๊ธ ๊ณ์ฐ (java) (0) | 2023.03.04 |
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 [3์ฐจ] ์์ถ (0) | 2022.04.25 |
์ต๊ทผ๋๊ธ