美文网首页
LeetCode专题-位运算

LeetCode专题-位运算

作者: 山中散人的博客 | 来源:发表于2019-05-26 16:02 被阅读0次

    389. Find the Difference

    Easy

    Given two strings s and t which consist of only lowercase letters.

    String t is generated by random shuffling string s and then add one more letter at a random position.

    Find the letter that was added in t.

    Example:

    Input:
    s = "abcd"
    t = "abcde"

    Output:
    e

    Explanation:
    'e' is the letter that was added.

    题目大意:给出两个字符串,其中一个字符串比另一个多一个字符,两个字符串中其余字符都相同,要找出多出的一个字符。

    解题思路:异或可以将两个相同的字符变为0,将所有的字符异或,剩下的就是多出的字符串。

    class Solution(object):
        def findTheDifference(self, s, t):
            """
            :type s: str
            :type t: str
            :rtype: str
            """
            if len(s) == 0 or len(t) == 0:
                if len(s) >= len(t):
                    return s
                else:
                    return t
    
            #since all the identical char would occur twice
            #  we can remove all of them via XOR
            ans = ord(s[0]) #convert to unicode code point
            for c in s[1:]:
                ans ^= ord(c)
    
            for c in t:
                ans ^= ord(c)
    
            #code point to unicode string
            return chr(ans) #fint the index of target char        
    

    测试一下,

    Success
    [Details]
    Runtime: 24 ms, faster than 79.90% of Python online submissions for Find the Difference.
    Memory Usage: 11.8 MB, less than 38.50% of Python online submissions for Find the Difference.
    

    397. Integer Replacement

    Medium

    Given a positive integer n and you can do operations as follow:

    If n is even, replace n with n/2.
    If n is odd, you can replace n with either n + 1 or n - 1.
    

    What is the minimum number of replacements needed for n to become 1?

    Example 1:

    Input:
    8

    Output:
    3

    Explanation:
    8 -> 4 -> 2 -> 1

    题目大意:n/2, n-1 和 n+1都是一次操作。给一个整数,要求求出最少的操作次数,将整数变为1。

    解题思路:偶数变小利用n/2,奇数变小用n+1,一个特例是整数变到3时,利用n-1。

    class Solution:
        def integerReplacement(self, n: int) -> int:
            cnt = 0
            while n != 1:
                cnt += 1
                if n%2 == 0: # if num if even, half
                    n //= 2
                elif n == 3: # if num equal 3, do not add by one but subtract by one
                    n -= 1
                elif n & 0x02 != 0:
                    n += 1
                else:
                    n -= 1
            return cnt        
    

    测试一下,

    Success
    [Details]
    Runtime: 36 ms, faster than 92.63% of Python3 online submissions for Integer Replacement.
    Memory Usage: 13 MB, less than 85.82% of Python3 online submissions for Integer Replacement.
    

    400. Nth Digit

    Easy

    Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...

    Note:
    n is positive and will fit within the range of a 32-bit signed integer (n < 231).

    Example 1:

    Input:
    3

    Output:
    3

    题目大意:找到规律,1-9直接有9个数,10-99有90*2个数。

    解题思路:尽可能多的剔除数,再利用数位数推测出目标。

    """ 1 - 9  : 9
        10 - 99 : 90 * 2
        100 - 999 : 900 * 3
        1000 - 9999 : 9000 * 4
    ... ...  """
    class Solution:
        def findNthDigit(self, n: int) -> int:
            start, bits, cnt = 1, 1, 9
    
            # advance by bits
            while n > bits*cnt:
                n -= bits*cnt
                bits += 1
                cnt = cnt*10  #multiply cnt base
                start = start*10 #advance cnt of digits
    
            start += (n-1)/bits # advance by nums
            return int(str(start)[(n-1)%bits]) # take the rest of digits into account        
    

    测试一下

    Success
    [Details]
    Runtime: 28 ms, faster than 99.69% of Python3 online submissions for Nth Digit.
    Memory Usage: 13.1 MB, less than 76.37% of Python3 online submissions for Nth Digit.
    

    相关文章

      网友评论

          本文标题:LeetCode专题-位运算

          本文链接:https://www.haomeiwen.com/subject/ieqftctx.html