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 / Letter Case Permutation.py
Created July 24, 2018 07:38
Letter Case Permutation
class Solution:
def letterCasePermutation(self, S):
"""
:type S: str
:rtype: List[str]
"""
if not S: return [S]
rest = self.letterCasePermutation(S[1:])
if S[0].isalpha():
return [S[0].lower() + s for s in rest] + [S[0].upper() + s for s in rest]
@luojiyin1987
luojiyin1987 / Max Consecutive Ones.go
Created July 24, 2018 03:02
Max Consecutive Ones
class Solution:
def findMaxConsecutiveOnes(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
maxNum = 0
count = 0
for num in nums:
@luojiyin1987
luojiyin1987 / Max Consecutive Ones.go
Created July 24, 2018 02:57
Max Consecutive Ones
func findMaxConsecutiveOnes(nums []int) int {
max := 0
count := 0
for i := range nums{
if nums[i] == 1 {
count++
} else if nums[i] == 0 {
if max < count {
max = count
}
@luojiyin1987
luojiyin1987 / Prime Number of Set Bits in Binary Representation.py
Created July 24, 2018 02:37
Prime Number of Set Bits in Binary Representation
func countPrimeSetBits(L int, R int) int {
primes := [...]int{2: 1, 3: 1, 5: 1, 7: 1, 11: 1, 13: 1, 17: 1, 19: 1}
res := 0
for i := L; i <= R; i++ {
bits := 0
for n := i; n > 0; n >>= 1 {
bits += n & 1
}
res += primes[bits]
@luojiyin1987
luojiyin1987 / Binary Number with Alternating Bits.go
Created July 24, 2018 02:01
Binary Number with Alternating Bits
func hasAlternatingBits(n int) bool {
count := 0
bit1 := 3
bit2 := 2
for n > 0 {
if count== 0 {
bit1 = n &1
fmt.Println(bit1, bit2)
} else {
bit1, bit2 = (n &1) , bit1
class Solution:
def calPoints(self, ops):
"""
:type ops: List[str]
:rtype: int
"""
n = len(ops)
keyStack = [0 for i in range(n)]
for i in range(n):
func calPoints(ops []string) int {
keyStack := make([]int, 0, len(ops))
for i:= range ops{
switch(ops[i]) {
case "+" :
r1 := keyStack[len(keyStack)-1]
r2 := keyStack[len(keyStack)-2]
keyStack = append(keyStack, r1+r2)
case "D":
@luojiyin1987
luojiyin1987 / Shortest Distance to a Character.py
Created July 23, 2018 08:01
Shortest Distance to a Character
class Solution:
def shortestToChar(self, S, C):
"""
:type S: str
:type C: str
:rtype: List[int]
"""
n = len(S)
result = [n for i in range (n)]
func binaryGap(N int) int {
maxDis := 0
indexFirst := 0
indexSecond := 0
i := 0
count := 0
for N > 0 {
if(N & 1 ) == 1 {
count +=1
def binaryGap(self, N):
"""
:type N: int
:rtype: int
"""
result = 0
last = None
for i in range(32):
if (N >> i) & 1:
if last is not None: