8000 Number Complement. · lexrus/LeetCode.swift@2e596a2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2e596a2

Browse files
committed
Number Complement.
1 parent 925c6f8 commit 2e596a2

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

LeetCode.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
D73A13E51B9C85C8001579E8 /* 263.swift in Sources */ = {isa = PBXBuildFile; fileRef = D73A13E41B9C85C8001579E8 /* 263.swift */; };
2222
D73A42381E668CDE00F34AD9 /* 520.swift in Sources */ = {isa = PBXBuildFile; fileRef = D73A42371E668CDE00F34AD9 /* 520.swift */; };
2323
D73A423A1E66929F00F34AD9 /* 504.swift in Sources */ = {isa = PBXBuildFile; fileRef = D73A42391E66929F00F34AD9 /* 504.swift */; };
24+
D73A423C1E67E50D00F34AD9 /* 476.swift in Sources */ = {isa = PBXBuildFile; fileRef = D73A423B1E67E50D00F34AD9 /* 476.swift */; };
2425
D743F3DC1C2EE7BF00A7BC6E /* 111.swift in Sources */ = {isa = PBXBuildFile; fileRef = D743F3DB1C2EE7BF00A7BC6E /* 111.swift */; };
2526
D7803DC21B95F1FE000C9270 /* 104.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7803DC11B95F1FE000C9270 /* 104.swift */; };
2627
D783CC9B1B2AC9160087FFC1 /* 226.swift in Sources */ = {isa = PBXBuildFile; fileRef = D783CC9A1B2AC9160087FFC1 /* 226.swift */; };
@@ -71,6 +72,7 @@
7172
D73A13E41B9C85C8001579E8 /* 263.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = 263.swift; sourceTree = "<group>"; };
7273
D73A42371E668CDE00F34AD9 /* 520.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = 520.swift; sourceTree = "<group>"; };
7374
D73A42391E66929F00F34AD9 /* 504.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = 504.swift; sourceTree = "<group>"; };
75+
D73A423B1E67E50D00F34AD9 /* 476.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = 476.swift; sourceTree = "<group>"; };
7476
D743F3DB1C2EE7BF00A7BC6E /* 111.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = 111.swift; sourceTree = "<group>"; };
7577
D7803DC11B95F1FE000C9270 /* 104.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = 104.swift; sourceTree = "<group>"; };
7678
D783CC9A1B2AC9160087FFC1 /* 226.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = 226.swift; sourceTree = "<group>"; };
@@ -182,6 +184,7 @@
182184
D71C171C1C30E99000936BF7 /* 283.swift */,
183185
D79437E71C4895AC005BA62B /* 326.swift */,
184186
D7B95A0E1E5B227A0059C57D /* 461.swift */,
187+
D73A423B1E67E50D00F34AD9 /* 476.swift */,
185188
D73A42391E66929F00F34AD9 /* 504.swift */,
186189
D73A42371E668CDE00F34AD9 /* 520.swift */,
187190
D7FB8E0B1AF899FE00286272 /* Supporting Files */,
@@ -298,6 +301,7 @@
298301
D73A423A1E66929F00F34AD9 /* 504.swift in Sources */,
299302
D7CB678A1C3E647100C4209D /* 114.swift in Sources */,
300303
D7FB8E2E1AF89B0400286272 /* 6.swift in Sources */,
304+
D73A423C1E67E50D00F34AD9 /* 476.swift in Sources */,
301305
D7DE016B1AF9F13600BB2AA0 /* 100.swift in Sources */,
302306
D71C171D1C30E99000936BF7 /* 283.swift in Sources */,
303307
D7FB8E1E1AF89A8200286272 /* 8.swift in Sources */,

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![Travis](https://img.shields.io/travis/lexrus/LeetCode.swift.svg)](https://travis-ci.org/lexrus/LeetCode.swift)
44
![Language](https://img.shields.io/badge/language-Swift%203-orange.svg)
55
![License](https://img.shields.io/github/license/lexrus/ios-dev-playbook.svg?style=flat)
6-
![Progress](https://img.shields.io/badge/Progress-43%20%2F%20490%20=%208.78%25-ff69b4.svg)
6+
![Progress](https://img.shields.io/badge/Progress-44%20%2F%20490%20=%208.98%25-ff69b4.svg)
77

88
> 愚者千虑,必有一得。
99
@@ -465,7 +465,7 @@ let 😎 = "Easy", 😐 = "Medium", 😨 = "Hard", ㊙️ = "require premium mem
465465
- [ ] 473 😐 [Matchsticks to Square](https://leetcode.com/problems/matchsticks-to-square)
466466
- [ ] 474 😐 [Ones and Zeroes](https://leetcode.com/problems/ones-and-zeroes)
467467
- [ ] 475 😎 [Heaters](https://leetcode.com/problems/heaters)
468-
- [ ] 476 😎 [Number Complement](https://leetcode.com/problems/number-complement)
468+
- [x] 476 😎 [Number Complement](https://leetcode.com/problems/number-complement) [.swift](./Tests/476.swift)
469469
- [ ] 477 😐 [Total Hamming Distance](https://leetcode.com/problems/total-hamming-distance)
470470
- [ ] 480 😨 [Sliding Window Median](https://leetcode.com/problems/sliding-window-median)
471471
- [ ] 481 😐 [Magical String](https://leetcode.com/problems/magical-string)

Tests/476.swift

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//
2+
// 476.swift
3+
// LeetCode
4+
//
5+
// Created by Lex on 3/2/17.
6+
// Copyright © 2017 Lex Tang. All rights reserved.
7+
//
8+
9+
import Foundation
10+
import XCTest
11+
12+
/*
13+
Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.
14+
15+
Note:
16+
The given integer is guaranteed to fit within the range of a 32-bit signed integer.
17+
You could assume no leading zero bit in the integer’s binary representation.
18+
19+
Example 1:
20+
Input: 5
21+
Output: 2
22+
Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.
23+
24+
Example 2:
25+
Input: 1
26+
Output: 0
27+
Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.
28+
*/
29+
30+
func findComplement(_ num: Int) -> Int {
31+
var mask: UInt32 = ~0
32+
while num & Int(mask) > 0 {
33+
mask <<= 1
34+
}
35+
return Int(~mask) & ~num
36+
}
37+
38+
class TestNumberComplement : XCTestCase {
39+
40+
func testNumberComplement() {
41+
XCTAssertEqual(findComplement(5), 2)
42+
XCTAssertEqual(findComplement(1), 0)
43+
}
44+
45+
}

0 commit comments

Comments
 (0)
0