Skip to content

Instantly share code, notes, and snippets.

View luojiyin1987's full-sized avatar
💭
I may be slow to respond.

luo jiyin luojiyin1987

💭
I may be slow to respond.
View GitHub Profile
@luojiyin1987
luojiyin1987 / Peak_Index_in_a_Mountain_Array.go
Created July 20, 2018 15:23
Peak Index in a Mountain Array
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
@luojiyin1987
luojiyin1987 / Judge_Route_Circle.go
Created July 20, 2018 13:58
Judge Route Circle
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
}
@luojiyin1987
luojiyin1987 / Hamming_Distance.go
Created July 20, 2018 11:59
Hamming Distance
func hammingDistance(x int, y int) int {
x ^= y
res := 0
for x > 0 {
res += x & 1
x >>= 1
}
return res
@luojiyin1987
luojiyin1987 / Unique_Morse_Code_Words.go
Created July 20, 2018 11:29
Unique Morse Code Words
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]
}
@luojiyin1987
luojiyin1987 / Unique_Morse_Code_Words.py
Last active July 20, 2018 10:12
#Unique Morse Code Words
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))
@luojiyin1987
luojiyin1987 / To_Lower_Case.py
Created July 20, 2018 05:45
#leetcode #Python3
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:
@luojiyin1987
luojiyin1987 / To_Lower_Case.go
Created July 20, 2018 05:37
#leetcode #golang
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
}
@luojiyin1987
luojiyin1987 / Jewels_and_Stones_use_map.go
Created July 20, 2018 04:51
#golang #map #leetcode
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 ++
func numJewelsInStones(J string, S string) int {
count := 0
for _, s:= range S {
for _,j := range J {
if s == j {
count +=1
}
}
}
return count
@luojiyin1987
luojiyin1987 / vaild_parar.go
Created July 19, 2018 14:59
#golang #leetcode
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 {