GitHub:Https://github.com/seventheli/LeetCode-PracticeSinglenumber
Core:a XOR B xor A xor C XOR B = C
class solution (Object): def Singlenumber (Self, nums): """ : Type Nums:list[int] : rtype:int " " "= 0 for in range (len (nums)): ^= Nums[each] return result
ReverseString
Core: Serializing String Value, then descending the list
class solution (Object): def reversestring (self, s): = [] for in Range (len (s)): lst.append (s[-(i + 1)]) "' . Join (LST) return LST
Nim GAME
Core:second player only win if total accoumt% 4 = 0
class solution (Object): def Canwinnim (self, N): = n 4 if val! = 0: return True Else: return False
Counting Bits
Core:
Each time the value is 2^n
The different between the first half and second half are only the first digit
Example with 7
0 to 7 is
000
----
001
----
010
11P
----
100
101
110
111
The Second half is always "1+second half
class solution (Object): def countbits (self, num): = [0] while Len (val) <= num: for in Val] return Val[:num + 1]
Sum of integers
Core:implementation of an array of full Adder
classsolution (object):defHalf_adder (self, A, b): s= a ^b cin= A &breturnS, CindefFull_adder (self, A, B, CIN): S1, C1=Self.half_adder (A, b) s2, C2=Self.half_adder (CIN, S1) c_out= C1 |C2returnS2, C_outdefgetsum (self, A, b): Mask= 1ci=0 Sum_total=0 whileMask <= 0x080000000: A1= A &Mask B1= B &Mask (S, ci)=Self.full_adder (A1, B1, Ci) sum_total= Sum_total |s CI<<= 1Mask<<= 1Print "S:"+ STR (s) +"CI:"+Str (CI) maximum= 0x7FFFFFFFMask= 0xFFFFFFFFifSum_total >Maximum:sum_total= ~ (sum_total ^mask)returnSum_total
Personal Leetcode Solution (Python)