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 peakIndexInMountainArray(A []int) int { | |
| start, end:= 0, len(A) -1 | |
| mid := 0 | |
| for start < end { | |
| mid = (start+ end ) /2 | |
| if A[mid-1] < A[mid] && A[mid] <A[mid+1] { | |
| start = mid | |
| } else if A[mid-1] > A[mid] && A[mid] > A[mid+1] { | |
| end = mid |
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
| import "strings" | |
| func judgeCircle(moves string) bool { | |
| up := strings.Count(moves, "U") | |
| down := strings.Count(moves, "D") | |
| right := strings.Count(moves, "R") | |
| left := strings.Count(moves, "L") | |
| return up == down && right == left | |
| } |
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 hammingDistance(x int, y int) int { | |
| x ^= y | |
| res := 0 | |
| for x > 0 { | |
| res += x & 1 | |
| x >>= 1 | |
| } | |
| return 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 uniqueMorseRepresentations(words []string) int { | |
| morseCode := []string{".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."} | |
| ascii_lowercase := "abcdefghijklmnopqrstuvwxyz" | |
| codeMap := make(map[byte]string, len(morseCode)) | |
| strMap := make(map[string]bool) | |
| for i := range ascii_lowercase { | |
| codeMap[ascii_lowercase[i]] = morseCode[i] | |
| } |
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
| def uniqueMorseRepresentations(self, words): | |
| """ | |
| :type words: List[str] | |
| :rtype: int | |
| """ | |
| morseCode =[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."] | |
| mdict = {c : m for c, m in zip(string.ascii_lowercase, morseCode)} | |
| return len(set(''.join(map(mdict.get, w)) for w in words)) | |
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
| def toLowerCase(self, str): | |
| """ | |
| :type str: str | |
| :rtype: str | |
| """ | |
| result = "" | |
| for s in str: | |
| if ord(s) >= ord('A') and ord(s) <= ord('Z'): | |
| result+= chr(ord(s) + 32) | |
| else: |
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 toLowerCase(str string) string { | |
| // optimize for ASCII-only strings. | |
| b := make([]byte, len(str)) | |
| for i := 0; i < len(str); i++ { | |
| c := str[i] | |
| if c >= 'A' && c <= 'Z' { | |
| c += 'a' - 'A' | |
| } | |
| b[i] = c | |
| } |
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 numJewelsInStones(J string, S string) int { | |
| isJewel := make(map[byte]bool, len(J)) | |
| count := 0 | |
| for j := range J { | |
| isJewel[J[j]] = true | |
| } | |
| for s := range S { | |
| if isJewel[S[s]] { | |
| count ++ |
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 numJewelsInStones(J string, S string) int { | |
| count := 0 | |
| for _, s:= range S { | |
| for _,j := range J { | |
| if s == j { | |
| count +=1 | |
| } | |
| } | |
| } | |
| return count |
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 isValid(s string) bool { | |
| stack := []rune{} | |
| for _, b := range s { | |
| switch b { | |
| case '(', '[', '{': | |
| stack = append([]rune{b}, stack...) | |
| default: | |
| if len(stack) > 0 { | |
| c := stack[0]; stack = stack[1:] | |
| switch c { |