This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func constructRectangle(area int) []int { | |
| res := 1 | |
| for i := 2; i * i <= area; i++ { | |
| if area % i == 0 { | |
| res = i | |
| } | |
| } | |
| return []int{area / res, res} | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func minMoves(nums []int) int { | |
| sort.Ints(nums) | |
| min := nums[0] | |
| sum :=0 | |
| for _, v := range nums { | |
| sum +=v | |
| } | |
| return sum - min * len(nums) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func intersection(nums1 []int, nums2 []int) []int { | |
| var temp []int | |
| if len(nums1) == 0 || len(nums2)==0 { | |
| return temp | |
| } | |
| temp1 := make(map[int]int) | |
| temp2 := make(map[int]int) | |
| for _, v := range nums1 { | |
| temp1[v] = v | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func canConstruct(ransomNote string, magazine string) bool { | |
| m := [26]int{} | |
| for _, v := range magazine { | |
| m[v-'a']++ | |
| } | |
| for _, v := range ransomNote { | |
| if m[v-'a'] == 0 { | |
| return false | |
| } | |
| m[v-'a']-- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func twoSum(numbers []int, target int) []int { | |
| if len(numbers) <2 { | |
| return nil | |
| } | |
| i :=0 | |
| j := len(numbers)-1 | |
| for i< j { | |
| sum := numbers[i] + numbers[j] | |
| if sum == target { | |
| return []int{i+1, j+1} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func findRestaurant(list1 []string, list2 []string) []string { | |
| temp1 := make(map[string]int) | |
| temp2 := make(map[string]int) | |
| temp3 := make(map[string]int) | |
| min := 0 | |
| temp :=[]int{} | |
| result := []string{} | |
| for k,v := range list1{ | |
| temp1[v]= k |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func majorityElement(nums []int) int { | |
| temp := make(map [int]int) | |
| result :=nums[0] | |
| if len(nums) == 1 { | |
| return result | |
| } | |
| for _, v := range nums { | |
| temp[v]++ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func titleToNumber(s string) int { | |
| sum := 0 | |
| l := len(s) -1 | |
| for i :=len(s)-1; i>=0; i-- { | |
| sum += int((s[i]-64)) * int(math.Pow(float64(26), float64(l-i) )) | |
| } | |
| return sum | |
| } | |
| //----------------------------------------------------------------- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func firstUniqChar(s string) int { //it is fast | |
| m := [26]int{} | |
| for _, ch := range s { | |
| m[int(ch-'a')]++ | |
| } | |
| for k, ch := range s { | |
| if m[int(ch-'a')] == 1 { | |
| return k | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func isAnagram(s string, t string) bool { | |
| if len(s) != len(t) { | |
| return false | |
| } | |
| if len(s) ==0 && len(t) ==0 { | |
| return true | |
| } | |
| temp1 := make(map[int32]int) | |