Skip to content

Commit d314414

Browse files
authored
Merge pull request #1904 from NikSWE/dart-sln
Add new dart solutions
2 parents f62ef13 + bc10660 commit d314414

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

Diff for: dart/0011-container-with-most-water.dart

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Time Complexity: O(n)
2+
// Space Complexity: O(1)
3+
4+
import 'dart:math';
5+
6+
class Solution {
7+
int maxArea(List<int> height) {
8+
var maxWater = 0, l = 0, r = height.length - 1;
9+
while (l < r) {
10+
var dist = r - l;
11+
var minHeight = min(height[l], height[r]);
12+
13+
if ((minHeight * dist) > maxWater) {
14+
maxWater = minHeight * dist;
15+
}
16+
17+
if (height[l] < height[r])
18+
l++;
19+
else
20+
r--;
21+
}
22+
return maxWater;
23+
}
24+
}

Diff for: dart/0125-valid-palindrome.dart

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Time Complexity: O(n)
2+
// Space Complexity: O(1)
3+
4+
class Solution {
5+
static bool isAlphaNum(String s) {
6+
if ((s.codeUnits[0] >= 'a'.codeUnitAt(0) &&
7+
s.codeUnits[0] <= 'z'.codeUnitAt(0)) ||
8+
(s.codeUnits[0] >= 'A'.codeUnitAt(0) &&
9+
s.codeUnits[0] <= 'Z'.codeUnitAt(0)) ||
10+
(s.codeUnits[0] >= '0'.codeUnitAt(0) &&
11+
s.codeUnits[0] <= '9'.codeUnitAt(0))) {
12+
return true;
13+
}
14+
return false;
15+
}
16+
17+
bool isPalindrome(String s) {
18+
var left = 0, right = s.length - 1;
19+
20+
while (left < right) {
21+
while (!Solution.isAlphaNum(s[left]) && left < right) left++;
22+
while (!Solution.isAlphaNum(s[right]) && left < right) right--;
23+
24+
if (s[left].toLowerCase() != s[right].toLowerCase()) return false;
25+
26+
left++;
27+
right--;
28+
}
29+
30+
return true;
31+
}
32+
}

0 commit comments

Comments
 (0)