LeetCode#476 Number Complement - in Swift

Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.

給一個正整數,算出他的二補數 (2’s complement)。 二補數就是將該數字的二進制碼全部翻轉過來。

Note:
The given integer is guaranteed to fit within the range of a 32-bit signed integer.
You could assume no leading zero bit in the integer’s binary representation.

Example 1:
Input: 5
Output: 2
Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.

由於 5 的二進制是 101 , 所以他的補數便是 010 , 整數是 2

Example 2:
Input: 1
Output: 0
Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.

由於 1 的二進制是 1 , 所以他的補數便是 0 , 整數是 0

第一次解題 : Accepted

將數字轉為二進制,尋遍二進制碼將數字轉換。

1

2

3

4

5

6

7

8

9

10

11
func (_ num: Int) -> Int {

let binary = String(num, radix: 2)

var complement: String = ""

for c in binary.characters {

complement += c == "0" ? "1" : 大专栏  LeetCode#476 Number Complement - in Swift"string">"0"

}

return Int(complement, radix: 2)!

}

Best Solution

遍尋一遍是很耗資源的做法。所以要使用二元運算的方法來解。

  1. 先取得與二進制 num 相同長度的 mask
    5(101) -> 111
    9(1001) -> 1111
  2. 5(101) 舉例,取得 Most Significant Bit 就是影響這個數串最大的數字,也就是最左邊的數 -> (1)01
    Java 有內建 Integer.highestOneBit(num) 的方法,但 Swift 只能自己手動建立,取得除了最大數為1其他數為0的值 -> (1)00
    接著對該數字利用下溢位 -1 ,取得 mask -> 111
  3. 取得 mask 後,解題方法有二:

    3-1: 使用 ^ 運算子,相同數 XOR 交換
    num(101) ^ mask(111) = 010

    3-2: 使用 ~ 跟 & 運算子,對 num 作 NOT 運算取補數,並且跟 mask 作 AND 運算
    ~num(101) & 111 = 010
    -> 11111111111111111111111111111010 & 0000...111 = 010

1

2

3

4

5

6

7

8
func (_ num: Int) -> Int {

var mask = 1

while(mask <= num) {

mask <<= 1

}

mask-=1

return num ^ mask

}

原文地址:https://www.cnblogs.com/lijianming180/p/12268008.html

时间: 2024-10-08 19:11:19

LeetCode#476 Number Complement - in Swift的相关文章

LeetCode 476. Number Complement(easy难度c++)

题目: Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation. Note: The given integer is guaranteed to fit within the range of a 32-bit signed integer. You could assume no leadin

LeetCode 476. Number Complement (数的补数)

Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation. Note: The given integer is guaranteed to fit within the range of a 32-bit signed integer. You could assume no leading ze

LeetCode解题思路:476. Number Complement

Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation. Note: The given integer is guaranteed to fit within the range of a 32-bit signed integer. You could assume no leading ze

476. Number Complement【位运算】

2017/3/14 15:36:44 Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation. Note: The given integer is guaranteed to fit within the range of a 32-bit signed integer. You could a

476. Number Complement

Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation. Note: The given integer is guaranteed to fit within the range of a 32-bit signed integer. You could assume no leading ze

[LeetCode] 476. Number Complement_Easy tag: Bit Manipulation

这个题目思路就是比如101 的结果是010, 可以从111^101 来得到, 那么我们就需要知道刚好比101多一位的1000, 所以利用 while i <= num : i <<= 1, 去得到1000, 然后-1, 便得到111, 再跟num ^, 也就是异或即可. Code class Solution(object): def findComplement(self, num): i = 1 while i <= num: i <<= 1 return (i-1

[leetcode]Valid Number @ Python

原题地址:http://oj.leetcode.com/problems/valid-number/ 题意:判断输入的字符串是否是合法的数. 解题思路:这题只能用确定有穷状态自动机(DFA)来写会比较优雅.本文参考了http://blog.csdn.net/kenden23/article/details/18696083里面的内容,在此致谢! 首先这个题有9种状态: 0初始无输入或者只有space的状态1输入了数字之后的状态2前面无数字,只输入了dot的状态3输入了符号状态4前面有数字和有do

leetCode: Single Number II [137]

[题目] Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? [题意] 给定一个整数以外,其中除了一个整数只出现一次以外,其他

[leetcode]_Palindrome Number

判断integer是否为回文串(负数全部不为回文串) 思路很直接,提取出integer中的每一位,一头一尾进行比较是否相同. 一次AC , 直接上代码: public boolean isPalindrome(int x) { if(x < 0) return false; else if(x >= 0 && x <= 9) return true; else{ int[] num = new int[20]; int i = 0 ; while(x > 0){ n