Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e3bb883

Browse files
Update solutions
1 parent 2828030 commit e3bb883

13 files changed

+178
-41
lines changed

‎lib/graph/num_islands.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ int numIslands(List<List<String>> grid) {
88

99
for (var row = 0; row < rows; row++) {
1010
for (var col = 0; col < cols; col++) {
11-
if (grid[row][col] == "1") {
11+
if (grid[row][col] == '1') {
1212
q.add([row, col]);
1313
while (q.isNotEmpty) {
1414
var top = q.removeFirst();
1515
var r = top[0];
1616
var c = top[1];
1717

18-
if (r >= 0 && r < rows && c >= 0 && c < cols && grid[r][c] == "1") {
19-
grid[r][c] = "0";
18+
if (r >= 0 && r < rows && c >= 0 && c < cols && grid[r][c] == '1') {
19+
grid[r][c] = '0';
2020
q.add([r + 1, c]);
2121
q.add([r - 1, c]);
2222
q.add([r, c + 1]);

‎lib/leetcode.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ export 'list/reverse_list.dart';
1111
export 'matrix/is_valid_sudoku.dart';
1212
export 'matrix/rotate.dart';
1313
export 'stack/is_valid.dart';
14+
export 'string/letter_combinations.dart';
1415
export 'tree/invert_tree.dart';
1516
export 'tree/is_same_tree.dart';
1617
export 'tree/is_symmetric.dart';
18+
export 'tree/level_order.dart';
1719
export 'tree/max_depth.dart';
1820
export 'tree/tree_node.dart';
1921
export 'trie/trie.dart';

‎lib/matrix/is_valid_sudoku.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import 'dart:collection';
22

33
bool isValidSudoku(List<List<String>> board) {
44
bool addIfValid(String input, HashSet<String> set) {
5-
if (input != ".") {
5+
if (input != '.') {
66
if (set.contains(input)) {
77
return false;
88
}

‎lib/string/letter_combinations.dart

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import 'dart:collection';
2+
3+
List<String> letterCombinations(String digits) {
4+
const chars = {
5+
'1': '',
6+
'2': 'abc',
7+
'3': 'def',
8+
'4': 'ghi',
9+
'5': 'jkl',
10+
'6': 'mno',
11+
'7': 'pqrs',
12+
'8': 'tuv',
13+
'9': 'wxyz',
14+
'0': '',
15+
};
16+
17+
var combinations = <String>[];
18+
if (digits.isEmpty) {
19+
return combinations;
20+
}
21+
22+
void letterCombinations(int index, String current) {
23+
if (index == digits.length) {
24+
combinations.add(current);
25+
return;
26+
}
27+
28+
var letters = chars[digits[index]]!;
29+
for (var i = 0; i < letters.length; i++) {
30+
var c = letters[i];
31+
letterCombinations(index + 1, current + c);
32+
}
33+
}
34+
35+
letterCombinations(0, '');
36+
37+
return combinations;
38+
}

‎lib/tree/level_order.dart

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import 'dart:collection';
2+
3+
import 'tree_node.dart';
4+
5+
List<List<int>> levelOrder(TreeNode? root) {
6+
if (root == null) {
7+
return [];
8+
}
9+
10+
var out = <List<int>>[];
11+
var q = Queue<TreeNode>()..add(root);
12+
13+
while (q.isNotEmpty) {
14+
var level = q.length;
15+
var curr = <int>[];
16+
17+
for (var i = 0; i < level; i++) {
18+
var top = q.removeFirst();
19+
if (top.left != null) {
20+
q.add(top.left!);
21+
}
22+
if (top.right != null) {
23+
q.add(top.right!);
24+
}
25+
26+
curr.add(top.val);
27+
}
28+
29+
out.add(curr);
30+
}
31+
32+
return out;
33+
}

‎test/array/contains_duplicate_test.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import 'package:test/test.dart';
33

44
void main() {
55
test('example 1', () {
6-
expect(containsDuplicate([1,2,3,1]), equals(true));
6+
expect(containsDuplicate([1,2, 3, 1]), equals(true));
77
});
88

99
test('example 2', () {
10-
expect(containsDuplicate([1,2,3,4]), equals(false));
10+
expect(containsDuplicate([1,2, 3, 4]), equals(false));
1111
});
1212

1313
test('example 3', () {
14-
expect(containsDuplicate([1,1,1,3,3,4,3,2,4,2]), equals(true));
14+
expect(containsDuplicate([1,1, 1, 3, 3, 4, 3, 2, 4, 2]), equals(true));
1515
});
1616
}

‎test/array/remove_element_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import 'package:test/test.dart';
33

44
void main() {
55
test('example 1', () {
6-
expect(removeElement([3,2,2,3], 3), equals(2));
6+
expect(removeElement([3,2, 2, 3], 3), equals(2));
77
});
88

99
test('example 2', () {
10-
expect(removeElement([0,1,2,2,3,0,4,2], 2), equals(5));
10+
expect(removeElement([0,1, 2, 2, 3, 0, 4, 2], 2), equals(5));
1111
});
1212
}

‎test/graph/num_islands_test.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import 'package:test/test.dart';
44
void main() {
55
test('example 1', () {
66
var input = [
7-
["1", "1", "1", "1", "0"],
8-
["1", "1", "0", "1", "0"],
9-
["1", "1", "0", "0", "0"],
10-
["0", "0", "0", "0", "0"]
7+
['1', '1', '1', '1', '0'],
8+
['1', '1', '0', '1', '0'],
9+
['1', '1', '0', '0', '0'],
10+
['0', '0', '0', '0', '0']
1111
];
1212
var expected = 1;
1313
var result = numIslands(input);
@@ -17,10 +17,10 @@ void main() {
1717

1818
test('example 2', () {
1919
var input = [
20-
["1", "1", "0", "0", "0"],
21-
["1", "1", "0", "0", "0"],
22-
["0", "0", "1", "0", "0"],
23-
["0", "0", "0", "1", "1"]
20+
['1', '1', '0', '0', '0'],
21+
['1', '1', '0', '0', '0'],
22+
['0', '0', '1', '0', '0'],
23+
['0', '0', '0', '1', '1']
2424
];
2525
var expected = 3;
2626
var result = numIslands(input);

‎test/matrix/is_valid_sudoku_test.dart

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,30 @@ import 'package:test/test.dart';
44
void main() {
55
test('example 1', () {
66
var board = [
7-
["5", "3", ".", ".", "7", ".", ".", ".", "."],
8-
["6", ".", ".", "1", "9", "5", ".", ".", "."],
9-
[".", "9", "8", ".", ".", ".", ".", "6", "."],
10-
["8", ".", ".", ".", "6", ".", ".", ".", "3"],
11-
["4", ".", ".", "8", ".", "3", ".", ".", "1"],
12-
["7", ".", ".", ".", "2", ".", ".", ".", "6"],
13-
[".", "6", ".", ".", ".", ".", "2", "8", "."],
14-
[".", ".", ".", "4", "1", "9", ".", ".", "5"],
15-
[".", ".", ".", ".", "8", ".", ".", "7", "9"],
7+
['5', '3', '.', '.', '7', '.', '.', '.', '.'],
8+
['6', '.', '.', '1', '9', '5', '.', '.', '.'],
9+
['.', '9', '8', '.', '.', '.', '.', '6', '.'],
10+
['8', '.', '.', '.', '6', '.', '.', '.', '3'],
11+
['4', '.', '.', '8', '.', '3', '.', '.', '1'],
12+
['7', '.', '.', '.', '2', '.', '.', '.', '6'],
13+
['.', '6', '.', '.', '.', '.', '2', '8', '.'],
14+
['.', '.', '.', '4', '1', '9', '.', '.', '5'],
15+
['.', '.', '.', '.', '8', '.', '.', '7', '9'],
1616
];
1717
expect(isValidSudoku(board), equals(true));
1818
});
1919

2020
test('example 2', () {
2121
var board = [
22-
["8", "3", ".", ".", "7", ".", ".", ".", "."],
23-
["6", ".", ".", "1", "9", "5", ".", ".", "."],
24-
[".", "9", "8", ".", ".", ".", ".", "6", "."],
25-
["8", ".", ".", ".", "6", ".", ".", ".", "3"],
26-
["4", ".", ".", "8", ".", "3", ".", ".", "1"],
27-
["7", ".", ".", ".", "2", ".", ".", ".", "6"],
28-
[".", "6", ".", ".", ".", ".", "2", "8", "."],
29-
[".", ".", ".", "4", "1", "9", ".", ".", "5"],
30-
[".", ".", ".", ".", "8", ".", ".", "7", "9"]
22+
['8', '3', '.', '.', '7', '.', '.', '.', '.'],
23+
['6', '.', '.', '1', '9', '5', '.', '.', '.'],
24+
['.', '9', '8', '.', '.', '.', '.', '6', '.'],
25+
['8', '.', '.', '.', '6', '.', '.', '.', '3'],
26+
['4', '.', '.', '8', '.', '3', '.', '.', '1'],
27+
['7', '.', '.', '.', '2', '.', '.', '.', '6'],
28+
['.', '6', '.', '.', '.', '.', '2', '8', '.'],
29+
['.', '.', '.', '4', '1', '9', '.', '.', '5'],
30+
['.', '.', '.', '.', '8', '.', '.', '7', '9']
3131
];
3232
expect(isValidSudoku(board), equals(false));
3333
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import 'package:leetcode/leetcode.dart';
2+
import 'package:test/test.dart';
3+
4+
void main() {
5+
test('example 1', () {
6+
var expected = ['ad', 'ae', 'af', 'bd', 'be', 'bf', 'cd', 'ce', 'cf'];
7+
expect(letterCombinations('23'), equals(expected));
8+
});
9+
10+
test('example 2', () {
11+
var expected = [];
12+
expect(letterCombinations(''), equals(expected));
13+
});
14+
15+
test('example 2', () {
16+
var expected = ['a', 'b', 'c'];
17+
expect(letterCombinations('2'), equals(expected));
18+
});
19+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /