https://leetcode.com/problems/minimum-index-sum-of-two-lists/submissions/
Minimum Index Sum of Two Lists - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
Golang~
map 유용하게 사용되니까 진짜러 익숙하게..
그리고 map for loop 순서 보장 안되는거~ 알고 있었지만 한번 더 체크
package main
func main() {
// list1 := []string{"Shogun", "Tapioca Express", "Burger King", "KFC"}
// list2 := []string{"Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"}
list1 := []string{"Shogun", "Tapioca Express", "Burger King", "KFC"}
list2 := []string{"KFC", "Shogun", "Burger King"}
findRestaurant(list1, list2)
}
// 제출 코드
func findRestaurant(list1 []string, list2 []string) []string {
listToMap := make(map[string]int, len(list1))
// result := make(map[string]int)
result := []string{}
for i, v := range list1 {
listToMap[v] = i
}
for i, v := range list2 {
if _, ok := listToMap[v]; ok {
listToMap[v] = listToMap[v] + i
// 밑에서 부터 머리 회전이 안되서 다른거 찾아보면서 참고했다.. 하 .. 진짜 거의 다왔는데 카페가 너무 어수선했음^^
if len(result) == 0 || listToMap[v] == listToMap[result[0]] {
result = append(result, v)
} else if listToMap[v] < listToMap[result[0]] {
result = []string{v}
}
}
}
return result
}
'Algorithm > Go' 카테고리의 다른 글
[LeetCode] Longest Common Prefix (0) | 2022.01.09 |
---|---|
[LeetCode] Longest Substring Without Repeating Characters (0) | 2022.01.03 |
[LeetCode] Merge Two Sorted Lists (0) | 2021.12.26 |
[LeetCode] Contains Duplicate (0) | 2021.12.26 |
[LeetCode] Rotate String (0) | 2021.12.20 |
https://leetcode.com/problems/minimum-index-sum-of-two-lists/submissions/
Minimum Index Sum of Two Lists - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
Golang~
map 유용하게 사용되니까 진짜러 익숙하게..
그리고 map for loop 순서 보장 안되는거~ 알고 있었지만 한번 더 체크
package main
func main() {
// list1 := []string{"Shogun", "Tapioca Express", "Burger King", "KFC"}
// list2 := []string{"Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"}
list1 := []string{"Shogun", "Tapioca Express", "Burger King", "KFC"}
list2 := []string{"KFC", "Shogun", "Burger King"}
findRestaurant(list1, list2)
}
// 제출 코드
func findRestaurant(list1 []string, list2 []string) []string {
listToMap := make(map[string]int, len(list1))
// result := make(map[string]int)
result := []string{}
for i, v := range list1 {
listToMap[v] = i
}
for i, v := range list2 {
if _, ok := listToMap[v]; ok {
listToMap[v] = listToMap[v] + i
// 밑에서 부터 머리 회전이 안되서 다른거 찾아보면서 참고했다.. 하 .. 진짜 거의 다왔는데 카페가 너무 어수선했음^^
if len(result) == 0 || listToMap[v] == listToMap[result[0]] {
result = append(result, v)
} else if listToMap[v] < listToMap[result[0]] {
result = []string{v}
}
}
}
return result
}
'Algorithm > Go' 카테고리의 다른 글
[LeetCode] Longest Common Prefix (0) | 2022.01.09 |
---|---|
[LeetCode] Longest Substring Without Repeating Characters (0) | 2022.01.03 |
[LeetCode] Merge Two Sorted Lists (0) | 2021.12.26 |
[LeetCode] Contains Duplicate (0) | 2021.12.26 |
[LeetCode] Rotate String (0) | 2021.12.20 |