10000 Hamming Distance. · lexrus/LeetCode.swift@d20649d · GitHub
[go: up one dir, main page]

Skip to content

Commit d20649d

Browse files
committed
Hamming Distance.
1 parent 9d82dc3 commit d20649d

File tree

3 files changed

+61
-4
lines changed

3 files changed

+61
-4
lines changed

LeetCode.xcodeproj/project.pbxproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
D79437E81C4895AC005BA62B /* 326.swift in Sources */ = {isa = PBXBuildFile; fileRef = D79437E71C4895AC005BA62B /* 326.swift */; };
2626
D7AB14CB1B99C85200DEDE9D /* 58.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7AB14CA1B99C85200DEDE9D /* 58.swift */; };
2727
D7AB14CE1B9B34FA00DEDE9D /* 67.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7AB14CD1B9B34FA00DEDE9D /* 67.swift */; };
28+
D7B95A0F1E5B227A0059C57D /* 461.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7B95A0E1E5B227A0059C57D /* 461.swift */; };
2829
D7C413261E541977006C36A5 /* 136.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7C413251E541977006C36A5 /* 136.swift */; };
2930
D7C413281E556204006C36A5 /* 268.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7C413271E556204006C36A5 /* 268.swift */; };
3031
D7CB678A1C3E647100C4209D /* 114.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7CB67891C3E647100C4209D /* 114.swift */; };
@@ -73,6 +74,7 @@
7374
D7AB14CA1B99C85200DEDE9D /* 58.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = 58.swift; sourceTree = "<group>"; };
7475
D7AB14CC1B9B344900DEDE9D /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
7576
D7AB14CD1B9B34FA00DEDE9D /* 67.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = 67.swift; sourceTree = "<group>"; };
77+
D7B95A0E1E5B227A0059C57D /* 461.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = 461.swift; sourceTree = "<group>"; };
7678
D7C413251E541977006C36A5 /* 136.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = 136.swift; sourceTree = "<group>"; };
7779
D7C413271E556204006C36A5 /* 268.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = 268.swift; sourceTree = "<group>"; };
7880
D7CB67891C3E647100C4209D /* 114.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = 114.swift; sourceTree = "<group>"; };
@@ -172,10 +174,11 @@
172174
D70B52A01C291563007E16DC /* 242.swift */,
173175
D73A13E21B9C6FDB001579E8 /* 258.swift */,
174176
D73A13E41B9C85C8001579E8 /* 263.swift */,
177+
D7C413271E556204006C36A5 /* 268.swift */,
175178
D71C171C1C30E99000936BF7 /* 283.swift */,
176179
D79437E71C4895AC005BA62B /* 326.swift */,
180+
D7B95A0E1E5B227A0059C57D /* 461.swift */,
177181
D7FB8E0B1AF899FE00286272 /* Supporting Files */,
178-
D7C413271E556204006C36A5 /* 268.swift */,
179182
);
180183
path = Tests;
181184
sourceTree = SOURCE_ROOT;
@@ -304,6 +307,7 @@
304307
D70B52A31C291BF9007E16DC /* 49.swift in Sources */,
305308
D7FB8E281AF89ADB00286272 /* 7.swift in Sources */,
306309
D7FB8E1A1AF89A6500286272 /* 14.swift in Sources */,
310+
D7B95A0F1E5B227A0059C57D /* 461.swift in Sources */,
307311
D79437E81C4895AC005BA62B /* 326.swift in Sources */,
308312
D7FB8E6A1AF9B9D400286272 /* 82.swift in Sources */,
309313
D743F3DC1C2EE7BF00A7BC6E /* 111.swift in Sources */,

README.md

