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 a26042a

Browse files
Init repo
0 parents commit a26042a

25 files changed

+574
-0
lines changed

‎.gitignore

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
.dart_tool/
2+
.packages
3+
build/
4+
pubspec.lock
5+
doc/api/
6+
.env*
7+
*.dart.js
8+
*.info.json # Produced by the --dump-info flag.
9+
*.js # When generated by dart2js. Don't specify *.js if your
10+
*.js_
11+
*.js.deps
12+
*.js.map
13+
.flutter-plugins
14+
.flutter-plugins-dependencies
15+
.idea/*
16+
*.iml

‎CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## 1.0.0
2+
3+
- Initial version.

‎README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# dart-leetcode
2+
Just solutions to Leetcode problems in Dart. Problems are organized in packages by topic with the goal to have the sample solutions as unit tests.

‎analysis_options.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# This file configures the static analysis results for your project (errors,
2+
# warnings, and lints).
3+
#
4+
# This enables the 'recommended' set of lints from `package:lints`.
5+
# This set helps identify many issues that may lead to problems when running
6+
# or consuming Dart code, and enforces writing Dart using a single, idiomatic
7+
# style and format.
8+
#
9+
# If you want a smaller set of lints you can change this to specify
10+
# 'package:lints/core.yaml'. These are just the most critical lints
11+
# (the recommended set includes the core lints).
12+
# The core lints are also what is used by pub.dev for scoring packages.
13+
14+
include: package:lints/recommended.yaml
15+
16+
# Uncomment the following section to specify additional rules.
17+
18+
# linter:
19+
# rules:
20+
# - camel_case_types
21+
22+
# analyzer:
23+
# exclude:
24+
# - path/to/excluded/files/**
25+
26+
# For more information about the core and recommended set of lints, see
27+
# https://dart.dev/go/core-lints
28+
29+
# For additional information about configuring this file, see
30+
# https://dart.dev/guides/language/analysis-options

‎lib/graph/num_islands.dart

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import 'dart:collection';
2+
3+
int numIslands(List<List<String>> grid) {
4+
var count = 0;
5+
var rows = grid.length;
6+
var cols = grid[0].length;
7+
var q = Queue<List<int>>();
8+
9+
for (var row = 0; row < rows; row++) {
10+
for (var col = 0; col < cols; col++) {
11+
if (grid[row][col] == "1") {
12+
q.add([row, col]);
13+
while (q.isNotEmpty) {
14+
var top = q.removeFirst();
15+
var r = top[0];
16+
var c = top[1];
17+
18+
if (r >= 0 && r < rows && c >= 0 && c < cols && grid[r][c] == "1") {
19+
grid[r][c] = "0";
20+
q.add([r + 1, c]);
21+
q.add([r - 1, c]);
22+
q.add([r, c + 1]);
23+
q.add([r, c - 1]);
24+
}
25+
}
26+
count += 1;
27+
}
28+
}
29+
}
30+
31+
return count;
32+
}

‎lib/hash/lru_cache.dart

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import 'dart:collection';
2+
3+
class LRUCache {
4+
final LinkedHashMap<int, int> _cache = LinkedHashMap();
5+
late final int _capacity;
6+
7+
LRUCache(int capacity) {
8+
_capacity = capacity;
9+
}
10+
11+
int get(int key) {
12+
if (_cache.containsKey(key)) {
13+
var removed = _cache.remove(key)!;
14+
_cache[key] = removed;
15+
return _cache[key]!;
16+
}
17+
18+
return -1;
19+
}
20+
21+
void put(int key, int value) {
22+
if (_cache.containsKey(key)) {
23+
_cache.remove(key);
24+
_cache[key] = value;
25+
return;
26+
}
27+
28+
if (_capacity == _cache.length) {
29+
_cache.remove(_cache.entries.first.key);
30+
}
31+
32+
_cache[key] = value;
33+
}
34+
}

‎lib/leetcode.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// Support for doing something awesome.
2+
///
3+
/// More dartdocs go here.
4+
library leetcode;
5+
6+
export 'graph/num_islands.dart';
7+
export 'hash/lru_cache.dart';
8+
export 'list/list_node.dart';
9+
export 'list/reverse_list.dart';
10+
export 'stack/is_valid.dart';
11+
export 'tree/invert_tree.dart';
12+
export 'tree/max_depth.dart';
13+
export 'tree/tree_node.dart';
14+
export 'trie/trie.dart';

‎lib/list/list_node.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class ListNode {
2+
int val;
3+
ListNode? next;
4+
ListNode([this.val = 0, this.next]);
5+
6+
static ListNode? fromList(List<int> list) {
7+
if (list.isEmpty) {
8+
return null;
9+
}
10+
11+
var head = ListNode();
12+
list.fold(head, (previousValue, element) {
13+
return previousValue.next = ListNode(element);
14+
});
15+
16+
return head.next;
17+
}
18+
19+
@override
20+
bool operator ==(Object other) =>
21+
identical(this, other) ||
22+
other is ListNode &&
23+
runtimeType == other.runtimeType &&
24+
val == other.val &&
25+
next == other.next;
26+
27+
@override
28+
int get hashCode => val.hashCode ^ next.hashCode;
29+
}

‎lib/list/reverse_list.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import 'list_node.dart';
2+
3+
ListNode? reverseList(ListNode? head) {
4+
ListNode? prev;
5+
var curr = head;
6+
while (curr != null) {
7+
var next = curr.next;
8+
curr.next = prev;
9+
prev = curr;
10+
curr = next;
11+
}
12+
13+
return prev;
14+
}

‎lib/matrix/rotate.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
void rotate(List<List<int>> matrix) {
2+
var n = matrix.length;
3+
4+
for (var i = 0; i < n; i++) {
5+
for (var j = i + 1; j < n; j++) {
6+
var t = matrix[i][j];
7+
matrix[i][j] = matrix[j][i];
8+
matrix[j][i] = t;
9+
}
10+
}
11+
12+
for (var i = 0; i < n; i++) {
13+
for (var j = 0; j < n / 2; j++) {
14+
var t = matrix[i][j];
15+
matrix[i][j] = matrix[i][n - j - 1];
16+
matrix[i][n - j - 1] = t;
17+
}
18+
}
19+
}

0 commit comments

Comments
(0)

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