Skip to content

2025-05-01 v. 1.0.2: refactored project #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions .github/workflows/dart.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,41 @@ on:
pull_request:
branches: [ master ]

env:
DART_VERSION: "3.7.2"

jobs:
build:
analyzer:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: dart-lang/setup-dart@v1.0
with:
sdk: ${{ env.DART_VERSION }}
- name: Install dependencies
run: dart pub get
- name: Run tests
run: dart analyze

formatter:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: dart-lang/setup-dart@v1.0
with:
sdk: ${{ env.DART_VERSION }}
- name: Install dependencies
run: dart pub get
- name: Run tests
run: dart format -l 120 --set-exit-if-changed ./

tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: dart-lang/setup-dart@v1.0
with:
sdk: 3.7.2
sdk: ${{ env.DART_VERSION }}
- name: Install dependencies
run: dart pub get
- name: Run tests
Expand Down
54 changes: 27 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,30 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).

### Easy

| Name | Link to LeetCode | Link to solution |
| ----------------------------------------------- | --------------------------------------------------------------------------------- | ------------------------------------------------------------------ |
| 1. Two Sum | [Link](https://leetcode.com/problems/two-sum/) | [Link](./lib/easy/two_sum.dart) |
| 9. Palindrome Number | [Link](https://leetcode.com/problems/palindrome-number/) | [Link](./lib/easy/palindrome_number.dart) |
| 13. Roman to Integer | [Link](https://leetcode.com/problems/roman-to-integer/) | [Link](./lib/easy/roman_to_integer.dart) |
| 14. Longest Common Prefix | [Link](https://leetcode.com/problems/longest-common-prefix/) | [Link](./lib/easy/longest_common_prefix.dart) |
| 20. Valid Parentheses | [Link](https://leetcode.com/problems/valid-parentheses/) | [Link](./lib/easy/valid_parentheses.dart) |
| 21. Merge Two Sorted Lists | [Link](https://leetcode.com/problems/merge-two-sorted-lists/) | [Link](./lib/easy/merge_two_sorted_lists.dart) |
| 27. Remove Element | [Link](https://leetcode.com/problems/remove-element/) | [Link](./lib/easy/remove_element.dart) |
| 35. Search Insert Position | [Link](https://leetcode.com/problems/search-insert-position/) | [Link](./lib/easy/search_insert_position.dart) |
| 58. Length of Last Word | [Link](https://leetcode.com/problems/length-of-last-word/) | [Link](./lib/easy/length_of_last_word.dart) |
| 66. Plus One | [Link](https://leetcode.com/problems/plus-one/) | [Link](./lib/easy/plus_one.dart) |
| 67. Add Binary | [Link](https://leetcode.com/problems/add-binary/) | [Link](./lib/easy/add_binary.dart) |
| 69. Sqrt(x) | [Link](https://leetcode.com/problems/sqrtx/) | [Link](./lib/easy/sqrt_x.dart) |
| 70. Climbing Stairs | [Link](https://leetcode.com/problems/climbing-stairs/) | [Link](./lib/easy/climbing_stairs.dart) |
| 83. Remove Duplicates from Sorted List | [Link](https://leetcode.com/problems/remove-duplicates-from-sorted-list/) | [Link](./lib/easy/remove_duplicates_from_sorted_list.dart) |
| 88. Merge Sorted Array | [Link](https://leetcode.com/problems/merge-sorted-array/) | [Link](./lib/easy/merge_sorted_array.dart) |
| 94. Binary Tree Inorder Traversal | [Link](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [Link](./lib/easy/binary_tree_inorder_traversal.dart) |
| 100. Same Tree | [Link](https://leetcode.com/problems/same-tree/) | [Link](./lib/easy/same_tree.dart) |
| 101. Symmetric Tree | [Link](https://leetcode.com/problems/symmetric-tree/) | [Link](./lib/easy/symmetric_tree.dart) |
| 104. Maximum Depth of Binary Tree | [Link](https://leetcode.com/problems/maximum-depth-of-binary-tree/) | [Link](./lib/easy/maximum_depth_of_binary_tree.dart) |
| 108. Convert Sorted Array to Binary Search Tree | [Link](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/) | [Link](./lib/easy/convert_sorted_array_to_binary_search_tree.dart) |
| 110. Balanced Binary Tree | [Link](https://leetcode.com/problems/balanced-binary-tree/) | [Link](./lib/easy/balanced_binary_tree.dart) |
| 111. Minimum Depth of Binary Tree | [Link](https://leetcode.com/problems/minimum-depth-of-binary-tree/) | [Link](./lib/easy/minimum_depth_of_binary_tree.dart) |
| 112. Path Sum | [Link](https://leetcode.com/problems/path-sum/) | [Link](./lib/easy/path_sum.dart) |
| 118. Pascal's Triangle | [Link](https://leetcode.com/problems/pascals-triangle/) | [Link](./lib/easy/pascals_triangle.dart) |
| 3516. Find Closest Person | [Link](https://leetcode.com/problems/find-closest-person/) | [Link](./lib/easy/find_closest_person.dart) |
| Name | Link to LeetCode | Link to solution |
| ----------------------------------------------- | --------------------------------------------------------------------------------- | ---------------------------------------------------------------------- |
| 1. Two Sum | [Link](https://leetcode.com/problems/two-sum/) | [Link](./lib/easy/1_two_sum.dart) |
| 9. Palindrome Number | [Link](https://leetcode.com/problems/palindrome-number/) | [Link](./lib/easy/9_palindrome_number.dart) |
| 13. Roman to Integer | [Link](https://leetcode.com/problems/roman-to-integer/) | [Link](./lib/easy/13_roman_to_integer.dart) |
| 14. Longest Common Prefix | [Link](https://leetcode.com/problems/longest-common-prefix/) | [Link](./lib/easy/14_longest_common_prefix.dart) |
| 20. Valid Parentheses | [Link](https://leetcode.com/problems/valid-parentheses/) | [Link](./lib/easy/20_valid_parentheses.dart) |
| 21. Merge Two Sorted Lists | [Link](https://leetcode.com/problems/merge-two-sorted-lists/) | [Link](./lib/easy/21_merge_two_sorted_lists.dart) |
| 27. Remove Element | [Link](https://leetcode.com/problems/remove-element/) | [Link](./lib/easy/27_remove_element.dart) |
| 35. Search Insert Position | [Link](https://leetcode.com/problems/search-insert-position/) | [Link](./lib/easy/35_search_insert_position.dart) |
| 58. Length of Last Word | [Link](https://leetcode.com/problems/length-of-last-word/) | [Link](./lib/easy/58_length_of_last_word.dart) |
| 66. Plus One | [Link](https://leetcode.com/problems/plus-one/) | [Link](./lib/easy/66_plus_one.dart) |
| 67. Add Binary | [Link](https://leetcode.com/problems/add-binary/) | [Link](./lib/easy/67_add_binary.dart) |
| 69. Sqrt(x) | [Link](https://leetcode.com/problems/sqrtx/) | [Link](./lib/easy/69_sqrt_x.dart) |
| 70. Climbing Stairs | [Link](https://leetcode.com/problems/climbing-stairs/) | [Link](./lib/easy/70_climbing_stairs.dart) |
| 83. Remove Duplicates from Sorted List | [Link](https://leetcode.com/problems/remove-duplicates-from-sorted-list/) | [Link](./lib/easy/83_remove_duplicates_from_sorted_list.dart) |
| 88. Merge Sorted Array | [Link](https://leetcode.com/problems/merge-sorted-array/) | [Link](./lib/easy/88_merge_sorted_array.dart) |
| 94. Binary Tree Inorder Traversal | [Link](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [Link](./lib/easy/94_binary_tree_inorder_traversal.dart) |
| 100. Same Tree | [Link](https://leetcode.com/problems/same-tree/) | [Link](./lib/easy/100_same_tree.dart) |
| 101. Symmetric Tree | [Link](https://leetcode.com/problems/symmetric-tree/) | [Link](./lib/easy/101_symmetric_tree.dart) |
| 104. Maximum Depth of Binary Tree | [Link](https://leetcode.com/problems/maximum-depth-of-binary-tree/) | [Link](./lib/easy/104_maximum_depth_of_binary_tree.dart) |
| 108. Convert Sorted Array to Binary Search Tree | [Link](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/) | [Link](./lib/easy/108_convert_sorted_array_to_binary_search_tree.dart) |
| 110. Balanced Binary Tree | [Link](https://leetcode.com/problems/balanced-binary-tree/) | [Link](./lib/easy/110_balanced_binary_tree.dart) |
| 111. Minimum Depth of Binary Tree | [Link](https://leetcode.com/problems/minimum-depth-of-binary-tree/) | [Link](./lib/easy/111_minimum_depth_of_binary_tree.dart) |
| 112. Path Sum | [Link](https://leetcode.com/problems/path-sum/) | [Link](./lib/easy/112_path_sum.dart) |
| 118. Pascal's Triangle | [Link](https://leetcode.com/problems/pascals-triangle/) | [Link](./lib/easy/118_pascals_triangle.dart) |
| 3516. Find Closest Person | [Link](https://leetcode.com/problems/find-closest-person/) | [Link](./lib/easy/3516_find_closest_person.dart) |
4 changes: 4 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
include: package:lints/core.yaml

linter:
rules:
file_names: false
6 changes: 1 addition & 5 deletions lib/common/tree_node.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,5 @@ class TreeNode {
TreeNode? left;
TreeNode? right;

TreeNode([
this.val = 0,
this.left,
this.right,
]);
TreeNode([this.val = 0, this.left, this.right]);
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,14 @@ class Solution {
if (root != null) {
var l = _maxHeight(root.left);
var r = _maxHeight(root.right);
return (l - r).abs() < 2 &&
isBalanced(root.left) &&
isBalanced(root.right);
return (l - r).abs() < 2 && isBalanced(root.left) && isBalanced(root.right);
}
return true;
}

int _maxHeight(TreeNode? node) {
if (node != null) {
return 1 +
max(
_maxHeight(node.left),
_maxHeight(node.right),
);
return 1 + max(_maxHeight(node.left), _maxHeight(node.right));
}
return 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ class Solution {
} else if (root.right == null) {
return minDepth(root.left) + 1;
}
return min(
minDepth(root.left) + 1,
minDepth(root.right) + 1,
);
return min(minDepth(root.left) + 1, minDepth(root.right) + 1);
}
return 0;
}
Expand Down
3 changes: 1 addition & 2 deletions lib/easy/path_sum.dart → lib/easy/112_path_sum.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ class Solution {
if (node.left == null && node.right == null) {
return node.val + count == targetSum;
}
return _sum(node.left, targetSum, node.val + count) ||
_sum(node.right, targetSum, node.val + count);
return _sum(node.left, targetSum, node.val + count) || _sum(node.right, targetSum, node.val + count);
}
return false;
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
// https://leetcode.com/problems/roman-to-integer/
class Solution {
int romanToInt(String s) {
final values = <String, int>{
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000,
};
final values = <String, int>{'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000};
int result = 0;
for (int i = s.length - 1; i >= 0; i--) {
int curr = values[s[i]]!;
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ class Solution {
var j = n - 1;
var l = m + n - 1;
while (l >= 0) {
var num =
j < 0 || (i >= 0 && nums1[i] > nums2[j]) ? nums1[i--] : nums2[j--];
var num = j < 0 || (i >= 0 && nums1[i] > nums2[j]) ? nums1[i--] : nums2[j--];
nums1[l--] = num;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,3 @@ class Solution {
return rev == x;
}
}

2 changes: 1 addition & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -370,4 +370,4 @@ packages:
source: hosted
version: "3.1.1"
sdks:
dart: ">=3.7.2 <4.0.0"
dart: ">=3.6.9 <4.0.0"
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name: leetcode_dart
description: Some solved problems from https://leetcode.com on Dart
version: 1.0.1
version: 1.0.2
homepage: https://github.com/fartem/leetcode-dart

environment:
sdk: '>=3.7.2 <4.0.0'
sdk: '>=3.6.9 <4.0.0'

dev_dependencies:
lints: ^5.1.1
Expand Down
30 changes: 16 additions & 14 deletions test/easy/same_tree_test.dart → test/easy/100_same_tree_test.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import 'package:leetcode_dart/common/tree_node.dart';
import 'package:leetcode_dart/easy/same_tree.dart';
import 'package:leetcode_dart/easy/100_same_tree.dart';
import 'package:test/test.dart';

void main() {
group(
'Example tests',
() {
final st = Solution();
final solution = Solution();

test(
'true',
() => expect(
true,
st.isSameTree(
solution.isSameTree(
TreeNode(
1,
TreeNode(2),
Expand All @@ -29,23 +30,24 @@ void main() {
'false',
() => expect(
false,
st.isSameTree(
TreeNode(
1,
TreeNode(2),
),
TreeNode(
1,
null,
TreeNode(2),
)),
solution.isSameTree(
TreeNode(
1,
TreeNode(2),
),
TreeNode(
1,
null,
TreeNode(2),
),
),
),
);
test(
'false',
() => expect(
false,
st.isSameTree(
solution.isSameTree(
TreeNode(
1,
TreeNode(2),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import 'package:leetcode_dart/common/tree_node.dart';
import 'package:leetcode_dart/easy/symmetric_tree.dart';
import 'package:leetcode_dart/easy/101_symmetric_tree.dart';
import 'package:test/test.dart';

void main() {
group(
'Example tests',
() {
final st = Solution();
final solution = Solution();

test(
'true',
() => expect(
true,
st.isSymmetric(
solution.isSymmetric(
TreeNode(
1,
TreeNode(
Expand All @@ -32,7 +33,7 @@ void main() {
'false',
() => expect(
false,
st.isSymmetric(
solution.isSymmetric(
TreeNode(
1,
TreeNode(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import 'package:leetcode_dart/common/tree_node.dart';
import 'package:leetcode_dart/easy/maximum_depth_of_binary_tree.dart';
import 'package:leetcode_dart/easy/104_maximum_depth_of_binary_tree.dart';
import 'package:test/test.dart';

void main() {
group(
'Example tests',
() {
final mdoft = Solution();
final solution = Solution();

test(
'3',
() => expect(
3,
mdoft.maxDepth(
solution.maxDepth(
TreeNode(
3,
TreeNode(9),
Expand All @@ -28,7 +29,7 @@ void main() {
'2',
() => expect(
2,
mdoft.maxDepth(
solution.maxDepth(
TreeNode(
1,
null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:leetcode_dart/common/tree_node.dart';
import 'package:leetcode_dart/easy/convert_sorted_array_to_binary_search_tree.dart';
import 'package:leetcode_dart/easy/108_convert_sorted_array_to_binary_search_tree.dart';
import 'package:test/test.dart';

import '../common/binary_tree_helper.dart';
Expand All @@ -8,7 +8,8 @@ void main() {
group(
'Example tests',
() {
final csatbst = Solution();
final solution = Solution();

test(
'[0, -10, 5, null, -3, null, 9]',
() => expect(
Expand All @@ -27,7 +28,7 @@ void main() {
TreeNode(9),
),
),
csatbst.sortedArrayToBST(
solution.sortedArrayToBST(
[-10, -3, 0, 5, 9],
),
),
Expand All @@ -43,7 +44,9 @@ void main() {
null,
TreeNode(1),
),
csatbst.sortedArrayToBST([3, 1]),
solution.sortedArrayToBST(
[3, 1],
),
),
),
);
Expand Down
Loading