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
| 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] |
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
| class Solution: | |
| def findMaxConsecutiveOnes(self, nums): | |
| """ | |
| :type nums: List[int] | |
| :rtype: int | |
| """ | |
| maxNum = 0 | |
| count = 0 | |
| for num in 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 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 | |
| } |
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 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] |
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 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 |
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
| 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): |
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 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": |
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
| 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)] |
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 binaryGap(N int) int { | |
| maxDis := 0 | |
| indexFirst := 0 | |
| indexSecond := 0 | |
| i := 0 | |
| count := 0 | |
| for N > 0 { | |
| if(N & 1 ) == 1 { | |
| count +=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
| 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: |