-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstrobogrammatic_number.dart
88 lines (77 loc) · 1.97 KB
/
strobogrammatic_number.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
-* Strobo-Grammatic Number *-
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).
Write a function to determine if a number is strobogrammatic. The number is represented as a string.
For example, the numbers "69", "88", and "818" are all strobogrammatic.
*/
import 'dart:collection';
class Solution {
bool isStrobogrammatic(String nums) {
HashMap<String, String> dict = HashMap();
dict['0'] = '0';
dict['1'] = '1';
dict['8'] = '8';
dict["6"] = '9';
dict['9'] = '6';
int i = 0;
int j = nums.length - 1;
while (i <= j) {
int f = nums.codeUnitAt(i);
int b = nums.codeUnitAt(j);
if (dict.containsKey(f) && dict.containsKey(b) && dict[f] == b) {
i++;
j--;
} else {
return false;
}
}
return true;
}
}
class B {
bool isStrobogrammatic(String nums) {
final Map<String, String> lut = {
'0': "0",
'1': '1',
'6': '9',
'8': '8',
'9': '6'
};
for (int l = 0, r = nums.length - 1; l <= r; l++, r--) {
if (lut.containsKey(nums[l]) ==
lut.entries.toList().getRange(lut.length - 2, lut.length) ||
lut[nums[l]] != nums[r]) {
return false;
}
}
return true;
}
}
class C {
bool isStrobogrammatic(String nums) {
for (int i = 0, j = nums.length - 1; i <= j; i++, j--)
if (!"00 11 88 696".contains(
nums[i] + "" + nums[i],
)) return false;
return true;
}
}
class D {
bool isStrobogrammatic(String nums) {
HashMap<String, String> map = HashMap();
map['0'] = '0';
map['1'] = '1';
map['8'] = '8';
map["6"] = '9';
map['9'] = '6';
if (nums.length == 0) return false;
int left = 0, right = nums.length - 1;
while (left <= right) {
int c = nums.codeUnitAt(left);
if (!map.containsKey(c) || map[c] != nums.codeUnitAt(right)) return false;
left++;
right--;
}
return true;
}
}