Personal Leetcode Solution (Python)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.