Lines changed: 3 additions & 3 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-39%20%2F%20482%20=%208.09%25-ff69b4.svg)
6+
![Progress](https://img.shields.io/badge/Progress-41%20%2F%20482%20=%208.51%25-ff69b4.svg)
77

88
> 愚者千虑,必有一得。
99
@@ -268,7 +268,7 @@ let 😎 = "Easy", 😐 = "Medium", 😨 = "Hard", ㊙️ = "require premium mem
268268
- [ ] 265 😨 [Paint House II](https://leetcode.comhttps://leetcode.com/problems/paint-house-ii) ㊙️
269269
- [ ] 266 😎 [Palindrome Permutation](https://leetcode.comhttps://leetcode.com/problems/palindrome-permutation) ㊙️
270270
- [ ] 267 😐 [Palindrome Permutation II](https://leetcode.comhttps://leetcode.com/problems/palindrome-permutation-ii) ㊙️
271-
- [ ] 268 😎 [Missing Number](https://leetcode.comhttps://leetcode.com/problems/missing-number)
271+
- [x] 268 😎 [Missing Number](https://leetcode.comhttps://leetcode.com/problems/missing-number) [.swift](./Tests/268.swift)
272272
- [ ] 269 😨 [Alien Dictionary](https://leetcode.comhttps://leetcode.com/problems/alien-dictionary) ㊙️
273273
- [ ] 270 😎 [Closest Binary Search Tree Value](https://leetcode.comhttps://leetcode.com/problems/closest-binary-search-tree-value) ㊙️
274274
- [ ] 271 😐 [Encode and Decode Strings](https://leetcode.comhttps://leetcode.com/problems/encode-and-decode-strings) ㊙️
@@ -451,7 +451,7 @@ let 😎 = "Easy", 😐 = "Medium", 😨 = "Hard", ㊙️ = "require premium mem
451451
- [ ] 456 😐 [132 Pattern](https://leetcode.comhttps://leetcode.com/problems/132-pattern)
452452
- [ ] 459 😎 [Repeated Substring Pattern](https://leetcode.comhttps://leetcode.com/problems/repeated-substring-pattern)
453453
- [ ] 460 😨 [LFU Cache](https://leetcode.comhttps://leetcode.com/problems/lfu-cache)
454-
- [ ] 461 😎 [Hamming Distance](https://leetcode.comhttps://leetcode.com/problems/hamming-distance)
454+
- [x] 461 😎 [Hamming Distance](https://leetcode.comhttps://leetcode.com/problems/hamming-distance) [.swift](./Tests/461.swift)
455455
- [ ] 462 😐 [Minimum Moves to Equal Array Elements II](https://leetcode.comhttps://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii)
456456
- [ ] 463 😎 [Island Perimeter](https://leetcode.comhttps://leetcode.com/problems/island-perimeter)
457457
- [ ] 464 😐 [Can I Win](https://leetcode.comhttps://leetcode.com/problems/can-i-win)

Tests/461.swift

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//
2+
// 461.swift
3+
// LeetCode
4+
//
5+
// Created by Lex on 20/02/2017.
6+
// Copyright © 2017 Lex Tang. All rights reserved.
7+
//
8+
9+
import Foundation
10+
import XCTest
11+
12+
/*
13+
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
14+
15+
Given two integers x and y, calculate the Hamming distance.
16+
17+
Note:
18+
0 ≤ x, y < 231.
19+
20+
Example:
21+
22+
Input: x = 1, y = 4
23+
24+
Output: 2
25+
26+
Explanation:
27+
1 (0 0 0 1)
28+
4 (0 1 0 0)
29+
↑ ↑
30+
31+
The above arrows point to positions where the corresponding bits are different.
32+
*/
33+
34+
func hammingDistance(_ x: Int, _ y: Int) -> Int {
35+
var xor = x ^ y
36+
var count = 0
37+
38+
while xor != 0 {
39+
count += xor & 1
40+
xor >>= 1
41+
}
42+
return count
43+
}
44+
45+
class TestHammingDistance : XCTestCase {
46+
47+
func testHammingDistance() {
48+
XCTAssertEqual(hammingDistance(1, 4), 2)
49+
XCTAssertEqual(hammingDistance(0, 0), 0)
50+
XCTAssertEqual(hammingDistance(1, 0), 1)
51+
}
52+
53+
}

0 commit comments

Comments
 (0)
0