質問するログイン新規登録

回答編集履歴

5

修正

2016/08/17 23:43

投稿

_Kentarou
_Kentarou

スコア8490

answer CHANGED
@@ -1,4 +1,5 @@
1
- 以下の様にすると同じ文字という事で結果が取れます。
1
+ 以下の様にすると一緒の文字数の結果が取れます。
2
+ ※ 数字に`0`が続いても大丈夫。
2
3
 
3
4
  ```swift
4
5
  let strValue = "012"

4

s修正

2016/08/17 23:43

投稿

_Kentarou
_Kentarou

スコア8490

answer CHANGED
@@ -1,5 +1,4 @@
1
- ほぼで力技ですが、以下の様にすると同じ文字という事で結果が取れます。
1
+ 以下の様にすると同じ文字という事で結果が取れます。
2
- もっとスマートに掛けるはず、、、
3
2
 
4
3
  ```swift
5
4
  let strValue = "012"
@@ -7,19 +6,14 @@
7
6
 
8
7
  var matchCount = 0
9
8
 
10
- if let str: String = String(intValue) {
9
+ let str = NSString(format: "%03d", intValue) as String
11
- var strArray = Array(str.characters)
10
+ var strArray = Array(str.characters)
11
+
12
-
12
+ Array(strValue.characters).enumerate().forEach({ (index, value) in
13
- if strArray.count == 2 {
13
+ if value == strArray[index] {
14
- strArray.insert("0", atIndex: 0)
14
+ matchCount += 1
15
15
  }
16
-
17
- Array(strValue.characters).enumerate().forEach({ (index, value) in
18
- if value == strArray[index] {
19
- matchCount += 1
20
- }
21
- })
16
+ })
22
- }
23
17
 
24
18
  matchCount
25
19
  //=> 3

3

s修正

2016/08/17 23:39

投稿

_Kentarou
_Kentarou

スコア8490

answer CHANGED
@@ -1,5 +1,5 @@
1
1
  ほぼで力技ですが、以下の様にすると同じ文字という事で結果が取れます。
2
- もっとスマートに掛けるはず、、、
2
+ もっとスマートに掛けるはず、、、
3
3
 
4
4
  ```swift
5
5
  let strValue = "012"

2

s修正

2016/08/17 23:22

投稿

_Kentarou
_Kentarou

スコア8490

answer CHANGED
@@ -1,5 +1,5 @@
1
- 全力で力技ですが、以下の様にすると同じ文字という事で結果が取れます。
1
+ ほぼで力技ですが、以下の様にすると同じ文字という事で結果が取れます。
2
- `0`が最初の場合を考慮すると一旦文字列にするしかなかったのでこの様にしましたが、もっとスマートに掛けるはず、、、
2
+ ※ もっとスマートに掛けるはず、、、
3
3
 
4
4
  ```swift
5
5
  let strValue = "012"
@@ -7,20 +7,15 @@
7
7
 
8
8
  var matchCount = 0
9
9
 
10
- if let intVal = Int(strValue) , let str1: String = String(intVal), let str2: String = String(intValue) {
10
+ if let str: String = String(intValue) {
11
- var strArray1 = Array(str1.characters)
11
+ var strArray = Array(str.characters)
12
- var strArray2 = Array(str2.characters)
13
12
 
14
- if strArray1.count == 2 {
13
+ if strArray.count == 2 {
15
- strArray1.insert("0", atIndex: 0)
14
+ strArray.insert("0", atIndex: 0)
16
15
  }
17
16
 
18
- if strArray2.count == 2 {
19
- strArray2.insert("0", atIndex: 0)
20
- }
21
-
22
- strArray1.enumerate().forEach({ (index, value) in
17
+ Array(strValue.characters).enumerate().forEach({ (index, value) in
23
- if value == strArray2[index] {
18
+ if value == strArray[index] {
24
19
  matchCount += 1
25
20
  }
26
21
  })

1

s修正

2016/08/17 23:22

投稿

_Kentarou
_Kentarou

スコア8490

answer CHANGED
@@ -1,17 +1,24 @@
1
- ほぼ力技ですが、以下の様にすると同じ文字という事で結果が取れます。
1
+ で力技ですが、以下の様にすると同じ文字という事で結果が取れます。
2
2
  ※ `0`が最初の場合を考慮すると一旦文字列にするしかなかったのでこの様にしましたが、もっとスマートに掛けるはず、、、
3
3
 
4
4
  ```swift
5
-
6
5
  let strValue = "012"
7
6
  let intValue = 012
8
7
 
9
8
  var matchCount = 0
10
9
 
11
10
  if let intVal = Int(strValue) , let str1: String = String(intVal), let str2: String = String(intValue) {
12
- let strArray1 = Array(str1.characters)
11
+ var strArray1 = Array(str1.characters)
13
- let strArray2 = Array(str2.characters)
12
+ var strArray2 = Array(str2.characters)
14
13
 
14
+ if strArray1.count == 2 {
15
+ strArray1.insert("0", atIndex: 0)
16
+ }
17
+
18
+ if strArray2.count == 2 {
19
+ strArray2.insert("0", atIndex: 0)
20
+ }
21
+
15
22
  strArray1.enumerate().forEach({ (index, value) in
16
23
  if value == strArray2[index] {
17
24
  matchCount += 1
@@ -20,5 +27,5 @@
20
27
  }
21
28
 
22
29
  matchCount
23
- //=> 2
30
+ //=> 3
24
31
  ```