为有牺牲多壮志,敢教日月换新天。

[Swift]LeetCode1009. 十进制整数的补码 | Complement of Base 10 Integer

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10546205.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

Every non-negative integer N has a binary representation.  For example, 5 can be represented as "101" in binary, 11 as "1011" in binary, and so on.  Note that except for N = 0, there are no leading zeroes in any binary representation.

The complement of a binary representation is the number in binary you get when changing every 1 to a 0and 0 to a 1.  For example, the complement of "101" in binary is "010" in binary.

For a given number N in base-10, return the complement of it's binary representation as a base-10 integer.

Example 1:

Input: 5
Output: 2
Explanation: 5 is "101" in binary, with complement "010" in binary, which is 2 in base-10.

Example 2:

Input: 7
Output: 0
Explanation: 7 is "111" in binary, with complement "000" in binary, which is 0 in base-10.

Example 3:

Input: 10
Output: 5
Explanation: 10 is "1010" in binary, with complement "0101" in binary, which is 5 in base-10.

Note:

  1. 0 <= N < 10^9

每个非负整数 N 都有其二进制表示。例如, 5 可以被表示为二进制 "101"11 可以用二进制 "1011" 表示,依此类推。注意,除 N = 0 外,任何二进制表示中都不含前导零。

二进制的补码表示是将每个 1 改为 0 且每个 0 变为 1。例如,二进制数 "101" 的二进制补码为 "010"

给定十进制数 N,返回其二进制表示的补码所对应的十进制整数。

示例 1:

输入:5
输出:2
解释:5 的二进制表示为 "101",其二进制补码为 "010",也就是十进制中的 2 。

示例 2:

输入:7
输出:0
解释:7 的二进制表示为 "111",其二进制补码为 "000",也就是十进制中的 0 。

示例 3:

输入:10
输出:5
解释:10 的二进制表示为 "1010",其二进制补码为 "0101",也就是十进制中的 5 。 

提示:

  1. 0 <= N < 10^9

Runtime: 4 ms
Memory Usage: 19 MB
 1 class Solution {
 2     func bitwiseComplement(_ N: Int) -> Int {
 3         var N = N
 4         var bits:[Int] = [Int]()
 5         repeat{
 6             bits.append(N % 2)
 7             N /= 2            
 8         }while(N != 0)
 9         for i in 0..<bits.count
10         {
11             bits[i] = 1 - bits[i]
12         }
13         var val:Int = 0
14         var p:Int = 1
15         for i in 0..<bits.count
16         {
17             val += p * bits[i]
18             p *= 2
19         } 
20         return val        
21     }
22 }

4ms
1 class Solution {
2     func bitwiseComplement(_ N: Int) -> Int {
3         var c = 1
4         while c < N {
5             c = (c << 1) | 1
6         }
7         return N ^ c        
8     }
9 }

Runtime: 8 ms

Memory Usage: 18.9 MB
 1 class Solution {
 2     func bitwiseComplement(_ N: Int) -> Int {
 3         var arrInt:[Int] = String(N,radix:2).map{Int(String($0))!}
 4         for i in 0..<arrInt.count
 5         {
 6             arrInt[i] = 1 - arrInt[i]         
 7         }
 8         return String(arrInt.map{Character(String($0))}).BinaryToDecimal()        
 9     }
10 }
11 extension String{
12    func BinaryToDecimal() -> Int {    
13        var sum:Int = 0
14        for c in self.characters {
15            if let number = Int(String(c))
16            {
17                sum = sum * 2 + number
18            }        
19        }
20        return sum   
21    }
22 }

 

posted @ 2019-03-17 12:15  为敢技术  阅读(614)  评论(0编辑  收藏  举报