├── CHANGELOG.md ├── .github ├── dependabot.yaml ├── ISSUE_TEMPLATE │ ├── feature_request.md │ └── bug_report.md └── workflows │ └── checkout.yml ├── lib ├── leetcode.dart └── src │ ├── problem │ ├── 2235.add_two_integers.dart │ ├── 104.maximum_depth_of_binary_tree.dart │ ├── 206.reverse_linked_list.dart │ ├── 485.max_consecutive_ones.dart │ ├── 234.palindrome_linked_list.dart │ ├── 283.move_zeroes.dart │ ├── 203.remove_linked_list_elements.dart │ ├── 1480.running_sum_of_1d_array.dart │ ├── 61.rotate_list.dart │ ├── 876.middle_of_the_linked_list.dart │ ├── 19.remove_nth_node_from_end_of_list.dart │ ├── 250.count_univalue_subtrees.dart │ ├── 905.sort_array_by_parity.dart │ ├── 412.fizz_buzz.dart │ ├── 112.path_sum.dart │ ├── 941.valid_mountain_array.dart │ ├── 102.binary_tree_level_order_traversal.dart │ ├── 1089.duplicate_zeros.dart │ ├── 448.find_all_numbers_disappeared_in_an_array.dart │ ├── 1342.number_of_steps_to_reduce_a_number_to_zero.dart │ ├── 977.squares_of_a_sorted_array.dart │ ├── 9.palindrome_number.dart │ ├── 487.max_consecutive_ones_ii.dart │ ├── 1295.find_numbers_with_even_number_of_digits.dart │ ├── 328.odd_even_linked_list.dart │ ├── 2.add_two_numbers.dart │ ├── 144.binary_tree_preorder_traversal.dart │ ├── 1299.replace_elements_with_greatest_element_on_right_side.dart │ ├── 106.construct_binary_tree_from_inorder_and_postorder_traversal.dart │ ├── 101.symmetric_tree.dart │ ├── 1672.richest_customer_wealth.dart │ ├── 1051.height_checker.dart │ ├── 1346.check_if_n_and_its_double_exist.dart │ ├── 300.longest_increasing_subsequence.dart │ ├── 145.binary_tree_postorder_traversal.dart │ ├── 414.third_maximum_number.dart │ ├── 94.binary_tree_inorder_traversal.dart │ ├── 383.ransom_note.dart │ ├── 21.merge_two_sorted_lists.dart │ ├── 88.merge_sorted_array.dart │ ├── 26.remove_duplicates_from_sorted_array.dart │ ├── 27.remove_element.dart │ └── 707.design_linked_list.dart │ └── structure │ ├── list_node.dart │ └── tree_node.dart ├── tool └── makefile │ ├── pub.mk │ ├── test.mk │ └── environment.mk ├── .vscode ├── extensions.json ├── launch.json └── settings.json ├── Makefile ├── .gitignore ├── pubspec.yaml ├── test ├── problem │ ├── 2235.add_two_integers.test.dart │ ├── 1342.number_of_steps_to_reduce_a_number_to_zero.test.dart │ ├── 1089.duplicate_zeros.test.dart │ ├── 300.longest_increasing_subsequence.test.dart │ ├── 414.third_maximum_number.test.dart │ ├── 448.find_all_numbers_disappeared_in_an_array.test.dart │ ├── 485.max_consecutive_ones.test.dart │ ├── 383.ransom_note.test.dart │ ├── 412.fizz_buzz.test.dart │ ├── 977.squares_of_a_sorted_array.test.dart │ ├── 876.middle_of_the_linked_list.test.dart │ ├── 203.remove_linked_list_elements.test.dart │ ├── 106.construct_binary_tree_from_inorder_and_postorder_traversal.test.dart │ ├── 1480.running_sum_of_1d_array.test.dart │ ├── 9.palindrome_number.test.dart │ ├── 104.maximum_depth_of_binary_tree.test.dart │ ├── 283.move_zeroes.test.dart │ ├── 206.reverse_linked_list.test.dart │ ├── 487.max_consecutive_ones_ii.test.dart │ ├── 1051.height_checker.test.dart │ ├── 905.sort_array_by_parity.test.dart │ ├── 19.remove_nth_node_from_end_of_list.test.dart │ ├── 328.odd_even_linked_list.test.dart │ ├── 112.path_sum.test.dart │ ├── 102.binary_tree_level_order_traversal.test.dart │ ├── 144.binary_tree_preorder_traversal.test.dart │ ├── 145.binary_tree_postorder_traversal.test.dart │ ├── 1672.richest_customer_wealth.test.dart │ ├── 234.palindrome_linked_list.test.dart │ ├── 1299.replace_elements_with_greatest_element_on_right_side.test.dart │ ├── 26.remove_duplicates_from_sorted_array.test.dart │ ├── 1346.check_if_n_and_its_double_exist.test.dart │ ├── 27.remove_element.test.dart │ ├── 941.valid_mountain_array.test.dart │ ├── 250.count_univalue_subtrees.test.dart │ ├── 61.rotate_list.test.dart │ ├── 94.binary_tree_inorder_traversal.test.dart │ ├── 101.symmetric_tree.test.dart │ ├── 88.merge_sorted_array.test.dart │ ├── 1295.find_numbers_with_even_number_of_digits.test.dart │ ├── 2.add_two_numbers.test.dart │ ├── 707.design_linked_list.test.dart │ └── 21.merge_two_sorted_lists.test.dart ├── structure │ ├── list_node.test.dart │ └── tree_node.test.dart └── leetcode_test.dart ├── LICENSE ├── README.md ├── CODE_OF_CONDUCT.md └── analysis_options.yaml /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.github/dependabot.yaml: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /lib/leetcode.dart: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /tool/makefile/pub.mk: -------------------------------------------------------------------------------- 1 | .PHONY: get 2 | 3 | get: 4 | @dart pub get -------------------------------------------------------------------------------- /.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | "recommendations": [ 3 | "dart-code.dart-code" 4 | ] 5 | } -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: help 2 | 3 | help: 4 | @echo "LeetCode Dart solutions" 5 | @echo "" 6 | @dart --version 7 | 8 | -include tool/makefile/*.mk -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Files and directories created by pub. 2 | .dart_tool/ 3 | .packages 4 | 5 | # Conventional directory for build output. 6 | build/ 7 | 8 | pubspec.lock 9 | 10 | coverage/ -------------------------------------------------------------------------------- /tool/makefile/test.mk: -------------------------------------------------------------------------------- 1 | .PHONY: test 2 | 3 | test: get 4 | @dart run coverage:test_with_coverage -fb -o coverage -- \ 5 | --concurrency=6 --coverage=./coverage --reporter=expanded test/leetcode_test.dart -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.2.0", 3 | "configurations": [ 4 | { 5 | "name": "leetcode", 6 | "request": "launch", 7 | "type": "dart" 8 | } 9 | ] 10 | } -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "[dart]": { 3 | "editor.defaultFormatter": "Dart-Code.dart-code", 4 | "editor.formatOnPaste": true, 5 | "editor.formatOnSave": true, 6 | "editor.insertSpaces": true 7 | }, 8 | "dart.lineLength": 120, 9 | "editor.rulers": [ 10 | 120 11 | ], 12 | "dart.doNotFormat": [] 13 | } -------------------------------------------------------------------------------- /pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: leetcode 2 | description: LeetCode Dart solutions 3 | version: 0.0.1-dev 4 | repository: https://github.com/PlugFox/leetcode 5 | issue_tracker: https://github.com/PlugFox/leetcode/issues 6 | homepage: https://github.com/PlugFox/leetcode 7 | 8 | environment: 9 | sdk: ">=2.17.0 <3.0.0" 10 | 11 | dependencies: 12 | meta: ^1.7.0 13 | 14 | dev_dependencies: 15 | test: ^1.16.0 16 | coverage: any 17 | -------------------------------------------------------------------------------- /test/problem/2235.add_two_integers.test.dart: -------------------------------------------------------------------------------- 1 | import 'package:leetcode/src/problem/2235.add_two_integers.dart'; 2 | import 'package:test/test.dart'; 3 | 4 | void main() => group('add_two_integers', () { 5 | final f = Solution().sum; 6 | 7 | test('f(12, 5)', () { 8 | expect(f(12, 5), equals(17)); 9 | }); 10 | 11 | test('f(-10, 4)', () { 12 | expect(f(-10, 4), equals(-6)); 13 | }); 14 | }); 15 | -------------------------------------------------------------------------------- /test/problem/1342.number_of_steps_to_reduce_a_number_to_zero.test.dart: -------------------------------------------------------------------------------- 1 | import 'package:leetcode/src/problem/1342.number_of_steps_to_reduce_a_number_to_zero.dart'; 2 | import 'package:test/test.dart'; 3 | 4 | void main() => group('number_of_steps_to_reduce_a_number_to_zero', () { 5 | final f = Solution().numberOfSteps; 6 | 7 | test('f(14)', () { 8 | expect(f(14), equals(6)); 9 | }); 10 | 11 | test('f(8)', () { 12 | expect(f(8), equals(4)); 13 | }); 14 | 15 | test('f(123)', () { 16 | expect(f(123), equals(12)); 17 | }); 18 | }); 19 | -------------------------------------------------------------------------------- /test/problem/1089.duplicate_zeros.test.dart: -------------------------------------------------------------------------------- 1 | import 'package:leetcode/src/problem/1089.duplicate_zeros.dart'; 2 | import 'package:test/test.dart'; 3 | 4 | void main() => group('duplicate_zeros', () { 5 | final f = Solution().duplicateZeros; 6 | 7 | test('f([1,0,2,3,0,4,5,0])', () { 8 | final list = [1, 0, 2, 3, 0, 4, 5, 0]; 9 | f(list); 10 | expect(list, equals([1, 0, 0, 2, 3, 0, 0, 4])); 11 | }); 12 | 13 | test('f([1,2,3])', () { 14 | final list = [1, 2, 3]; 15 | f(list); 16 | expect(list, equals([1, 2, 3])); 17 | }); 18 | }); 19 | -------------------------------------------------------------------------------- /test/problem/300.longest_increasing_subsequence.test.dart: -------------------------------------------------------------------------------- 1 | import 'package:leetcode/src/problem/300.longest_increasing_subsequence.dart'; 2 | import 'package:test/test.dart'; 3 | 4 | void main() => group('longest_increasing_subsequence', () { 5 | final f = Solution().lengthOfLIS; 6 | 7 | test('f([10,9,2,5,3,7,101,18])', () { 8 | expect(f([10, 9, 2, 5, 3, 7, 101, 18]), equals(4)); 9 | }); 10 | 11 | test('f([0,1,0,3,2,3])', () { 12 | expect(f([0, 1, 0, 3, 2, 3]), equals(4)); 13 | }); 14 | 15 | test('f([7,7,7,7,7,7,7])', () { 16 | expect(f([7, 7, 7, 7, 7, 7, 7]), equals(1)); 17 | }); 18 | }); 19 | -------------------------------------------------------------------------------- /test/problem/414.third_maximum_number.test.dart: -------------------------------------------------------------------------------- 1 | import 'package:leetcode/src/problem/414.third_maximum_number.dart'; 2 | import 'package:test/test.dart'; 3 | 4 | void main() => group('third_maximum_number', () { 5 | final f = Solution().thirdMax; 6 | 7 | test('f([1,2])', () { 8 | expect(f([1, 2]), equals(2)); 9 | }); 10 | 11 | test('f([3,2,1])', () { 12 | expect(f([3, 2, 1]), equals(1)); 13 | }); 14 | 15 | test('f([2,2,3,1])', () { 16 | expect(f([2, 2, 3, 1]), equals(1)); 17 | }); 18 | 19 | test('f([])', () { 20 | expect(() => f([]), throwsArgumentError); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /test/problem/448.find_all_numbers_disappeared_in_an_array.test.dart: -------------------------------------------------------------------------------- 1 | import 'package:leetcode/src/problem/448.find_all_numbers_disappeared_in_an_array.dart'; 2 | import 'package:test/test.dart'; 3 | 4 | void main() => group('find_all_numbers_disappeared_in_an_array', () { 5 | final f = Solution().findDisappearedNumbers; 6 | 7 | test('f([1,2])', () { 8 | expect(f([1, 2]), isEmpty); 9 | }); 10 | 11 | test('f([4,3,2,7,8,2,3,1])', () { 12 | expect(f([4, 3, 2, 7, 8, 2, 3, 1]), containsAll([5, 6])); 13 | }); 14 | 15 | test('f([1,1])', () { 16 | expect(f([1, 1]), containsAll([2])); 17 | }); 18 | }); 19 | -------------------------------------------------------------------------------- /lib/src/problem/2235.add_two_integers.dart: -------------------------------------------------------------------------------- 1 | /* 2 | * 2235. Add Two Integers 3 | * 4 | * Given two integers num1 and num2, return the sum of the two integers. 5 | * 6 | * 7 | * Example 1: 8 | * 9 | * Input: num1 = 12, num2 = 5 10 | * Output: 17 11 | * Explanation: num1 is 12, num2 is 5, and their sum is 12 + 5 = 17, so 17 is returned. 12 | * 13 | * 14 | * Example 2: 15 | * 16 | * Input: num1 = -10, num2 = 4 17 | * Output: -6 18 | * Explanation: num1 + num2 = -6, so -6 is returned. 19 | * 20 | * 21 | * Constraints: 22 | * 23 | * -100 <= num1, num2 <= 100 24 | */ 25 | 26 | class Solution { 27 | int sum(int num1, int num2) => num1 + num2; 28 | } 29 | -------------------------------------------------------------------------------- /test/problem/485.max_consecutive_ones.test.dart: -------------------------------------------------------------------------------- 1 | import 'package:leetcode/src/problem/485.max_consecutive_ones.dart'; 2 | import 'package:test/test.dart'; 3 | 4 | void main() => group('max_consecutive_ones', () { 5 | final f = Solution().findMaxConsecutiveOnes; 6 | 7 | test('f([0])', () { 8 | expect(f([0]), equals(0)); 9 | }); 10 | 11 | test('f([1])', () { 12 | expect(f([1]), equals(1)); 13 | }); 14 | 15 | test('f([1,1,0,1,1,1])', () { 16 | expect(f([1, 1, 0, 1, 1, 1]), equals(3)); 17 | }); 18 | 19 | test('f([1,0,1,1,0,1])', () { 20 | expect(f([1, 0, 1, 1, 0, 1]), equals(2)); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: "" 5 | labels: "" 6 | assignees: "" 7 | --- 8 | 9 | **Is your feature request related to a problem? Please describe.** 10 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 11 | 12 | **Describe the solution you'd like** 13 | A clear and concise description of what you want to happen. 14 | 15 | **Describe alternatives you've considered** 16 | A clear and concise description of any alternative solutions or features you've considered. 17 | 18 | **Additional context** 19 | Add any other context or screenshots about the feature request here. 20 | -------------------------------------------------------------------------------- /tool/makefile/environment.mk: -------------------------------------------------------------------------------- 1 | 2 | PLATFORM := $(shell arch) 3 | ifeq ($(PLATFORM),x86_64) 4 | GLIBC := assets/glibc/x86_64/glibc-2.29-r0.apk 5 | GLIBC_BIN := assets/glibc/x86_64/glibc-bin-2.29-r0.apk 6 | else ifeq ($(PLATFORM),arm64) 7 | GLIBC := assets/glibc/arm/glibc-2.30-r0.apk 8 | GLIBC_BIN := assets/glibc/arm/glibc-bin-2.30-r0.apk 9 | else 10 | GLIBC := assets/glibc/x86_64/glibc-2.29-r0.apk 11 | GLIBC_BIN := assets/glibc/x86_64/glibc-bin-2.29-r0.apk 12 | endif 13 | 14 | ifeq ($(OS),Windows_NT) 15 | OS := win 16 | else 17 | _detected_OS := $(shell uname -s) 18 | ifeq ($(_detected_OS),Linux) 19 | OS := lin 20 | else ifeq ($(_detected_OS),Darwin) 21 | OS := mac 22 | endif 23 | endif 24 | -------------------------------------------------------------------------------- /test/problem/383.ransom_note.test.dart: -------------------------------------------------------------------------------- 1 | import 'package:leetcode/src/problem/383.ransom_note.dart'; 2 | import 'package:test/test.dart'; 3 | 4 | void main() => group('ransom_note', () { 5 | final f1 = SolutionV1().canConstruct; 6 | final f2 = SolutionV2().canConstruct; 7 | 8 | test('f(a, b)', () { 9 | expect(f1('a', 'b'), isFalse); 10 | expect(f2('a', 'b'), isFalse); 11 | }); 12 | 13 | test('f(aa, ab)', () { 14 | expect(f1('aa', 'ab'), isFalse); 15 | expect(f2('aa', 'ab'), isFalse); 16 | }); 17 | 18 | test('f(aa, aab)', () { 19 | expect(f1('aa', 'aab'), isTrue); 20 | expect(f2('aa', 'aab'), isTrue); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /test/problem/412.fizz_buzz.test.dart: -------------------------------------------------------------------------------- 1 | import 'package:leetcode/src/problem/412.fizz_buzz.dart'; 2 | import 'package:test/test.dart'; 3 | 4 | void main() => group('fizz_buzz', () { 5 | final f = Solution().fizzBuzz; 6 | 7 | test('f(3)', () { 8 | expect(f(3), equals(['1', '2', 'Fizz'])); 9 | }); 10 | 11 | test('f(5)', () { 12 | expect(f(5), equals(['1', '2', 'Fizz', '4', 'Buzz'])); 13 | }); 14 | 15 | test('f(15)', () { 16 | expect( 17 | f(15), 18 | equals( 19 | ['1', '2', 'Fizz', '4', 'Buzz', 'Fizz', '7', '8', 'Fizz', 'Buzz', '11', 'Fizz', '13', '14', 'FizzBuzz'], 20 | ), 21 | ); 22 | }); 23 | }); 24 | -------------------------------------------------------------------------------- /test/problem/977.squares_of_a_sorted_array.test.dart: -------------------------------------------------------------------------------- 1 | import 'package:leetcode/src/problem/977.squares_of_a_sorted_array.dart'; 2 | import 'package:test/test.dart'; 3 | 4 | void main() => group('squares_of_a_sorted_array', () { 5 | final f = Solution().sortedSquares; 6 | 7 | test('f([])', () { 8 | expect(f([]), isEmpty); 9 | }); 10 | 11 | test('f([0])', () { 12 | expect(f([0]), equals([0])); 13 | }); 14 | 15 | test('f([-4,-1,0,3,10])', () { 16 | expect(f([-4, -1, 0, 3, 10]), equals([0, 1, 9, 16, 100])); 17 | }); 18 | 19 | test('f([-7,-3,2,3,11])', () { 20 | expect(f([-7, -3, 2, 3, 11]), equals([4, 9, 9, 49, 121])); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /test/problem/876.middle_of_the_linked_list.test.dart: -------------------------------------------------------------------------------- 1 | import 'package:leetcode/src/problem/876.middle_of_the_linked_list.dart'; 2 | import 'package:leetcode/src/structure/list_node.dart'; 3 | import 'package:test/test.dart'; 4 | 5 | void main() => group('middle_of_the_linked_list', () { 6 | final f = Solution().middleNode; 7 | 8 | test('f(null)', () { 9 | expect(f(null), isNull); 10 | }); 11 | 12 | test('f([1, 2, 3, 4, 5])', () { 13 | expect(f(ListNode.of([1, 2, 3, 4, 5])), orderedEquals([3, 4, 5])); 14 | }); 15 | 16 | test('f([1, 2, 3, 4, 5, 6])', () { 17 | expect(f(ListNode.of([1, 2, 3, 4, 5, 6])), orderedEquals([4, 5, 6])); 18 | }); 19 | }); 20 | -------------------------------------------------------------------------------- /test/problem/203.remove_linked_list_elements.test.dart: -------------------------------------------------------------------------------- 1 | import 'package:leetcode/src/problem/203.remove_linked_list_elements.dart'; 2 | import 'package:leetcode/src/structure/list_node.dart'; 3 | import 'package:test/test.dart'; 4 | 5 | void main() => group('remove_linked_list_elements', () { 6 | final f = Solution().removeElements; 7 | 8 | test('f([], 1)', () { 9 | expect(f(null, 1), isNull); 10 | }); 11 | 12 | test('f([7,7,7,7], 7)', () { 13 | expect(f(ListNode.of([7, 7, 7, 7]), 7), isNull); 14 | }); 15 | 16 | test('f([1,2,6,3,4,5,6], 6)', () { 17 | expect(f(ListNode.of([1, 2, 6, 3, 4, 5, 6]), 6)?.toList(), equals([1, 2, 3, 4, 5])); 18 | }); 19 | }); 20 | -------------------------------------------------------------------------------- /test/problem/106.construct_binary_tree_from_inorder_and_postorder_traversal.test.dart: -------------------------------------------------------------------------------- 1 | import 'package:leetcode/src/problem/106.construct_binary_tree_from_inorder_and_postorder_traversal.dart'; 2 | import 'package:test/test.dart'; 3 | 4 | void main() => group('construct_binary_tree_from_inorder_and_postorder_traversal', () { 5 | final f = Solution().buildTree; 6 | 7 | test('f([9,3,15,20,7],[9,15,7,20,3])', () { 8 | expect(f([9, 3, 15, 20, 7], [9, 15, 7, 20, 3])?.toList(), equals([3, 9, 20, null, null, 15, 7])); 9 | }); 10 | 11 | test('f([-1],[-1])', () { 12 | expect(f([-1], [-1])?.toList(), equals([-1])); 13 | }); 14 | 15 | test('f([],[])', () { 16 | expect(f([], []), equals(null)); 17 | }); 18 | }); 19 | -------------------------------------------------------------------------------- /test/problem/1480.running_sum_of_1d_array.test.dart: -------------------------------------------------------------------------------- 1 | import 'package:leetcode/src/problem/1480.running_sum_of_1d_array.dart'; 2 | import 'package:test/test.dart'; 3 | 4 | void main() => group('running_sum_of_1d_array', () { 5 | final f = Solution().runningSum; 6 | 7 | test('f([1,2,3,4])', () { 8 | expect( 9 | f([1, 2, 3, 4]), 10 | equals([1, 3, 6, 10]), 11 | ); 12 | }); 13 | 14 | test('f([1,1,1,1,1])', () { 15 | expect( 16 | f([1, 1, 1, 1, 1]), 17 | equals([1, 2, 3, 4, 5]), 18 | ); 19 | }); 20 | 21 | test('f([3,1,2,10,1])', () { 22 | expect( 23 | f([3, 1, 2, 10, 1]), 24 | equals([3, 4, 6, 16, 17]), 25 | ); 26 | }); 27 | }); 28 | -------------------------------------------------------------------------------- /test/problem/9.palindrome_number.test.dart: -------------------------------------------------------------------------------- 1 | import 'package:leetcode/src/problem/9.palindrome_number.dart'; 2 | import 'package:test/test.dart'; 3 | 4 | void main() => group('palindrome_number', () { 5 | final f = Solution().isPalindrome; 6 | 7 | test('f(0)', () { 8 | expect(f(0), isTrue); 9 | }); 10 | 11 | test('f(121)', () { 12 | expect(f(121), isTrue); 13 | }); 14 | 15 | test('f(-121)', () { 16 | expect(f(-121), isFalse); 17 | }); 18 | 19 | test('f(11)', () { 20 | expect(f(11), isTrue); 21 | }); 22 | 23 | test('f(1001)', () { 24 | expect(f(1001), isTrue); 25 | }); 26 | 27 | test('f(10)', () { 28 | expect(f(10), isFalse); 29 | }); 30 | }); 31 | -------------------------------------------------------------------------------- /test/problem/104.maximum_depth_of_binary_tree.test.dart: -------------------------------------------------------------------------------- 1 | import 'package:leetcode/src/problem/104.maximum_depth_of_binary_tree.dart'; 2 | import 'package:leetcode/src/structure/tree_node.dart'; 3 | import 'package:test/test.dart'; 4 | 5 | void main() => group('maximum_depth_of_binary_tree', () { 6 | final f = Solution().maxDepth; 7 | 8 | test('f([3,9,20,null,null,15,7])', () { 9 | expect(f(TreeNode.of([3, 9, 20, null, null, 15, 7])), equals(3)); 10 | }); 11 | 12 | test('f([])', () { 13 | expect(f(TreeNode.of([])), equals(0)); 14 | }); 15 | 16 | test('f([1])', () { 17 | expect(f(TreeNode.of([1])), equals(1)); 18 | }); 19 | 20 | test('f([1,null,2])', () { 21 | expect(f(TreeNode.of([1, null, 2])), equals(2)); 22 | }); 23 | }); 24 | -------------------------------------------------------------------------------- /test/problem/283.move_zeroes.test.dart: -------------------------------------------------------------------------------- 1 | import 'package:leetcode/src/problem/283.move_zeroes.dart'; 2 | import 'package:test/test.dart'; 3 | 4 | void main() => group('move_zeroes', () { 5 | final moveZeroes = Solution().moveZeroes; 6 | List f(List list) { 7 | moveZeroes(list); 8 | return list; 9 | } 10 | 11 | test('f([0,1,0,3,12])', () { 12 | expect(f([0, 1, 0, 3, 12]), equals([1, 3, 12, 0, 0])); 13 | }); 14 | 15 | test('f([0,4,0,0,2,1,0])', () { 16 | expect(f([0, 4, 0, 0, 2, 1, 0]), equals([4, 2, 1, 0, 0, 0, 0])); 17 | }); 18 | 19 | test('f([1, 0])', () { 20 | expect(f([1, 0]), equals([1, 0])); 21 | }); 22 | 23 | test('f([0])', () { 24 | expect(f([0]), equals([0])); 25 | }); 26 | }); 27 | -------------------------------------------------------------------------------- /test/problem/206.reverse_linked_list.test.dart: -------------------------------------------------------------------------------- 1 | import 'package:leetcode/src/problem/206.reverse_linked_list.dart'; 2 | import 'package:leetcode/src/structure/list_node.dart'; 3 | import 'package:test/test.dart'; 4 | 5 | void main() => group('reverse_linked_list', () { 6 | final f = Solution().reverseList; 7 | 8 | test('f([])', () { 9 | expect(f(null), isNull); 10 | }); 11 | 12 | test('f([1])', () { 13 | expect(f(ListNode.of([1])), containsAllInOrder([1])); 14 | }); 15 | 16 | test('f([1,2])', () { 17 | expect(f(ListNode.of([1, 2])), containsAllInOrder([2, 1])); 18 | }); 19 | 20 | test('f([1,2,3,4,5])', () { 21 | expect(f(ListNode.of([1, 2, 3, 4, 5])), containsAllInOrder([5, 4, 3, 2, 1])); 22 | }); 23 | }); 24 | -------------------------------------------------------------------------------- /test/problem/487.max_consecutive_ones_ii.test.dart: -------------------------------------------------------------------------------- 1 | import 'package:leetcode/src/problem/487.max_consecutive_ones_ii.dart'; 2 | import 'package:test/test.dart'; 3 | 4 | void main() => group('max_consecutive_ones_ii', () { 5 | final f = Solution().findMaxConsecutiveOnes; 6 | 7 | test('f([1])', () { 8 | expect(f([1]), equals(1)); 9 | }); 10 | 11 | test('f([1,0,1,1,0])', () { 12 | expect(f([1, 0, 1, 1, 0]), equals(4)); 13 | }); 14 | 15 | test('f([1,0,1,1,0,1])', () { 16 | expect(f([1, 0, 1, 1, 0, 1]), equals(4)); 17 | }); 18 | 19 | test('f([1,1,0,1])', () { 20 | expect(f([1, 1, 0, 1]), equals(4)); 21 | }); 22 | 23 | test('f([0,1,0,0,1,1,1,1])', () { 24 | expect(f([0, 1, 0, 0, 1, 1, 1, 1]), equals(5)); 25 | }); 26 | }); 27 | -------------------------------------------------------------------------------- /test/problem/1051.height_checker.test.dart: -------------------------------------------------------------------------------- 1 | import 'package:leetcode/src/problem/1051.height_checker.dart'; 2 | import 'package:test/test.dart'; 3 | 4 | void main() => group('height_checker', () { 5 | final f = Solution().heightChecker; 6 | 7 | test('f([])', () { 8 | expect(f([]), equals(0)); 9 | }); 10 | 11 | test('f([1])', () { 12 | expect(f([1]), equals(0)); 13 | }); 14 | 15 | test('f([2, 1])', () { 16 | expect(f([2, 1]), equals(2)); 17 | }); 18 | 19 | test('f([1,1,4,2,1,3])', () { 20 | expect(f([1, 1, 4, 2, 1, 3]), equals(3)); 21 | }); 22 | 23 | test('f([5,1,2,3,4])', () { 24 | expect(f([5, 1, 2, 3, 4]), equals(5)); 25 | }); 26 | 27 | test('f([1,2,3,4,5])', () { 28 | expect(f([1, 2, 3, 4, 5]), equals(0)); 29 | }); 30 | }); 31 | -------------------------------------------------------------------------------- /test/problem/905.sort_array_by_parity.test.dart: -------------------------------------------------------------------------------- 1 | import 'package:leetcode/src/problem/905.sort_array_by_parity.dart'; 2 | import 'package:test/test.dart'; 3 | 4 | void main() => group('sort_array_by_parity', () { 5 | final f = Solution().sortArrayByParity; 6 | 7 | test('f([3,1,2,4])', () { 8 | expect(f([3, 1, 2, 4]).take(2), containsAll([2, 4])); 9 | expect(f([3, 1, 2, 4]).skip(2), containsAll([3, 1])); 10 | }); 11 | 12 | test('f([1,1,0,1,0,0,1])', () { 13 | expect(f([1, 1, 0, 1, 0, 0, 1]), containsAllInOrder([0, 0, 0, 1, 1, 1, 1])); 14 | }); 15 | 16 | test('f([0,0,1,1,0,0,1])', () { 17 | expect(f([0, 0, 1, 1, 0, 0, 1]), containsAllInOrder([0, 0, 0, 0, 1, 1, 1])); 18 | }); 19 | 20 | test('f([0])', () { 21 | expect(f([0]), equals([0])); 22 | }); 23 | }); 24 | -------------------------------------------------------------------------------- /test/problem/19.remove_nth_node_from_end_of_list.test.dart: -------------------------------------------------------------------------------- 1 | import 'package:leetcode/src/problem/19.remove_nth_node_from_end_of_list.dart'; 2 | import 'package:leetcode/src/structure/list_node.dart'; 3 | import 'package:test/test.dart'; 4 | 5 | void main() => group('remove_nth_node_from_end_of_list', () { 6 | final f = Solution().removeNthFromEnd; 7 | 8 | test('f([1])', () { 9 | expect(f(ListNode.of([1]), 1), isNull); 10 | }); 11 | 12 | test('f([1,2])', () { 13 | expect(f(ListNode.of([1, 2]), 1), containsAllInOrder([1])); 14 | }); 15 | 16 | test('f([1,3])', () { 17 | expect(f(ListNode.of([1, 3]), 2), containsAllInOrder([3])); 18 | }); 19 | 20 | test('f([1,2,3,4,5])', () { 21 | expect(f(ListNode.of([1, 2, 3, 4, 5]), 2), containsAllInOrder([1, 2, 3, 5])); 22 | }); 23 | }); 24 | -------------------------------------------------------------------------------- /lib/src/problem/104.maximum_depth_of_binary_tree.dart: -------------------------------------------------------------------------------- 1 | /* 2 | * 104. Maximum Depth of Binary Tree 3 | * 4 | * Given the root of a binary tree, return its maximum depth. 5 | * 6 | * A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 7 | * 8 | * 9 | * Example 1: 10 | * 11 | * Input: root = [3,9,20,null,null,15,7] 12 | * Output: 3 13 | * 14 | * 15 | * Example 2: 16 | * 17 | * Input: root = [1,null,2] 18 | * Output: 2 19 | * 20 | * 21 | * Constraints: 22 | * 23 | * The number of nodes in the tree is in the range [0, 104]. 24 | * -100 <= Node.val <= 100 25 | */ 26 | 27 | import 'dart:math' as math; 28 | 29 | import '../structure/tree_node.dart'; 30 | 31 | class Solution { 32 | int maxDepth(TreeNode? root) => root == null ? 0 : math.max(maxDepth(root.left), maxDepth(root.right)) + 1; 33 | } 34 | -------------------------------------------------------------------------------- /test/problem/328.odd_even_linked_list.test.dart: -------------------------------------------------------------------------------- 1 | import 'package:leetcode/src/problem/328.odd_even_linked_list.dart'; 2 | import 'package:leetcode/src/structure/list_node.dart'; 3 | import 'package:test/test.dart'; 4 | 5 | void main() => group('odd_even_linked_list', () { 6 | final f = Solution().oddEvenList; 7 | 8 | test('f([])', () { 9 | expect(f(null)?.toList(), isNull); 10 | }); 11 | 12 | test('f([1,2,3,4,5,6,7,8])', () { 13 | expect(f(ListNode.of([1, 2, 3, 4, 5, 6, 7, 8]))?.toList(), equals([1, 3, 5, 7, 2, 4, 6, 8])); 14 | }); 15 | 16 | test('f([1,2,3,4,5])', () { 17 | expect(f(ListNode.of([1, 2, 3, 4, 5]))?.toList(), equals([1, 3, 5, 2, 4])); 18 | }); 19 | 20 | test('f([2,1,3,5,6,4,7])', () { 21 | expect(f(ListNode.of([2, 1, 3, 5, 6, 4, 7]))?.toList(), equals([2, 3, 6, 7, 1, 5, 4])); 22 | }); 23 | }); 24 | -------------------------------------------------------------------------------- /test/problem/112.path_sum.test.dart: -------------------------------------------------------------------------------- 1 | import 'package:leetcode/src/problem/112.path_sum.dart'; 2 | import 'package:leetcode/src/structure/tree_node.dart'; 3 | import 'package:test/test.dart'; 4 | 5 | void main() => group('path_sum', () { 6 | final f = Solution().hasPathSum; 7 | 8 | test('f([5,4,8,11,null,13,4,7,2,null,null,null,1], 22)', () { 9 | expect(f(TreeNode.of([5, 4, 8, 11, null, 13, 4, 7, 2, null, null, null, 1]), 22), isTrue); 10 | }); 11 | 12 | test('f([1,2,3], 5)', () { 13 | expect(f(TreeNode.of([1, 2, 3]), 5), isFalse); 14 | }); 15 | 16 | test('f([1], 1)', () { 17 | expect(f(TreeNode.of([1]), 1), isTrue); 18 | }); 19 | 20 | test('f([], 0)', () { 21 | expect(f(TreeNode.of([]), 0), isFalse); 22 | }); 23 | 24 | test('f([1,2], 1)', () { 25 | expect(f(TreeNode.of([1, 2]), 1), isFalse); 26 | }); 27 | }); 28 | -------------------------------------------------------------------------------- /test/problem/102.binary_tree_level_order_traversal.test.dart: -------------------------------------------------------------------------------- 1 | import 'package:leetcode/src/problem/102.binary_tree_level_order_traversal.dart'; 2 | import 'package:leetcode/src/structure/tree_node.dart'; 3 | import 'package:test/test.dart'; 4 | 5 | void main() => group('binary_tree_level_order_traversal', () { 6 | final f = Solution().levelOrder; 7 | 8 | test('f([3,9,20,null,null,15,7])', () { 9 | expect( 10 | f(TreeNode.of([3, 9, 20, null, null, 15, 7])), 11 | equals([ 12 | [3], 13 | [9, 20], 14 | [15, 7] 15 | ]), 16 | ); 17 | }); 18 | 19 | test('f([1])', () { 20 | expect( 21 | f(TreeNode.of([1])), 22 | equals([ 23 | [1] 24 | ]), 25 | ); 26 | }); 27 | 28 | test('f([])', () { 29 | expect(f(TreeNode.of([])), isEmpty); 30 | }); 31 | }); 32 | -------------------------------------------------------------------------------- /test/problem/144.binary_tree_preorder_traversal.test.dart: -------------------------------------------------------------------------------- 1 | import 'package:leetcode/src/problem/144.binary_tree_preorder_traversal.dart'; 2 | import 'package:leetcode/src/structure/tree_node.dart'; 3 | import 'package:test/test.dart'; 4 | 5 | void main() => group('binary_tree_preorder_traversal', () { 6 | final f1 = SolutionV1().preorderTraversal; 7 | final f2 = SolutionV2().preorderTraversal; 8 | 9 | test('f([1,null,2,3])', () { 10 | expect(f1(TreeNode.of([1, null, 2, 3])), equals([1, 2, 3])); 11 | expect(f2(TreeNode.of([1, null, 2, 3])), equals([1, 2, 3])); 12 | }); 13 | 14 | test('f([])', () { 15 | expect(f1(TreeNode.of([])), isEmpty); 16 | expect(f2(TreeNode.of([])), isEmpty); 17 | }); 18 | 19 | test('f([1])', () { 20 | expect(f1(TreeNode.of([1])), equals([1])); 21 | expect(f2(TreeNode.of([1])), equals([1])); 22 | }); 23 | }); 24 | -------------------------------------------------------------------------------- /test/problem/145.binary_tree_postorder_traversal.test.dart: -------------------------------------------------------------------------------- 1 | import 'package:leetcode/src/problem/145.binary_tree_postorder_traversal.dart'; 2 | import 'package:leetcode/src/structure/tree_node.dart'; 3 | import 'package:test/test.dart'; 4 | 5 | void main() => group('binary_tree_postorder_traversal', () { 6 | final f1 = SolutionV1().postorderTraversal; 7 | final f2 = SolutionV2().postorderTraversal; 8 | 9 | test('f([1,null,2,3])', () { 10 | expect(f1(TreeNode.of([1, null, 2, 3])), equals([3, 2, 1])); 11 | expect(f2(TreeNode.of([1, null, 2, 3])), equals([3, 2, 1])); 12 | }); 13 | 14 | test('f([])', () { 15 | expect(f1(TreeNode.of([])), isEmpty); 16 | expect(f2(TreeNode.of([])), isEmpty); 17 | }); 18 | 19 | test('f([1])', () { 20 | expect(f1(TreeNode.of([1])), equals([1])); 21 | expect(f2(TreeNode.of([1])), equals([1])); 22 | }); 23 | }); 24 | -------------------------------------------------------------------------------- /lib/src/problem/206.reverse_linked_list.dart: -------------------------------------------------------------------------------- 1 | /* 2 | * 206. Reverse Linked List 3 | * 4 | * Given the head of a singly linked list, reverse the list, and return the reversed list. 5 | * 6 | * 7 | * Example 1: 8 | * 9 | * Input: head = [1,2,3,4,5] 10 | * Output: [5,4,3,2,1] 11 | * 12 | * 13 | * Example 2: 14 | * 15 | * Input: head = [1,2] 16 | * Output: [2,1] 17 | * 18 | * 19 | * Example 3: 20 | * 21 | * Input: head = [] 22 | * Output: [] 23 | * 24 | * 25 | * Constraints: 26 | * 27 | * The number of nodes in the list is the range [0, 5000]. 28 | * -5000 <= Node.val <= 5000 29 | */ 30 | 31 | import '../structure/list_node.dart'; 32 | 33 | class Solution { 34 | ListNode? reverseList(ListNode? head) { 35 | var prev = head, next = head?.next; 36 | head?.next = null; 37 | while (next != null) { 38 | prev = next; 39 | next = next.next; 40 | head = prev..next = head; 41 | } 42 | return head; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /test/problem/1672.richest_customer_wealth.test.dart: -------------------------------------------------------------------------------- 1 | import 'package:leetcode/src/problem/1672.richest_customer_wealth.dart'; 2 | import 'package:test/test.dart'; 3 | 4 | void main() => group('richest_customer_wealth', () { 5 | final f = Solution().maximumWealth; 6 | 7 | test('f([[1,2,3],[3,2,1]])', () { 8 | expect( 9 | f([ 10 | [1, 2, 3], 11 | [3, 2, 1] 12 | ]), 13 | equals(6), 14 | ); 15 | }); 16 | 17 | test('f([[1,5],[7,3],[3,5]])', () { 18 | expect( 19 | f([ 20 | [1, 5], 21 | [7, 3], 22 | [3, 5] 23 | ]), 24 | equals(10), 25 | ); 26 | }); 27 | 28 | test('f([[2,8,7],[7,1,3],[1,9,5]])', () { 29 | expect( 30 | f([ 31 | [2, 8, 7], 32 | [7, 1, 3], 33 | [1, 9, 5] 34 | ]), 35 | equals(17), 36 | ); 37 | }); 38 | }); 39 | -------------------------------------------------------------------------------- /test/problem/234.palindrome_linked_list.test.dart: -------------------------------------------------------------------------------- 1 | import 'package:leetcode/src/problem/234.palindrome_linked_list.dart'; 2 | import 'package:leetcode/src/structure/list_node.dart'; 3 | import 'package:test/test.dart'; 4 | 5 | void main() => group('palindrome_linked_list', () { 6 | final f = Solution().isPalindrome; 7 | 8 | test('f([])', () { 9 | expect(f(null), isTrue); 10 | }); 11 | 12 | test('f([1])', () { 13 | expect(f(ListNode.of([1])), isTrue); 14 | }); 15 | 16 | test('f([1,2])', () { 17 | expect(f(ListNode.of([1, 2])), isFalse); 18 | }); 19 | 20 | test('f([1,2,1])', () { 21 | expect(f(ListNode.of([1, 2, 1])), isTrue); 22 | }); 23 | 24 | test('f([1,2,1,4])', () { 25 | expect(f(ListNode.of([1, 2, 1, 4])), isFalse); 26 | }); 27 | 28 | test('f([1, 2, 2, 1])', () { 29 | expect(f(ListNode.of([1, 2, 2, 1])), isTrue); 30 | }); 31 | }); 32 | -------------------------------------------------------------------------------- /test/problem/1299.replace_elements_with_greatest_element_on_right_side.test.dart: -------------------------------------------------------------------------------- 1 | import 'package:leetcode/src/problem/1299.replace_elements_with_greatest_element_on_right_side.dart'; 2 | import 'package:test/test.dart'; 3 | 4 | void main() => group('replace_elements_with_greatest_element_on_right_side', () { 5 | final f = Solution().replaceElements; 6 | 7 | test('f([])', () { 8 | expect(f([]), equals([])); 9 | }); 10 | 11 | test('f([400])', () { 12 | expect(f([400]), equals([-1])); 13 | }); 14 | 15 | test('f([17,18,5,4,6,1])', () { 16 | expect( 17 | f([17, 18, 5, 4, 6, 1]), 18 | equals([18, 6, 6, 6, 1, -1]), 19 | ); 20 | }); 21 | 22 | test('f([57010,40840,69871,14425,70605])', () { 23 | expect( 24 | f([57010, 40840, 69871, 14425, 70605]), 25 | equals([70605, 70605, 70605, 70605, -1]), 26 | ); 27 | }); 28 | }); 29 | -------------------------------------------------------------------------------- /test/problem/26.remove_duplicates_from_sorted_array.test.dart: -------------------------------------------------------------------------------- 1 | import 'package:leetcode/src/problem/26.remove_duplicates_from_sorted_array.dart'; 2 | import 'package:test/test.dart'; 3 | 4 | void main() => group('remove_duplicates_from_sorted_array', () { 5 | final f = Solution().removeDuplicates; 6 | 7 | test('f([])', () { 8 | final list = []; 9 | final result = f(list); 10 | expect(list, isEmpty); 11 | expect(result, equals(0)); 12 | }); 13 | 14 | test('f([1,1,2])', () { 15 | final list = [1, 1, 2]; 16 | final result = f(list); 17 | expect(list, containsAllInOrder([1, 2])); 18 | expect(result, equals(2)); 19 | }); 20 | 21 | test('f([0,0,1,1,1,2,2,3,3,4])', () { 22 | final list = [0, 0, 1, 1, 1, 2, 2, 3, 3, 4]; 23 | final result = f(list); 24 | expect(list, containsAllInOrder([0, 1, 2, 3, 4])); 25 | expect(result, equals(5)); 26 | }); 27 | }); 28 | -------------------------------------------------------------------------------- /test/problem/1346.check_if_n_and_its_double_exist.test.dart: -------------------------------------------------------------------------------- 1 | import 'package:leetcode/src/problem/1346.check_if_n_and_its_double_exist.dart'; 2 | import 'package:test/test.dart'; 3 | 4 | void main() => group('check_if_n_and_its_double_exist', () { 5 | final f = Solution().checkIfExist; 6 | 7 | test('f([])', () { 8 | expect(f([]), isFalse); 9 | }); 10 | 11 | test('f([10,0,2,0])', () { 12 | expect(f([10, 0, 2, 0]), isTrue); 13 | }); 14 | 15 | test('f([10,0,2,3])', () { 16 | expect(f([10, 0, 2, 3]), isFalse); 17 | }); 18 | 19 | test('f([10,2,5,3])', () { 20 | expect(f([10, 2, 5, 3]), isTrue); 21 | }); 22 | 23 | test('f([7,1,14,11])', () { 24 | expect(f([7, 1, 14, 11]), isTrue); 25 | }); 26 | 27 | test('f([3,1,7,11])', () { 28 | expect(f([3, 1, 7, 11]), isFalse); 29 | }); 30 | 31 | test('f([-10,12,-20,-8,15])', () { 32 | expect(f([-10, 12, -20, -8, 15]), isTrue); 33 | }); 34 | }); 35 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: "" 5 | labels: "" 6 | assignees: "" 7 | --- 8 | 9 | **Describe the bug** 10 | A clear and concise description of what the bug is. 11 | 12 | **To Reproduce** 13 | Steps to reproduce the behavior: 14 | 15 | 1. Go to '...' 16 | 2. Click on '....' 17 | 3. Scroll down to '....' 18 | 4. See error 19 | 20 | **Expected behavior** 21 | A clear and concise description of what you expected to happen. 22 | 23 | **Screenshots** 24 | If applicable, add screenshots to help explain your problem. 25 | 26 | **Desktop (please complete the following information):** 27 | 28 | - OS: [e.g. iOS] 29 | - Browser [e.g. chrome, safari] 30 | - Version [e.g. 22] 31 | 32 | **Smartphone (please complete the following information):** 33 | 34 | - Device: [e.g. iPhone6] 35 | - OS: [e.g. iOS8.1] 36 | - Browser [e.g. stock browser, safari] 37 | - Version [e.g. 22] 38 | 39 | **Additional context** 40 | Add any other context about the problem here. 41 | -------------------------------------------------------------------------------- /lib/src/problem/485.max_consecutive_ones.dart: -------------------------------------------------------------------------------- 1 | /* 2 | * 485. Max Consecutive Ones 3 | * 4 | * Given a binary array nums, return the maximum number of consecutive 1's in the array. 5 | * 6 | * 7 | * Example 1: 8 | * 9 | * Input: nums = [1,1,0,1,1,1] 10 | * Output: 3 11 | * Explanation: The first two digits or the last three digits are consecutive 1s. 12 | * The maximum number of consecutive 1s is 3. 13 | * 14 | * 15 | * Example 2: 16 | * 17 | * Input: nums = [1,0,1,1,0,1] 18 | * Output: 2 19 | * 20 | * 21 | * Constraints: 22 | * 23 | * 1 <= nums.length <= 10^5 24 | * nums[i] is either 0 or 1. 25 | */ 26 | 27 | import 'dart:math' as math; 28 | 29 | class Solution { 30 | int findMaxConsecutiveOnes(List nums) { 31 | var max = 0, cur = 0; 32 | for (var i = 0; i < nums.length; i++) { 33 | if (nums[i] == 1) { 34 | cur++; 35 | continue; 36 | } 37 | max = math.max(max, cur); 38 | cur = 0; 39 | } 40 | return math.max(max, cur); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /lib/src/problem/234.palindrome_linked_list.dart: -------------------------------------------------------------------------------- 1 | /* 2 | * 234. Palindrome Linked List 3 | * 4 | * Given the head of a singly linked list, 5 | * return true if it is a palindrome or false otherwise. 6 | * 7 | * 8 | * Example 1: 9 | * 10 | * 11 | * Input: head = [1,2,2,1] 12 | * Output: true 13 | * 14 | * 15 | * Example 2: 16 | * 17 | * 18 | * Input: head = [1,2] 19 | * Output: false 20 | * 21 | * 22 | * Constraints: 23 | * 24 | * The number of nodes in the list is in the range [1, 10^5]. 25 | * 0 <= Node.val <= 9 26 | * 27 | * 28 | * Follow up: Could you do it in O(n) time and O(1) space? 29 | */ 30 | 31 | import '../structure/list_node.dart'; 32 | 33 | class Solution { 34 | bool isPalindrome(ListNode? head) { 35 | final buffer = []; 36 | for (var cursor = head; cursor != null; cursor = cursor.next) buffer.add(cursor.val.toString()); 37 | for (var i = 0; i < buffer.length ~/ 2; i++) if (buffer[i] != buffer[buffer.length - i - 1]) return false; 38 | return true; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright © 2022 Plague Fox 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /test/problem/27.remove_element.test.dart: -------------------------------------------------------------------------------- 1 | import 'package:leetcode/src/problem/27.remove_element.dart'; 2 | import 'package:test/test.dart'; 3 | 4 | void main() => group('remove_element', () { 5 | final f = Solution().removeElement; 6 | 7 | test('f([3,2,2,3], 3)', () { 8 | final list = [3, 2, 2, 3]; 9 | final result = f(list, 3); 10 | expect(list, isNot(contains(3))); 11 | expect(list, containsAll([2, 2])); 12 | expect(result, equals(2)); 13 | }); 14 | 15 | test('f([0,1,2,2,3,0,4,2], 2)', () { 16 | final list = [0, 1, 2, 2, 3, 0, 4, 2]; 17 | final result = f(list, 2); 18 | expect(list, isNot(contains(2))); 19 | expect(list, containsAll([0, 1, 4, 0, 3])); 20 | expect(result, equals(5)); 21 | }); 22 | 23 | test('f([3, 3], 3)', () { 24 | final list = [3, 3]; 25 | final result = f(list, 3); 26 | expect(list, isNot(contains(3))); 27 | expect(result, equals(0)); 28 | }); 29 | }); 30 | -------------------------------------------------------------------------------- /lib/src/problem/283.move_zeroes.dart: -------------------------------------------------------------------------------- 1 | /* 2 | * 283. Move Zeroes 3 | * 4 | * Given an integer array nums, move all 0's to the end of it 5 | * while maintaining the relative order of the non-zero elements. 6 | * 7 | * Note that you must do this in-place without making a copy of the array. 8 | * 9 | * 10 | * Example 1: 11 | * 12 | * Input: nums = [0,1,0,3,12] 13 | * Output: [1,3,12,0,0] 14 | * 15 | * 16 | * Example 2: 17 | * 18 | * Input: nums = [0] 19 | * Output: [0] 20 | * 21 | * 22 | * Constraints: 23 | * 24 | * 1 <= nums.length <= 10^4 25 | * -2^31 <= nums[i] <= 2^31 - 1 26 | */ 27 | 28 | class Solution { 29 | void moveZeroes(List nums) { 30 | if (nums.length < 2) return; 31 | 32 | void swap(int x, int y) { 33 | if (x == y) return; 34 | nums[x] = nums[x] ^ nums[y]; 35 | nums[y] = nums[x] ^ nums[y]; 36 | nums[x] = nums[x] ^ nums[y]; 37 | } 38 | 39 | var p = 0; 40 | for (var i = 0; i < nums.length; i++) { 41 | if (nums[i] == 0) continue; 42 | swap(i, p); 43 | p++; 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /test/problem/941.valid_mountain_array.test.dart: -------------------------------------------------------------------------------- 1 | import 'package:leetcode/src/problem/941.valid_mountain_array.dart'; 2 | import 'package:test/test.dart'; 3 | 4 | void main() => group('valid_mountain_array', () { 5 | final f = Solution().validMountainArray; 6 | 7 | test('f([])', () { 8 | expect(f([]), isFalse); 9 | }); 10 | 11 | test('f([0])', () { 12 | expect(f([0]), isFalse); 13 | }); 14 | 15 | test('f([2,1])', () { 16 | expect(f([2, 1]), isFalse); 17 | }); 18 | 19 | test('f([2,3,1])', () { 20 | expect(f([2, 3, 1]), isTrue); 21 | }); 22 | 23 | test('f([3,5,5])', () { 24 | expect(f([3, 5, 5]), isFalse); 25 | }); 26 | 27 | test('f([0,3,2,1])', () { 28 | expect(f([0, 3, 2, 1]), isTrue); 29 | }); 30 | 31 | test('f([0,1,2,3,4,5,6,7,8,9])', () { 32 | expect(f([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), isFalse); 33 | }); 34 | 35 | test('f([9,8,7,6,5,4,3,2,1,0])', () { 36 | expect(f([9, 8, 7, 6, 5, 4, 3, 2, 1, 0]), isFalse); 37 | }); 38 | }); 39 | -------------------------------------------------------------------------------- /test/problem/250.count_univalue_subtrees.test.dart: -------------------------------------------------------------------------------- 1 | import 'package:leetcode/src/problem/250.count_univalue_subtrees.dart'; 2 | import 'package:leetcode/src/structure/tree_node.dart'; 3 | import 'package:test/test.dart'; 4 | 5 | void main() => group('count_univalue_subtrees', () { 6 | final countUnivalSubtrees = Solution().countUnivalSubtrees; 7 | int f(List list) => countUnivalSubtrees(TreeNode.of(list)); 8 | 9 | test('f([5,1,5,5,5,null,5])', () { 10 | expect(f([5, 1, 5, 5, 5, null, 5]), equals(4)); 11 | }); 12 | 13 | test('f([])', () { 14 | expect(f([]), equals(0)); 15 | }); 16 | 17 | test('f([5,5,5,5,5,null,5])', () { 18 | expect(f([5, 5, 5, 5, 5, null, 5]), equals(6)); 19 | }); 20 | 21 | test('f([0])', () { 22 | expect(f([0]), equals(1)); 23 | }); 24 | 25 | test('f([1,2,2])', () { 26 | expect(f([1, 2, 2]), equals(2)); 27 | }); 28 | 29 | test('f([5,null,4,null,5,5,5,null,null,5])', () { 30 | expect(f([5, null, 4, null, 5, 5, 5, null, null, 5]), equals(4)); 31 | }); 32 | }); 33 | -------------------------------------------------------------------------------- /lib/src/problem/203.remove_linked_list_elements.dart: -------------------------------------------------------------------------------- 1 | /* 2 | * 203. Remove Linked List Elements 3 | * 4 | * Given the head of a linked list and an integer val, 5 | * remove all the nodes of the linked list that has Node.val == val, 6 | * and return the new head. 7 | * 8 | * Example 1: 9 | * 10 | * Input: head = [1,2,6,3,4,5,6], val = 6 11 | * Output: [1,2,3,4,5] 12 | * 13 | * 14 | * Example 2: 15 | * 16 | * Input: head = [], val = 1 17 | * Output: [] 18 | * 19 | * 20 | * Example 3: 21 | * 22 | * Input: head = [7,7,7,7], val = 7 23 | * Output: [] 24 | * 25 | * 26 | * Constraints: 27 | * 28 | * The number of nodes in the list is in the range [0, 10^4]. 29 | * 1 <= Node.val <= 50 30 | * 0 <= val <= 50 31 | */ 32 | 33 | import '../structure/list_node.dart'; 34 | 35 | class Solution { 36 | ListNode? removeElements(ListNode? head, int val) { 37 | ListNode? newHead, last; 38 | while (head != null) { 39 | if (head.val != val) last == null ? newHead = last = head : last = last.next = head; 40 | head = head.next; 41 | } 42 | last?.next = null; 43 | return newHead; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /test/problem/61.rotate_list.test.dart: -------------------------------------------------------------------------------- 1 | import 'package:leetcode/src/problem/61.rotate_list.dart'; 2 | import 'package:leetcode/src/structure/list_node.dart'; 3 | import 'package:test/test.dart'; 4 | 5 | void main() => group('rotate_list', () { 6 | final f = Solution().rotateRight; 7 | 8 | test('returnsNormally', () { 9 | expect(() => f(ListNode.of([1, 2, 3, 4, 5]), 2), returnsNormally); 10 | }); 11 | 12 | test('f(null, 2)', () { 13 | expect( 14 | f(null, 2)?.toList(), 15 | isNull, 16 | ); 17 | }); 18 | 19 | test('f([1,2,3], 0)', () { 20 | expect( 21 | f(ListNode.of([1, 2, 3]), 0)?.toList(), 22 | equals([1, 2, 3]), 23 | ); 24 | }); 25 | 26 | test('f([1,2,3,4,5], 2)', () { 27 | expect( 28 | f(ListNode.of([1, 2, 3, 4, 5]), 2)?.toList(), 29 | equals([4, 5, 1, 2, 3]), 30 | ); 31 | }); 32 | 33 | test('f([0,1,2], 4)', () { 34 | expect( 35 | f(ListNode.of([0, 1, 2]), 4)?.toList(), 36 | equals([2, 0, 1]), 37 | ); 38 | }); 39 | }); 40 | -------------------------------------------------------------------------------- /lib/src/problem/1480.running_sum_of_1d_array.dart: -------------------------------------------------------------------------------- 1 | /* 2 | * 1480. Running Sum of 1d Array 3 | * 4 | * Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]). 5 | * 6 | * Return the running sum of nums. 7 | * 8 | * 9 | * Example 1: 10 | * 11 | * Input: nums = [1,2,3,4] 12 | * Output: [1,3,6,10] 13 | * Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4]. 14 | * 15 | * 16 | * Example 2: 17 | * 18 | * Input: nums = [1,1,1,1,1] 19 | * Output: [1,2,3,4,5] 20 | * Explanation: Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1]. 21 | * 22 | * 23 | * Example 3: 24 | * 25 | * Input: nums = [3,1,2,10,1] 26 | * Output: [3,4,6,16,17] 27 | * 28 | * 29 | * Constraints: 30 | * 31 | * 1 <= nums.length <= 1000 32 | * -10^6 <= nums[i] <= 10^6 33 | */ 34 | 35 | class Solution { 36 | List runningSum(List nums) => List.of( 37 | () sync* { 38 | var prev = 0; 39 | for (var i = 0; i < nums.length; i++) { 40 | yield prev += nums[i]; 41 | } 42 | }(), 43 | ); 44 | } 45 | -------------------------------------------------------------------------------- /lib/src/problem/61.rotate_list.dart: -------------------------------------------------------------------------------- 1 | /* 2 | * 61. Rotate List 3 | * 4 | * Given the head of a linked list, rotate the list to the right by k places. 5 | * 6 | * 7 | * Example 1: 8 | * 9 | * Input: head = [1,2,3,4,5], k = 2 10 | * Output: [4,5,1,2,3] 11 | * 12 | * 13 | * Example 2: 14 | * 15 | * Input: head = [0,1,2], k = 4 16 | * Output: [2,0,1] 17 | * 18 | * Constraints: 19 | * 20 | * The number of nodes in the list is in the range [0, 500]. 21 | * -100 <= Node.val <= 100 22 | * 0 <= k <= 2 * 10^9 23 | */ 24 | 25 | import '../structure/list_node.dart'; 26 | 27 | /// Implementation via recursion, 28 | /// but be aware that this threatens to overflow the call stack. 29 | class Solution { 30 | ListNode? rotateRight(ListNode? head, int k) { 31 | if (head == null || k < 1) return head; 32 | 33 | var length = 0; 34 | void rotate(ListNode node) { 35 | length++; 36 | node.next != null ? rotate(node.next!) : k %= length; 37 | if (k > 0) head = node..next = head; 38 | if (k == 0) node.next = null; 39 | k--; 40 | } 41 | 42 | rotate(head!); 43 | return head; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /lib/src/problem/876.middle_of_the_linked_list.dart: -------------------------------------------------------------------------------- 1 | /* 2 | * 876. Middle of the Linked List 3 | * 4 | * Given the head of a singly linked list, return the middle node of the linked list. 5 | * 6 | * If there are two middle nodes, return the second middle node. 7 | * 8 | * 9 | * Example 1: 10 | * 11 | * Input: head = [1,2,3,4,5] 12 | * Output: [3,4,5] 13 | * Explanation: The middle node of the list is node 3. 14 | * 15 | * 16 | * Example 2: 17 | * 18 | * Input: head = [1,2,3,4,5,6] 19 | * Output: [4,5,6] 20 | * Explanation: Since the list has two middle nodes with values 3 and 4, we return the second one. 21 | * 22 | * 23 | * Constraints: 24 | * 25 | * The number of nodes in the list is in the range [1, 100]. 26 | * 1 <= Node.val <= 100 27 | */ 28 | 29 | import '../structure/list_node.dart'; 30 | 31 | class Solution { 32 | ListNode? middleNode(ListNode? head) { 33 | if (head == null) return null; 34 | ListNode? slow = head, fast = head; 35 | while (fast != null && fast.next != null) { 36 | slow = slow?.next; 37 | fast = fast.next?.next; 38 | } 39 | return slow; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /test/structure/list_node.test.dart: -------------------------------------------------------------------------------- 1 | import 'package:leetcode/src/structure/list_node.dart'; 2 | import 'package:test/test.dart'; 3 | 4 | void main() => group('list_node', () { 5 | test('create', () { 6 | expect(() => ListNode(1), returnsNormally); 7 | expect(ListNode(1), isA()); 8 | expect(ListNode(1).val, equals(1)); 9 | }); 10 | 11 | test('from_empty_collection', () { 12 | expect(() => ListNode.of([]), returnsNormally); 13 | expect(ListNode.of([]), isNull); 14 | }); 15 | 16 | test('from_collection', () { 17 | expect(() => ListNode.of([1, 2, 3]), returnsNormally); 18 | expect(ListNode.of([1, 2, 3]), equals([1, 2, 3])); 19 | }); 20 | 21 | test('toJson', () { 22 | expect(() => ListNode.of([1, 2, 3])?.toJson(), returnsNormally); 23 | expect(ListNode.of([1, 2, 3])?.toJson(), equals([1, 2, 3])); 24 | }); 25 | 26 | test('toString', () { 27 | expect(() => ListNode.of([1, 2, 3]).toString(), returnsNormally); 28 | expect(ListNode.of([1, 2, 3]).toString(), equals('(1, 2, 3)')); 29 | }); 30 | }); 31 | -------------------------------------------------------------------------------- /test/problem/94.binary_tree_inorder_traversal.test.dart: -------------------------------------------------------------------------------- 1 | import 'package:leetcode/src/problem/94.binary_tree_inorder_traversal.dart'; 2 | import 'package:leetcode/src/structure/tree_node.dart'; 3 | import 'package:test/test.dart'; 4 | 5 | void main() => group('binary_tree_inorder_traversal', () { 6 | final f1 = SolutionV1().inorderTraversal; 7 | final f2 = SolutionV2().inorderTraversal; 8 | final f3 = SolutionV3().inorderTraversal; 9 | 10 | test('f([1,null,2,3])', () { 11 | expect(f1(TreeNode.of([1, null, 2, 3])), equals([1, 3, 2])); 12 | expect(f2(TreeNode.of([1, null, 2, 3])), equals([1, 3, 2])); 13 | expect(f3(TreeNode.of([1, null, 2, 3])), equals([1, 3, 2])); 14 | }); 15 | 16 | test('f([])', () { 17 | expect(f1(TreeNode.of([])), isEmpty); 18 | expect(f2(TreeNode.of([])), isEmpty); 19 | expect(f3(TreeNode.of([])), isEmpty); 20 | }); 21 | 22 | test('f([1])', () { 23 | expect(f1(TreeNode.of([1])), equals([1])); 24 | expect(f2(TreeNode.of([1])), equals([1])); 25 | expect(f3(TreeNode.of([1])), equals([1])); 26 | }); 27 | }); 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LeetCode Dart solutions. 2 | 3 | [![Pipeline](https://github.com/PlugFox/leetcode/actions/workflows/checkout.yml/badge.svg?branch=master)](https://github.com/PlugFox/leetcode/actions/workflows/checkout.yml) 4 | [![Coverage](https://codecov.io/gh/PlugFox/leetcode/branch/master/graph/badge.svg?token=5yrpQh4j5M)](https://codecov.io/gh/PlugFox/leetcode) 5 | [![Code size](https://img.shields.io/github/languages/code-size/plugfox/leetcode?logo=github&logoColor=white)](https://github.com/plugfox/leetcode) 6 | [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) 7 | 8 | In this repository, I post solutions to LeetCode problems on Dart. 9 | The repository also contains the [structures](./lib/src/structure) necessary for solving problems on LeetCode. 10 | 11 | - [LeetCode](https://leetcode.com/) 12 | - [Explore](https://leetcode.com/explore/) 13 | - [Problems](https://leetcode.com/problemset/all/) 14 | - [Profile](https://leetcode.com/PlugFox/) 15 | 16 | ## Coverage 17 | 18 | [![](https://codecov.io/gh/PlugFox/leetcode/branch/master/graphs/sunburst.svg)](https://codecov.io/gh/PlugFox/leetcode/branch/master) 19 | -------------------------------------------------------------------------------- /lib/src/problem/19.remove_nth_node_from_end_of_list.dart: -------------------------------------------------------------------------------- 1 | /* 2 | * 19. Remove Nth Node From End of List 3 | * 4 | * Given the head of a linked list, 5 | * remove the nth node from the end of the list and return its head. 6 | * 7 | * 8 | * Example 1: 9 | * 10 | * Input: head = [1,2,3,4,5], n = 2 11 | * Output: [1,2,3,5] 12 | * 13 | * 14 | * Example 2: 15 | * 16 | * Input: head = [1], n = 1 17 | * Output: [] 18 | * 19 | * 20 | * Example 3: 21 | * 22 | * Input: head = [1,2], n = 1 23 | * Output: [1] 24 | * 25 | * 26 | * Constraints: 27 | * 28 | * The number of nodes in the list is sz. 29 | * 1 <= sz <= 30 30 | * 0 <= Node.val <= 100 31 | * 1 <= n <= sz 32 | * 33 | * 34 | * Follow up: Could you do this in one pass? 35 | */ 36 | 37 | import '../structure/list_node.dart'; 38 | 39 | class Solution { 40 | ListNode? removeNthFromEnd(ListNode? head, int n) { 41 | var cur = head, pos = head; 42 | while (cur != null) { 43 | n < 0 ? pos = pos?.next : n--; 44 | cur = cur.next; 45 | } 46 | if (n >= 0) return n > 0 ? null : head?.next; 47 | pos?.next = pos.next?.next; 48 | return head; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /lib/src/problem/250.count_univalue_subtrees.dart: -------------------------------------------------------------------------------- 1 | /* 2 | * 250. Count Univalue Subtrees 3 | * 4 | * Given the root of a binary tree, return the number of uni-value subtrees. 5 | * 6 | * A uni-value subtree means all nodes of the subtree have the same value. 7 | * 8 | * 9 | * Example 1: 10 | * 11 | * Input: root = [5,1,5,5,5,null,5] 12 | * Output: 4 13 | * 14 | * 15 | * Example 2: 16 | * 17 | * Input: root = [] 18 | * Output: 0 19 | * 20 | * 21 | * Example 3: 22 | * 23 | * Input: root = [5,5,5,5,5,null,5] 24 | * Output: 6 25 | * 26 | * 27 | * Constraints: 28 | * 29 | * The number of the node in the tree will be in the range [0, 1000]. 30 | * -1000 <= Node.val <= 1000 31 | */ 32 | 33 | import '../structure/tree_node.dart'; 34 | 35 | class Solution { 36 | int countUnivalSubtrees(TreeNode? root) { 37 | if (root == null) return 0; 38 | var count = 0; 39 | int? visit(TreeNode node) { 40 | if ((node.left != null && visit(node.left!) != node.val) | (node.right != null && visit(node.right!) != node.val)) 41 | return null; 42 | count++; 43 | return node.val; 44 | } 45 | 46 | visit(root); 47 | return count; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /lib/src/problem/905.sort_array_by_parity.dart: -------------------------------------------------------------------------------- 1 | /* 2 | * 905. Sort Array By Parity 3 | * 4 | * Given an integer array nums, move all the even integers 5 | * at the beginning of the array followed by all the odd integers. 6 | * 7 | * Return any array that satisfies this condition. 8 | * 9 | * 10 | * Example 1: 11 | * 12 | * Input: nums = [3,1,2,4] 13 | * Output: [2,4,3,1] 14 | * Explanation: The outputs [4,2,3,1], [2,4,1,3], 15 | * and [4,2,1,3] would also be accepted. 16 | * 17 | * 18 | * Example 2: 19 | * 20 | * Input: nums = [0] 21 | * Output: [0] 22 | * 23 | * 24 | * Constraints: 25 | * 26 | * 1 <= nums.length <= 5000 27 | * 0 <= nums[i] <= 5000 28 | */ 29 | 30 | class Solution { 31 | List sortArrayByParity(List nums) { 32 | if (nums.length < 2) return nums; 33 | 34 | void swap(int x, int y) { 35 | if (x == y) return; 36 | nums[x] = nums[x] ^ nums[y]; 37 | nums[y] = nums[x] ^ nums[y]; 38 | nums[x] = nums[x] ^ nums[y]; 39 | } 40 | 41 | var p = 0; 42 | for (var i = 0; i < nums.length; i++) { 43 | if (nums[i].isOdd) continue; 44 | swap(i, p); 45 | p++; 46 | } 47 | 48 | return nums; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /lib/src/problem/412.fizz_buzz.dart: -------------------------------------------------------------------------------- 1 | /* 2 | * 412. Fizz Buzz 3 | * 4 | * Given an integer n, return a string array answer (1-indexed) where: 5 | * 6 | * answer[i] == "FizzBuzz" if i is divisible by 3 and 5. 7 | * answer[i] == "Fizz" if i is divisible by 3. 8 | * answer[i] == "Buzz" if i is divisible by 5. 9 | * answer[i] == i (as a string) if none of the above conditions are true. 10 | * 11 | * 12 | * Example 1: 13 | * 14 | * Input: n = 3 15 | * Output: ["1","2","Fizz"] 16 | * 17 | * 18 | * Example 2: 19 | * 20 | * Input: n = 5 21 | * Output: ["1","2","Fizz","4","Buzz"] 22 | * 23 | * 24 | * Example 3: 25 | * 26 | * Input: n = 15 27 | * Output: ["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz"] 28 | * 29 | * 30 | * Constraints: 31 | * 32 | * 1 <= n <= 10^4 33 | */ 34 | 35 | class Solution { 36 | List fizzBuzz(int n) => List.generate(n, (idx) { 37 | final x = idx + 1; 38 | final buffer = StringBuffer(); 39 | if (x % 3 == 0) buffer.write('Fizz'); 40 | if (x % 5 == 0) buffer.write('Buzz'); 41 | if (buffer.isEmpty) buffer.write(x); 42 | return buffer.toString(); 43 | }); 44 | } 45 | -------------------------------------------------------------------------------- /lib/src/problem/112.path_sum.dart: -------------------------------------------------------------------------------- 1 | /* 2 | * 112. Path Sum 3 | * 4 | * Given the root of a binary tree and an integer targetSum, 5 | * return true if the tree has a root-to-leaf path such 6 | * that adding up all the values along the path equals targetSum. 7 | * 8 | * A leaf is a node with no children. 9 | * 10 | * 11 | * Example 1: 12 | * 13 | * Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22 14 | * Output: true 15 | * 16 | * 17 | * Example 2: 18 | * 19 | * Input: root = [1,2,3], targetSum = 5 20 | * Output: false 21 | * 22 | * 23 | * Example 3: 24 | * 25 | * Input: root = [], targetSum = 0 26 | * Output: false 27 | * 28 | * 29 | * Constraints: 30 | * 31 | * The number of nodes in the tree is in the range [0, 5000]. 32 | * -1000 <= Node.val <= 1000 33 | * -1000 <= targetSum <= 1000 34 | */ 35 | 36 | import '../structure/tree_node.dart'; 37 | 38 | class Solution { 39 | bool hasPathSum(TreeNode? root, int targetSum) { 40 | if (root == null) return false; 41 | targetSum -= root.val; 42 | if (root.left == null && root.right == null) return targetSum == 0; 43 | return hasPathSum(root.left, targetSum) || hasPathSum(root.right, targetSum); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /test/problem/101.symmetric_tree.test.dart: -------------------------------------------------------------------------------- 1 | import 'package:leetcode/src/problem/101.symmetric_tree.dart'; 2 | import 'package:leetcode/src/structure/tree_node.dart'; 3 | import 'package:test/test.dart'; 4 | 5 | void main() => group('binary_tree_level_order_traversal', () { 6 | final f1 = SolutionV1().isSymmetric; 7 | final f2 = SolutionV2().isSymmetric; 8 | 9 | test('f([])', () { 10 | expect(f1(TreeNode.of([])), isTrue); 11 | expect(f2(TreeNode.of([])), isTrue); 12 | }); 13 | 14 | test('f([1])', () { 15 | expect(f1(TreeNode.of([1])), isTrue); 16 | expect(f2(TreeNode.of([1])), isTrue); 17 | }); 18 | 19 | test('f([1, 2])', () { 20 | expect(f1(TreeNode.of([1, 2])), isFalse); 21 | expect(f2(TreeNode.of([1, 2])), isFalse); 22 | }); 23 | 24 | test('f([1,2,2,3,4,4,3])', () { 25 | expect(f1(TreeNode.of([1, 2, 2, 3, 4, 4, 3])), isTrue); 26 | expect(f2(TreeNode.of([1, 2, 2, 3, 4, 4, 3])), isTrue); 27 | }); 28 | 29 | test('f([1,2,2,null,3,null,3])', () { 30 | expect(f1(TreeNode.of([1, 2, 2, null, 3, null, 3])), isFalse); 31 | expect(f2(TreeNode.of([1, 2, 2, null, 3, null, 3])), isFalse); 32 | }); 33 | }); 34 | -------------------------------------------------------------------------------- /lib/src/problem/941.valid_mountain_array.dart: -------------------------------------------------------------------------------- 1 | /* 2 | * 941. Valid Mountain Array 3 | * 4 | * Given an array of integers arr, return true if and only if it is a valid mountain array. 5 | * 6 | * Recall that arr is a mountain array if and only if: 7 | * 8 | * arr.length >= 3 9 | * There exists some i with 0 < i < arr.length - 1 such that: 10 | * arr[0] < arr[1] < ... < arr[i - 1] < arr[i] 11 | * arr[i] > arr[i + 1] > ... > arr[arr.length - 1] 12 | * 13 | * 14 | * Example 1: 15 | * 16 | * Input: arr = [2,1] 17 | * Output: false 18 | * 19 | * 20 | * Example 2: 21 | * 22 | * Input: arr = [3,5,5] 23 | * Output: false 24 | * 25 | * 26 | * Example 3: 27 | * 28 | * Input: arr = [0,3,2,1] 29 | * Output: true 30 | * 31 | * 32 | * Constraints: 33 | * 34 | * 1 <= arr.length <= 10^4 35 | * 0 <= arr[i] <= 10^4 36 | */ 37 | 38 | class Solution { 39 | bool validMountainArray(List arr) { 40 | if (arr.length < 3 || arr[0] >= arr[1]) return false; 41 | var inc = true; 42 | for (var i = 2; i < arr.length; i++) { 43 | if (inc) { 44 | if (arr[i] > arr[i - 1]) continue; 45 | inc = false; 46 | } 47 | if (arr[i] < arr[i - 1]) continue; 48 | return false; 49 | } 50 | return !inc; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /lib/src/problem/102.binary_tree_level_order_traversal.dart: -------------------------------------------------------------------------------- 1 | /* 2 | * 102. Binary Tree Level Order Traversal 3 | * 4 | * Given the root of a binary tree, 5 | * return the level order traversal of its nodes' values. 6 | * (i.e., from left to right, level by level). 7 | * 8 | * 9 | * Example 1: 10 | * 11 | * Input: root = [3,9,20,null,null,15,7] 12 | * Output: [[3],[9,20],[15,7]] 13 | * 14 | * 15 | * Example 2: 16 | * 17 | * Input: root = [1] 18 | * Output: [[1]] 19 | * 20 | * 21 | * Example 3: 22 | * 23 | * Input: root = [] 24 | * Output: [] 25 | * 26 | * 27 | * Constraints: 28 | * 29 | * The number of nodes in the tree is in the range [0, 2000]. 30 | * -1000 <= Node.val <= 1000 31 | */ 32 | 33 | import '../structure/tree_node.dart'; 34 | 35 | class Solution { 36 | List> levelOrder(TreeNode? root) { 37 | Iterable> traversal() sync* { 38 | var queue = [if (root != null) root]; 39 | for (var length = queue.length; length != 0; length = queue.length) { 40 | yield List.generate(length, (i) => queue[i].val); 41 | queue = queue.expand((e) => [e.left, e.right]).whereType().toList(growable: false); 42 | } 43 | } 44 | 45 | return traversal().toList(); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /lib/src/problem/1089.duplicate_zeros.dart: -------------------------------------------------------------------------------- 1 | /* 2 | * 1089. Duplicate Zeros 3 | * 4 | * Given a fixed-length integer array arr, duplicate each occurrence of zero, 5 | * shifting the remaining elements to the right. 6 | * 7 | * Note that elements beyond the length of the original array are not written. 8 | * Do the above modifications to the input array in place and do not return anything. 9 | * 10 | * 11 | * Example 1: 12 | * 13 | * Input: arr = [1,0,2,3,0,4,5,0] 14 | * Output: [1,0,0,2,3,0,0,4] 15 | * Explanation: After calling your function, the input array is modified to: [1,0,0,2,3,0,0,4] 16 | * 17 | * 18 | * Example 2: 19 | * 20 | * Input: arr = [1,2,3] 21 | * Output: [1,2,3] 22 | * Explanation: After calling your function, the input array is modified to: [1,2,3] 23 | * 24 | * 25 | * Constraints: 26 | * 27 | * 1 <= arr.length <= 10^4 28 | * 0 <= arr[i] <= 9 29 | */ 30 | 31 | class Solution { 32 | void duplicateZeros(List arr) { 33 | void shift(int pos) { 34 | assert(pos > 0 && pos < arr.length, 'Position is out of range'); 35 | for (var i = arr.length - 1; i > pos; i--) { 36 | arr[i] = arr[i - 1]; 37 | } 38 | } 39 | 40 | for (var i = 0; i < arr.length - 1; i++) { 41 | if (arr[i] != 0) continue; 42 | shift(i + 1); 43 | arr[i + 1] = 0; 44 | i++; 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /test/problem/88.merge_sorted_array.test.dart: -------------------------------------------------------------------------------- 1 | import 'package:leetcode/src/problem/88.merge_sorted_array.dart'; 2 | import 'package:test/test.dart'; 3 | 4 | void main() => group('merge_sorted_array', () { 5 | final f1 = SolutionV1().merge; 6 | final f2 = SolutionV2().merge; 7 | 8 | test('f([1,2,3,0,0,0], 3, [2,5,6], 3)', () { 9 | { 10 | final list = [1, 2, 3, 0, 0, 0]; 11 | f1(list, 3, [2, 5, 6], 3); 12 | expect(list, equals([1, 2, 2, 3, 5, 6])); 13 | } 14 | { 15 | final list = [1, 2, 3, 0, 0, 0]; 16 | f2(list, 3, [2, 5, 6], 3); 17 | expect(list, equals([1, 2, 2, 3, 5, 6])); 18 | } 19 | }); 20 | 21 | test('f([1], 1, [], 0)', () { 22 | { 23 | final list = [1]; 24 | f1(list, 1, [], 0); 25 | expect(list, equals([1])); 26 | } 27 | { 28 | final list = [1]; 29 | f2(list, 1, [], 0); 30 | expect(list, equals([1])); 31 | } 32 | }); 33 | 34 | test('f([0], 0, [1], 1)', () { 35 | { 36 | final list = [0]; 37 | f1(list, 0, [1], 1); 38 | expect(list, equals([1])); 39 | } 40 | { 41 | final list = [0]; 42 | f2(list, 0, [1], 1); 43 | expect(list, equals([1])); 44 | } 45 | }); 46 | }); 47 | -------------------------------------------------------------------------------- /lib/src/problem/448.find_all_numbers_disappeared_in_an_array.dart: -------------------------------------------------------------------------------- 1 | /* 2 | * 448. Find All Numbers Disappeared in an Array 3 | * 4 | * Given an array nums of n integers where nums[i] is in the range [1, n], 5 | * return an array of all the integers in the range [1, n] that do not appear in nums. 6 | * 7 | * 8 | * Example 1: 9 | * 10 | * Input: nums = [4,3,2,7,8,2,3,1] 11 | * Output: [5,6] 12 | * 13 | * 14 | * Example 2: 15 | * 16 | * Input: nums = [1,1] 17 | * Output: [2] 18 | * 19 | * 20 | * Constraints: 21 | * 22 | * n == nums.length 23 | * 1 <= n <= 10^5 24 | * 1 <= nums[i] <= n 25 | * 26 | * 27 | * Follow up: Could you do it without extra space and in O(n) runtime? 28 | * You may assume the returned list does not count as extra space. 29 | */ 30 | 31 | class Solution { 32 | List findDisappearedNumbers(List nums) { 33 | assert(nums.isNotEmpty, 'Array should contains numbers'); 34 | for (var i = 0, e = 0; i < nums.length; i++) { 35 | e = nums[i].abs(); 36 | assert(e > 0, 'Number in array should be greater then 0'); 37 | assert(e <= nums.length, 'Number in array should be lower or equal then ${nums.length}'); 38 | nums[e - 1] = -nums[e - 1].abs(); 39 | } 40 | final result = []; 41 | for (var i = 0; i < nums.length; i++) nums[i] < 0 ? nums[i] = nums[i].abs() : result.add(i + 1); 42 | return result; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /lib/src/problem/1342.number_of_steps_to_reduce_a_number_to_zero.dart: -------------------------------------------------------------------------------- 1 | /* 2 | * 1342. Number of Steps to Reduce a Number to Zero 3 | * 4 | * Given an integer num, return the number of steps to reduce it to zero. 5 | * 6 | * In one step, if the current number is even, you have to divide it by 2, 7 | * otherwise, you have to subtract 1 from it. 8 | * 9 | * 10 | * Example 1: 11 | * 12 | * Input: num = 14 13 | * Output: 6 14 | * Explanation: 15 | * Step 1) 14 is even; divide by 2 and obtain 7. 16 | * Step 2) 7 is odd; subtract 1 and obtain 6. 17 | * Step 3) 6 is even; divide by 2 and obtain 3. 18 | * Step 4) 3 is odd; subtract 1 and obtain 2. 19 | * Step 5) 2 is even; divide by 2 and obtain 1. 20 | * Step 6) 1 is odd; subtract 1 and obtain 0. 21 | * 22 | * 23 | * Example 2: 24 | * 25 | * Input: num = 8 26 | * Output: 4 27 | * Explanation: 28 | * Step 1) 8 is even; divide by 2 and obtain 4. 29 | * Step 2) 4 is even; divide by 2 and obtain 2. 30 | * Step 3) 2 is even; divide by 2 and obtain 1. 31 | * Step 4) 1 is odd; subtract 1 and obtain 0. 32 | * 33 | * 34 | * Example 3: 35 | * 36 | * Input: num = 123 37 | * Output: 12 38 | * 39 | * 40 | * Constraints: 41 | * 42 | * 0 <= num <= 10^6 43 | */ 44 | 45 | class Solution { 46 | int numberOfSteps(int num) { 47 | var i = 0; 48 | for (; num != 0; i++) num.isEven ? num ~/= 2 : num--; 49 | return i; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /lib/src/problem/977.squares_of_a_sorted_array.dart: -------------------------------------------------------------------------------- 1 | /* 2 | * 977. Squares of a Sorted Array 3 | * 4 | * Given an integer array nums sorted in non-decreasing order, 5 | * return an array of the squares of each number sorted in non-decreasing order. 6 | * 7 | * 8 | * Example 1: 9 | * 10 | * Input: nums = [-4,-1,0,3,10] 11 | * Output: [0,1,9,16,100] 12 | * Explanation: After squaring, the array becomes [16,1,0,9,100]. 13 | * After sorting, it becomes [0,1,9,16,100]. 14 | * 15 | * 16 | * Example 2: 17 | * 18 | * Input: nums = [-7,-3,2,3,11] 19 | * Output: [4,9,9,49,121] 20 | * 21 | * 22 | * Constraints: 23 | * 24 | * 1 <= nums.length <= 10^4 25 | * -10^4 <= nums[i] <= 10^4 26 | * nums is sorted in non-decreasing order. 27 | * 28 | * 29 | * Follow up: Squaring each element and sorting the new array is very trivial, 30 | * could you find an O(n) solution using a different approach? 31 | */ 32 | 33 | class Solution { 34 | List sortedSquares(List nums) { 35 | final result = List.filled(nums.length, 0, growable: false); 36 | for (var left = 0, right = nums.length - 1, n = 0, i = right; i >= 0; i--) { 37 | if (nums[left].abs() < nums[right].abs()) { 38 | n = nums[right]; 39 | right--; 40 | } else { 41 | n = nums[left]; 42 | left++; 43 | } 44 | result[i] = n * n; 45 | } 46 | return result; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /lib/src/problem/9.palindrome_number.dart: -------------------------------------------------------------------------------- 1 | /* 2 | * 9. Palindrome Number 3 | * 4 | * Given an integer x, return true if x is palindrome integer. 5 | * 6 | * An integer is a palindrome when it reads the same backward as forward. 7 | * 8 | * For example, 121 is a palindrome while 123 is not. 9 | * 10 | * 11 | * Example 1: 12 | * 13 | * Input: x = 121 14 | * Output: true 15 | * Explanation: 121 reads as 121 from left to right and from right to left. 16 | * 17 | * 18 | * Example 2: 19 | * 20 | * Input: x = -121 21 | * Output: false 22 | * Explanation: From left to right, it reads -121. From right to left, 23 | * it becomes 121-. Therefore it is not a palindrome. 24 | * 25 | * 26 | * Example 3: 27 | * 28 | * Input: x = 10 29 | * Output: false 30 | * Explanation: Reads 01 from right to left. Therefore it is not a palindrome. 31 | * 32 | * 33 | * Constraints: 34 | * 35 | * -2^31 <= x <= 2^31 - 1 36 | * 37 | * 38 | * Follow up: Could you solve it without converting the integer to a string? 39 | */ 40 | 41 | class Solution { 42 | bool isPalindrome(int x) { 43 | if (x > -1 && x < 10) return true; 44 | if (x.isNegative || x % 10 == 0) return false; 45 | final chars = x.toString().codeUnits; 46 | final length = chars.length, middle = chars.length / 2; 47 | for (var i = 0; i < middle; i++) if (chars[i] != chars[length - i - 1]) return false; 48 | return true; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /lib/src/problem/487.max_consecutive_ones_ii.dart: -------------------------------------------------------------------------------- 1 | /* 2 | * 487. Max Consecutive Ones II 3 | * 4 | * Given a binary array nums, return the maximum number of consecutive 1's 5 | * in the array if you can flip at most one 0. 6 | * 7 | * 8 | * Example 1: 9 | * 10 | * Input: nums = [1,0,1,1,0] 11 | * Output: 4 12 | * Explanation: Flip the first zero will get the maximum number of consecutive 1s. 13 | * After flipping, the maximum number of consecutive 1s is 4. 14 | * 15 | * 16 | * Example 2: 17 | * 18 | * Input: nums = [1,0,1,1,0,1] 19 | * Output: 4 20 | * 21 | * 22 | * Constraints: 23 | * 24 | * 1 <= nums.length <= 10^5 25 | * nums[i] is either 0 or 1. 26 | * 27 | * 28 | * Follow up: What if the input numbers come in one by one as an infinite stream? 29 | * In other words, you can't store all numbers coming from the stream as 30 | * it's too large to hold in memory. Could you solve it efficiently? 31 | */ 32 | 33 | import 'dart:math' as math; 34 | 35 | class Solution { 36 | int findMaxConsecutiveOnes(List nums) { 37 | var result = 0, current = 0, next = 0; 38 | 39 | void flipZero() { 40 | result = math.max(result, current); 41 | current = next + 1; 42 | next = 0; 43 | } 44 | 45 | for (var i = 0; i < nums.length; i++) { 46 | if (nums[i] == 1) { 47 | current++; 48 | next++; 49 | continue; 50 | } 51 | flipZero(); 52 | } 53 | return math.max(result, current); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /lib/src/problem/1295.find_numbers_with_even_number_of_digits.dart: -------------------------------------------------------------------------------- 1 | /* 2 | * 1295. Find Numbers with Even Number of Digits 3 | * 4 | * Given an array nums of integers, return how many of them contain an even number of digits. 5 | * 6 | * 7 | * Example 1: 8 | * 9 | * Input: nums = [12,345,2,6,7896] 10 | * Output: 2 11 | * Explanation: 12 | * 12 contains 2 digits (even number of digits). 13 | * 345 contains 3 digits (odd number of digits). 14 | * 2 contains 1 digit (odd number of digits). 15 | * 6 contains 1 digit (odd number of digits). 16 | * 7896 contains 4 digits (even number of digits). 17 | * Therefore only 12 and 7896 contain an even number of digits. 18 | * 19 | * 20 | * Example 2: 21 | * 22 | * Input: nums = [555,901,482,1771] 23 | * Output: 1 24 | * Explanation: 25 | * Only 1771 contains an even number of digits. 26 | * 27 | * 28 | * Constraints: 29 | * 30 | * 1 <= nums.length <= 500 31 | * 1 <= nums[i] <= 105 32 | */ 33 | 34 | import 'dart:math' as math; 35 | 36 | class SolutionV1 { 37 | int findNumbers(List nums) { 38 | var count = 0; 39 | for (var i = 0; i < nums.length; i++) { 40 | var j = 0, n = nums[i]; 41 | do { 42 | j++; 43 | n ~/= 10; 44 | } while (n > 0); 45 | if (j.isOdd) continue; 46 | count++; 47 | } 48 | return count; 49 | } 50 | } 51 | 52 | class SolutionV2 { 53 | int findNumbers(List nums) => nums.where((n) => (math.log(n + 1) ~/ math.ln10 + 1) % 2 == 0).length; 54 | } 55 | -------------------------------------------------------------------------------- /lib/src/structure/list_node.dart: -------------------------------------------------------------------------------- 1 | class ListNode extends Iterable { 2 | ListNode(this.val, [this.next]); 3 | 4 | static ListNode? of(Iterable collection) { 5 | final iterator = collection.iterator; 6 | if (!iterator.moveNext()) return null; 7 | final head = ListNode(iterator.current); 8 | var node = head; 9 | while (iterator.moveNext()) { 10 | node = node.next = ListNode(iterator.current); 11 | } 12 | return head; 13 | } 14 | 15 | int val; 16 | ListNode? next; 17 | 18 | @override 19 | Iterator get iterator => _ListNodeIterator(this); 20 | 21 | List toJson() => toList(); 22 | 23 | @override 24 | String toString() { 25 | ListNode? node = this; 26 | final buffer = StringBuffer('(')..write(node.val); 27 | while (true) { 28 | node = node?.next; 29 | if (node == null) break; 30 | buffer 31 | ..write(', ') 32 | ..write(node.val); 33 | } 34 | return (buffer..write(')')).toString(); 35 | } 36 | } 37 | 38 | class _ListNodeIterator implements Iterator { 39 | _ListNodeIterator(ListNode node) : _node = node; 40 | 41 | bool _head = true; 42 | ListNode _node; 43 | 44 | @override 45 | int get current => _node.val; 46 | 47 | @override 48 | @pragma('vm:prefer-inline') 49 | bool moveNext() { 50 | final next = _head ? _node : _node.next; 51 | _head = false; 52 | if (next == null) { 53 | return false; 54 | } 55 | _node = next; 56 | return true; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /test/problem/1295.find_numbers_with_even_number_of_digits.test.dart: -------------------------------------------------------------------------------- 1 | import 'package:leetcode/src/problem/1295.find_numbers_with_even_number_of_digits.dart'; 2 | import 'package:test/test.dart'; 3 | 4 | void main() => group('find_numbers_with_even_number_of_digits', () { 5 | final f1 = SolutionV1().findNumbers; 6 | final f2 = SolutionV2().findNumbers; 7 | 8 | test('f([])', () { 9 | { 10 | expect(f1([]), equals(0)); 11 | } 12 | { 13 | expect(f2([]), equals(0)); 14 | } 15 | }); 16 | 17 | test('f([1000])', () { 18 | { 19 | expect(f1([1000]), equals(1)); 20 | } 21 | { 22 | expect(f2([1000]), equals(1)); 23 | } 24 | }); 25 | 26 | test('f([12,345,2,6,7896])', () { 27 | { 28 | expect(f1([12, 345, 2, 6, 7896]), equals(2)); 29 | } 30 | { 31 | expect(f2([12, 345, 2, 6, 7896]), equals(2)); 32 | } 33 | }); 34 | 35 | test('f([555,901,482,1771])', () { 36 | { 37 | expect(f1([555, 901, 482, 1771]), equals(1)); 38 | } 39 | { 40 | expect(f2([555, 901, 482, 1771]), equals(1)); 41 | } 42 | }); 43 | 44 | test('f([12,345,2,6,7896])', () { 45 | { 46 | expect(f1([12, 345, 2, 6, 7896]), equals(2)); 47 | } 48 | { 49 | expect(f2([12, 345, 2, 6, 7896]), equals(2)); 50 | } 51 | }); 52 | }); 53 | -------------------------------------------------------------------------------- /lib/src/problem/328.odd_even_linked_list.dart: -------------------------------------------------------------------------------- 1 | /* 2 | * 328. Odd Even Linked List 3 | * 4 | * Given the head of a singly linked list, 5 | * group all the nodes with odd indices together followed by the nodes 6 | * with even indices, and return the reordered list. 7 | * 8 | * The first node is considered odd, and the second node is even, and so on. 9 | * 10 | * Note that the relative order inside both the even and odd groups 11 | * should remain as it was in the input. 12 | * 13 | * You must solve the problem in O(1) extra space complexity 14 | * and O(n) time complexity. 15 | * 16 | * 17 | * Example 1: 18 | * 19 | * 20 | * Input: head = [1,2,3,4,5] 21 | * Output: [1,3,5,2,4] 22 | * 23 | * 24 | * Example 2: 25 | * 26 | * 27 | * Input: head = [2,1,3,5,6,4,7] 28 | * Output: [2,3,6,7,1,5,4] 29 | * 30 | * 31 | * Constraints: 32 | * 33 | * The number of nodes in the linked list is in the range [0, 10^4]. 34 | * -10^6 <= Node.val <= 10^6 35 | */ 36 | 37 | import '../structure/list_node.dart'; 38 | 39 | class Solution { 40 | ListNode? oddEvenList(ListNode? head) { 41 | final head2 = head?.next; 42 | if (head == null || head2 == null) return head; 43 | var ptr1 = head, ptr2 = head2; 44 | while (true) { 45 | ptr1.next = ptr2.next; 46 | if (ptr1.next == null) break; 47 | ptr1 = ptr1.next!; 48 | ptr2.next = ptr1.next; 49 | if (ptr2.next == null) break; 50 | ptr2 = ptr2.next!; 51 | } 52 | ptr1.next = head2; 53 | ptr2.next = null; 54 | return head; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /lib/src/problem/2.add_two_numbers.dart: -------------------------------------------------------------------------------- 1 | /* 2 | * 2. Add Two Numbers 3 | * 4 | * You are given two non-empty linked lists representing two non-negative integers. 5 | * The digits are stored in reverse order, 6 | * and each of their nodes contains a single digit. 7 | * Add the two numbers and return the sum as a linked list. 8 | * 9 | * You may assume the two numbers do not contain any leading zero, 10 | * except the number 0 itself. 11 | * 12 | * 13 | * Example 1: 14 | * 15 | * Input: l1 = [2,4,3], l2 = [5,6,4] 16 | * Output: [7,0,8] 17 | * Explanation: 342 + 465 = 807. 18 | * 19 | * 20 | * Example 2: 21 | * 22 | * Input: l1 = [0], l2 = [0] 23 | * Output: [0] 24 | * 25 | * 26 | * Example 3: 27 | * 28 | * Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9] 29 | * Output: [8,9,9,9,0,0,0,1] 30 | * 31 | * 32 | * Constraints: 33 | * 34 | * The number of nodes in each linked list is in the range [1, 100]. 35 | * 0 <= Node.val <= 9 36 | * It is guaranteed that the list represents a number that does not have leading zeros. 37 | */ 38 | 39 | import '../structure/list_node.dart'; 40 | 41 | class Solution { 42 | ListNode? addTwoNumbers(ListNode? l1, ListNode? l2) { 43 | if (l1 == null || l2 == null) return l1 ?? l2; 44 | final dummyHead = ListNode(0); 45 | var carry = 0, sum = 0; 46 | for (var curr = dummyHead; l1 != null || l2 != null || carry != 0; curr = curr.next = ListNode(sum % 10)) { 47 | sum = carry + ((l1 != null) ? l1.val : 0) + ((l2 != null) ? l2.val : 0); 48 | carry = sum ~/ 10; 49 | l1 = l1?.next; 50 | l2 = l2?.next; 51 | } 52 | return dummyHead.next; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /lib/src/problem/144.binary_tree_preorder_traversal.dart: -------------------------------------------------------------------------------- 1 | /* 2 | * 144. Binary Tree Preorder Traversal 3 | * 4 | * Given the root of a binary tree, return the preorder traversal of its nodes' values. 5 | * 6 | * 7 | * Example 1: 8 | * 9 | * Input: root = [1,null,2,3] 10 | * Output: [1,2,3] 11 | * 12 | * 13 | * Example 2: 14 | * 15 | * Input: root = [] 16 | * Output: [] 17 | * 18 | * 19 | * Example 3: 20 | * 21 | * Input: root = [1] 22 | * Output: [1] 23 | * 24 | * 25 | * Constraints: 26 | * 27 | * The number of nodes in the tree is in the range [0, 100]. 28 | * -100 <= Node.val <= 100 29 | * 30 | * 31 | * Follow up: Recursive solution is trivial, could you do it iteratively? 32 | */ 33 | 34 | import '../structure/tree_node.dart'; 35 | 36 | class SolutionV1 { 37 | List preorderTraversal(TreeNode? root) { 38 | final list = []; 39 | void visit(TreeNode? node) { 40 | if (node == null) return; 41 | list.add(node.val); 42 | visit(node.left); 43 | visit(node.right); 44 | } 45 | 46 | visit(root); 47 | return list; 48 | } 49 | } 50 | 51 | class SolutionV2 { 52 | List preorderTraversal(TreeNode? root) { 53 | Iterable traversal() sync* { 54 | final stack = []; 55 | TreeNode? maybePop() => stack.isEmpty ? null : stack.removeLast(); 56 | void maybePush(TreeNode? node) => node == null ? null : stack.add(node); 57 | for (var node = root; node != null; node = maybePop()) { 58 | yield node.val; 59 | maybePush(node.right); 60 | maybePush(node.left); 61 | } 62 | } 63 | 64 | return traversal().toList(); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /lib/src/problem/1299.replace_elements_with_greatest_element_on_right_side.dart: -------------------------------------------------------------------------------- 1 | /* 2 | * 1299. Replace Elements with Greatest Element on Right Side 3 | * 4 | * Given an array arr, replace every element in that array with the greatest 5 | * element among the elements to its right, and replace the last element with -1. 6 | * 7 | * After doing so, return the array. 8 | * 9 | * 10 | * Example 1: 11 | * 12 | * Input: arr = [17,18,5,4,6,1] 13 | * Output: [18,6,6,6,1,-1] 14 | * Explanation: 15 | * - index 0 --> the greatest element to the right of index 0 is index 1 (18). 16 | * - index 1 --> the greatest element to the right of index 1 is index 4 (6). 17 | * - index 2 --> the greatest element to the right of index 2 is index 4 (6). 18 | * - index 3 --> the greatest element to the right of index 3 is index 4 (6). 19 | * - index 4 --> the greatest element to the right of index 4 is index 5 (1). 20 | * - index 5 --> there are no elements to the right of index 5, so we put -1. 21 | * 22 | * 23 | * Example 2: 24 | * 25 | * Input: arr = [400] 26 | * Output: [-1] 27 | * Explanation: There are no elements to the right of index 0. 28 | * 29 | * 30 | * Constraints: 31 | * 32 | * 1 <= arr.length <= 10^4 33 | * 1 <= arr[i] <= 10^5 34 | */ 35 | 36 | class Solution { 37 | List replaceElements(List arr) { 38 | if (arr.isEmpty) return arr; 39 | final last = arr.length - 1; 40 | var max = 0; 41 | for (var i = 0; i < last; i++) { 42 | if (max == i) { 43 | max = i + 1; 44 | for (var j = i + 2; j <= last; j++) if (arr[j] >= arr[max]) max = j; 45 | } 46 | arr[i] = arr[max]; 47 | } 48 | return arr..[last] = -1; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /test/structure/tree_node.test.dart: -------------------------------------------------------------------------------- 1 | import 'package:leetcode/src/structure/tree_node.dart'; 2 | import 'package:test/test.dart'; 3 | 4 | void main() => group('tree_node', () { 5 | test('create', () { 6 | expect(() => TreeNode(1), returnsNormally); 7 | expect(TreeNode(1), isA()); 8 | expect(TreeNode(1).val, equals(1)); 9 | }); 10 | 11 | test('of', () { 12 | expect(() => TreeNode.of([1, 2, 3]), returnsNormally); 13 | expect( 14 | TreeNode.of([])?.toList(), 15 | isNull, 16 | ); 17 | expect( 18 | TreeNode.of([0])?.toList(), 19 | equals([0]), 20 | ); 21 | expect( 22 | TreeNode.of([1, 2, 3])?.toList(), 23 | equals([1, 2, 3]), 24 | ); 25 | expect( 26 | TreeNode.of([1, null, 2, 3])?.toList(), 27 | equals([1, null, 2, 3]), 28 | ); 29 | expect( 30 | TreeNode.of([5, 4, 7, 3, null, 2, null, -1, null, 9])?.toList(), 31 | equals([5, 4, 7, 3, null, 2, null, -1, null, 9]), 32 | ); 33 | expect( 34 | TreeNode.of([1, 2, 3, null, null, 6, 7])?.toList(), 35 | equals([1, 2, 3, null, null, 6, 7]), 36 | ); 37 | }); 38 | 39 | test('toJson', () { 40 | expect(() => TreeNode.of([1, 2, 3])?.toJson(), returnsNormally); 41 | expect(TreeNode.of([1, 2, 3])?.toJson(), isA>()); 42 | }); 43 | 44 | test('toString', () { 45 | expect(() => TreeNode.of([1, 2, 3]).toString(), returnsNormally); 46 | expect(TreeNode.of([1, 2, 3])?.toString(), isA()); 47 | }); 48 | }); 49 | -------------------------------------------------------------------------------- /lib/src/problem/106.construct_binary_tree_from_inorder_and_postorder_traversal.dart: -------------------------------------------------------------------------------- 1 | /* 2 | * 106. Construct Binary Tree from Inorder and Postorder Traversal 3 | * 4 | * Given two integer arrays inorder and postorder where inorder 5 | * is the inorder traversal of a binary tree and postorder 6 | * is the postorder traversal of the same tree, 7 | * construct and return the binary tree. 8 | * 9 | * 10 | * Example 1: 11 | * 12 | * 13 | * Input: inorder = [9,3,15,20,7], postorder = [9,15,7,20,3] 14 | * Output: [3,9,20,null,null,15,7] 15 | * 16 | * 17 | * Example 2: 18 | * 19 | * Input: inorder = [-1], postorder = [-1] 20 | * Output: [-1] 21 | * 22 | * 23 | * Constraints: 24 | * 25 | * 1 <= inorder.length <= 3000 26 | * postorder.length == inorder.length 27 | * -3000 <= inorder[i], postorder[i] <= 3000 28 | * inorder and postorder consist of unique values. 29 | * Each value of postorder also appears in inorder. 30 | * inorder is guaranteed to be the inorder traversal of the tree. 31 | * postorder is guaranteed to be the postorder traversal of the tree. 32 | */ 33 | 34 | import '../structure/tree_node.dart'; 35 | 36 | class Solution { 37 | TreeNode? buildTree(List inorder, List postorder) { 38 | assert(inorder.length == postorder.length); 39 | assert(inorder.toSet().length == inorder.length); 40 | assert(postorder.toSet().length == postorder.length); 41 | if (inorder.isEmpty) return null; 42 | final idx = inorder.indexOf(postorder.last); 43 | return TreeNode( 44 | postorder.last, 45 | buildTree(inorder.sublist(0, idx), postorder.sublist(0, idx)), 46 | buildTree(inorder.sublist(idx + 1), postorder.sublist(idx, postorder.length - 1)), 47 | ); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /lib/src/problem/101.symmetric_tree.dart: -------------------------------------------------------------------------------- 1 | /* 2 | * 101. Symmetric Tree 3 | * 4 | * Given the root of a binary tree, 5 | * check whether it is a mirror of itself 6 | * (i.e., symmetric around its center). 7 | * 8 | * 9 | * Example 1: 10 | * 11 | * 12 | * Input: root = [1,2,2,3,4,4,3] 13 | * Output: true 14 | * 15 | * 16 | * Example 2: 17 | * 18 | * 19 | * Input: root = [1,2,2,null,3,null,3] 20 | * Output: false 21 | * 22 | * 23 | * Constraints: 24 | * 25 | * The number of nodes in the tree is in the range [1, 1000]. 26 | * -100 <= Node.val <= 100 27 | * 28 | * 29 | * Follow up: Could you solve it both recursively and iteratively? 30 | */ 31 | 32 | import '../structure/tree_node.dart'; 33 | 34 | class SolutionV1 { 35 | bool isSymmetric(TreeNode? root) { 36 | bool compare(TreeNode? left, TreeNode? right) { 37 | if (left == null && right == null) return true; 38 | if (left?.val != right?.val) return false; 39 | return compare(left?.left, right?.right) && compare(left?.right, right?.left); 40 | } 41 | 42 | return compare(root?.left, root?.right); 43 | } 44 | } 45 | 46 | class SolutionV2 { 47 | bool isSymmetric(TreeNode? root) { 48 | final lefty = [root?.left], righty = [root?.right]; 49 | TreeNode? pop(List list) => list.isEmpty ? null : list.removeLast(); 50 | while (lefty.isNotEmpty && righty.isNotEmpty) { 51 | final l = pop(lefty), r = pop(righty); 52 | if (l == null && r == null) continue; 53 | if (l?.val != r?.val) return false; 54 | lefty 55 | ..add(l?.left) 56 | ..add(l?.right); 57 | righty 58 | ..add(r?.right) 59 | ..add(r?.left); 60 | } 61 | 62 | return true; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /lib/src/problem/1672.richest_customer_wealth.dart: -------------------------------------------------------------------------------- 1 | /* 2 | * 1672. Richest Customer Wealth 3 | * 4 | * You are given an m x n integer grid accounts where accounts[i][j] 5 | * is the amount of money the i^​​​​​​​​​​​th​​​​ customer has in the j​​​​​​​​​​^​th​​​​ bank. 6 | * Return the wealth that the richest customer has. 7 | * 8 | * A customer's wealth is the amount of money they have in all their bank accounts. 9 | * The richest customer is the customer that has the maximum wealth. 10 | * 11 | * 12 | * Example 1: 13 | * 14 | * Input: accounts = [[1,2,3],[3,2,1]] 15 | * Output: 6 16 | * Explanation: 17 | * 1st customer has wealth = 1 + 2 + 3 = 6 18 | * 2nd customer has wealth = 3 + 2 + 1 = 6 19 | * Both customers are considered the richest with a wealth of 6 each, so return 6. 20 | * 21 | * 22 | * Example 2: 23 | * 24 | * Input: accounts = [[1,5],[7,3],[3,5]] 25 | * Output: 10 26 | * Explanation: 27 | * 1st customer has wealth = 6 28 | * 2nd customer has wealth = 10 29 | * 3rd customer has wealth = 8 30 | * The 2nd customer is the richest with a wealth of 10. 31 | * 32 | * 33 | * Example 3: 34 | * 35 | * Input: accounts = [[2,8,7],[7,1,3],[1,9,5]] 36 | * Output: 17 37 | * 38 | * 39 | * Constraints: 40 | * 41 | * m == accounts.length 42 | * n == accounts[i].length 43 | * 1 <= m, n <= 50 44 | * 1 <= accounts[i][j] <= 100 45 | */ 46 | 47 | import 'dart:math' as math; 48 | 49 | class Solution { 50 | int maximumWealth(List> accounts) { 51 | var maximum = 0; 52 | for (var i = 0; i < accounts.length; i++) { 53 | var sum = 0; 54 | for (var j = 0; j < accounts[i].length; j++) { 55 | sum += accounts[i][j]; 56 | } 57 | maximum = math.max(maximum, sum); 58 | } 59 | return maximum; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /lib/src/problem/1051.height_checker.dart: -------------------------------------------------------------------------------- 1 | /* 2 | * 1051. Height Checker 3 | * 4 | * A school is trying to take an annual photo of all the students. 5 | * The students are asked to stand in a single file line in non-decreasing order by height. 6 | * Let this ordering be represented by the integer array expected where expected[i] 7 | * is the expected height of the i^th student in line. 8 | * 9 | * You are given an integer array heights representing the current order that 10 | * the students are standing in. Each heights[i] is the height 11 | * of the ith student in line (0-indexed). 12 | * 13 | * Return the number of indices where heights[i] != expected[i]. 14 | * 15 | * 16 | * Example 1: 17 | * 18 | * Input: heights = [1,1,4,2,1,3] 19 | * Output: 3 20 | * Explanation: 21 | * heights: [1,1,4,2,1,3] 22 | * expected: [1,1,1,2,3,4] 23 | * Indices 2, 4, and 5 do not match. 24 | * 25 | * 26 | * Example 2: 27 | * 28 | * Input: heights = [5,1,2,3,4] 29 | * Output: 5 30 | * Explanation: 31 | * heights: [5,1,2,3,4] 32 | * expected: [1,2,3,4,5] 33 | * All indices do not match. 34 | * 35 | * 36 | * Example 3: 37 | * 38 | * Input: heights = [1,2,3,4,5] 39 | * Output: 0 40 | * Explanation: 41 | * heights: [1,2,3,4,5] 42 | * expected: [1,2,3,4,5] 43 | * All indices match. 44 | * 45 | * 46 | * Constraints: 47 | * 48 | * 1 <= heights.length <= 100 49 | * 1 <= heights[i] <= 100 50 | */ 51 | 52 | class Solution { 53 | int heightChecker(List heights) { 54 | if (heights.length < 2) return 0; 55 | final tmp = heights.toList(growable: false)..sort(); 56 | var count = 0; 57 | for (var i = 0; i < heights.length; i++) { 58 | if (heights[i] == tmp[i]) continue; 59 | count++; 60 | } 61 | return count; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /lib/src/problem/1346.check_if_n_and_its_double_exist.dart: -------------------------------------------------------------------------------- 1 | /* 2 | * 1346. Check If N and Its Double Exist 3 | * 4 | * Given an array arr of integers, check if there exists two integers N and M 5 | * such that N is the double of M ( i.e. N = 2 * M). 6 | * 7 | * More formally check if there exists two indices i and j such that : 8 | * 9 | * i != j 10 | * 0 <= i, j < arr.length 11 | * arr[i] == 2 * arr[j] 12 | * 13 | * 14 | * Example 1: 15 | * 16 | * Input: arr = [10,2,5,3] 17 | * Output: true 18 | * Explanation: N = 10 is the double of M = 5,that is, 10 = 2 * 5. 19 | * 20 | * 21 | * Example 2: 22 | * 23 | * Input: arr = [7,1,14,11] 24 | * Output: true 25 | * Explanation: N = 14 is the double of M = 7,that is, 14 = 2 * 7. 26 | * 27 | * 28 | * Example 3: 29 | * 30 | * Input: arr = [3,1,7,11] 31 | * Output: false 32 | * Explanation: In this case does not exist N and M, such that N = 2 * M. 33 | * 34 | * 35 | * Constraints: 36 | * 37 | * 2 <= arr.length <= 500 38 | * -10^3 <= arr[i] <= 10^3 39 | */ 40 | 41 | class Solution { 42 | bool checkIfExist(List arr) { 43 | if (arr.length < 2) return false; 44 | for (var i = 0; i < arr.length - 1; i++) { 45 | final x = arr[i] * 2, y = arr[i] / 2; 46 | if (x == 0) { 47 | for (var j = i + 1; j < arr.length; j++) { 48 | if (arr[j] == 0) return true; 49 | continue; 50 | } 51 | } else if (y == y.truncate()) { 52 | for (var j = i + 1; j < arr.length; j++) { 53 | if (arr[j] == x || arr[j] == y) return true; 54 | continue; 55 | } 56 | } else { 57 | for (var j = i + 1; j < arr.length; j++) { 58 | if (arr[j] == x) return true; 59 | continue; 60 | } 61 | } 62 | } 63 | return false; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /lib/src/structure/tree_node.dart: -------------------------------------------------------------------------------- 1 | import 'dart:convert'; 2 | 3 | class TreeNode { 4 | TreeNode([this.val = 0, this.left, this.right]); 5 | 6 | static TreeNode? of(Iterable collection) { 7 | final iterator = collection.iterator; 8 | if (!iterator.moveNext() || iterator.current == null) return null; 9 | final head = TreeNode(iterator.current!); 10 | for (var queue = [head]; queue.isNotEmpty;) { 11 | final newQueue = []; 12 | for (final node in queue) { 13 | if (!iterator.moveNext()) return head; 14 | if (iterator.current != null) newQueue.add(node.left = TreeNode(iterator.current!)); 15 | if (!iterator.moveNext()) return head; 16 | if (iterator.current != null) newQueue.add(node.right = TreeNode(iterator.current!)); 17 | } 18 | queue = newQueue; 19 | } 20 | return head; 21 | } 22 | 23 | int val; 24 | TreeNode? left; 25 | TreeNode? right; 26 | 27 | List toList() { 28 | final list = [val]; 29 | for (var queue = [this]; queue.isNotEmpty;) { 30 | final newQueue = []; 31 | for (final node in queue) { 32 | list.addAll([node.left?.val, node.right?.val]); 33 | if (node.left != null) newQueue.add(node.left!); 34 | if (node.right != null) newQueue.add(node.right!); 35 | } 36 | queue = newQueue; 37 | } 38 | var last = list.length - 1; 39 | for (; last >= 0; last--) if (list[last] != null) break; 40 | return list.sublist(0, last + 1); 41 | } 42 | 43 | Map toJson() => { 44 | 'val': val, 45 | 'left': left?.toJson(), 46 | 'right': right?.toJson(), 47 | }; 48 | 49 | @override 50 | String toString() => const JsonEncoder.withIndent(' ').convert(this); 51 | } 52 | -------------------------------------------------------------------------------- /lib/src/problem/300.longest_increasing_subsequence.dart: -------------------------------------------------------------------------------- 1 | /* 2 | * 300. Longest Increasing Subsequence 3 | * 4 | * Given an integer array nums, 5 | * return the length of the longest strictly increasing subsequence. 6 | * 7 | * A subsequence is a sequence that can be derived from an array 8 | * by deleting some or no elements without changing the order of the remaining elements. 9 | * For example, [3,6,2,7] is a subsequence of the array [0,3,1,6,2,2,7]. 10 | * 11 | * 12 | * Example 1: 13 | * 14 | * Input: nums = [10,9,2,5,3,7,101,18] 15 | * Output: 4 16 | * Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4. 17 | * 18 | * 19 | * Example 2: 20 | * 21 | * Input: nums = [0,1,0,3,2,3] 22 | * Output: 4 23 | * 24 | * 25 | * Example 3: 26 | * 27 | * Input: nums = [7,7,7,7,7,7,7] 28 | * Output: 1 29 | * 30 | * 31 | * Constraints: 32 | * 33 | * 1 <= nums.length <= 2500 34 | * -10^4 <= nums[i] <= 10^4 35 | * 36 | * 37 | * Follow up: Can you come up with an algorithm that runs in O(n log(n)) time complexity? 38 | */ 39 | 40 | class Solution { 41 | int lengthOfLIS(List nums) { 42 | final sub = []; 43 | for (final n in nums) { 44 | final i = _binarySearch(sub, n); 45 | i == sub.length ? sub.add(n) : sub[i] = n; 46 | } 47 | return sub.length; 48 | } 49 | 50 | static int _binarySearch>(List sortedList, T value) { 51 | var min = 0, max = sortedList.length; 52 | while (min < max) { 53 | final mid = min + ((max - min) >> 1); 54 | final comp = sortedList[mid].compareTo(value); 55 | if (comp == 0) { 56 | return mid; 57 | } else if (comp.isNegative) { 58 | min = mid + 1; 59 | } else { 60 | max = mid; 61 | } 62 | } 63 | return min; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /lib/src/problem/145.binary_tree_postorder_traversal.dart: -------------------------------------------------------------------------------- 1 | /* 2 | * 145. Binary Tree Postorder Traversal 3 | * 4 | * Given the root of a binary tree, 5 | * return the postorder traversal of its nodes' values. 6 | * 7 | * 8 | * Example 1: 9 | * 10 | * Input: root = [1,null,2,3] 11 | * Output: [3,2,1] 12 | * 13 | * 14 | * Example 2: 15 | * 16 | * Input: root = [] 17 | * Output: [] 18 | * 19 | * 20 | * Example 3: 21 | * 22 | * Input: root = [1] 23 | * Output: [1] 24 | * 25 | * 26 | * Constraints: 27 | * 28 | * The number of the nodes in the tree is in the range [0, 100]. 29 | * -100 <= Node.val <= 100 30 | * 31 | * 32 | * Follow up: Recursive solution is trivial, could you do it iteratively? 33 | */ 34 | 35 | import '../structure/tree_node.dart'; 36 | 37 | class SolutionV1 { 38 | List postorderTraversal(TreeNode? root) { 39 | final list = []; 40 | void visit(TreeNode? node) { 41 | if (node == null) return; 42 | visit(node.left); 43 | visit(node.right); 44 | list.add(node.val); 45 | } 46 | 47 | visit(root); 48 | return list; 49 | } 50 | } 51 | 52 | class SolutionV2 { 53 | List postorderTraversal(TreeNode? root) { 54 | Iterable traversal() sync* { 55 | final stack = []; 56 | for (var node = root; node != null;) { 57 | final left = node.left; 58 | if (left != null) { 59 | stack.add(node..left = null); 60 | node = left; 61 | continue; 62 | } 63 | final right = node.right; 64 | if (right != null) { 65 | stack.add(node..right = null); 66 | node = right; 67 | continue; 68 | } 69 | yield node.val; 70 | node = stack.isEmpty ? null : stack.removeLast(); 71 | } 72 | } 73 | 74 | return traversal().toList(); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /lib/src/problem/414.third_maximum_number.dart: -------------------------------------------------------------------------------- 1 | /* 2 | * 414. Third Maximum Number 3 | * 4 | * Given an integer array nums, return the third distinct maximum number in this array. 5 | * If the third maximum does not exist, return the maximum number. 6 | * 7 | * 8 | * Example 1: 9 | * 10 | * Input: nums = [3,2,1] 11 | * Output: 1 12 | * Explanation: 13 | * The first distinct maximum is 3. 14 | * The second distinct maximum is 2. 15 | * The third distinct maximum is 1. 16 | * 17 | * 18 | * Example 2: 19 | * 20 | * Input: nums = [1,2] 21 | * Output: 2 22 | * Explanation: 23 | * The first distinct maximum is 2. 24 | * The second distinct maximum is 1. 25 | * The third distinct maximum does not exist, so the maximum (2) is returned instead. 26 | * 27 | * 28 | * Example 3: 29 | * 30 | * Input: nums = [2,2,3,1] 31 | * Output: 1 32 | * Explanation: 33 | * The first distinct maximum is 3. 34 | * The second distinct maximum is 2 (both 2's are counted together since they have the same value). 35 | * The third distinct maximum is 1. 36 | * 37 | * 38 | * Constraints: 39 | * 40 | * 1 <= nums.length <= 10^4 41 | * -2^31 <= nums[i] <= 2^31 - 1 42 | * 43 | * 44 | * Follow up: Can you find an O(n) solution? 45 | */ 46 | 47 | class Solution { 48 | int thirdMax(List nums) { 49 | const maximusLength = 3; 50 | final maximus = List.filled(maximusLength, null, growable: false); 51 | for (var i = 0; i < nums.length; i++) { 52 | var n = nums[i]; 53 | for (var j = 0; j < maximusLength; j++) { 54 | final m = maximus[j]; 55 | if (m == null) { 56 | maximus[j] = n; 57 | break; 58 | } else if (m < n) { 59 | maximus[j] = n; 60 | n = m; 61 | continue; 62 | } else if (m == n) { 63 | break; 64 | } 65 | } 66 | } 67 | return maximus.last ?? 68 | maximus.first ?? 69 | (throw ArgumentError.value(nums, 'nums', 'Array should contain at least one number')); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /test/problem/2.add_two_numbers.test.dart: -------------------------------------------------------------------------------- 1 | import 'package:leetcode/src/problem/2.add_two_numbers.dart'; 2 | import 'package:leetcode/src/structure/list_node.dart'; 3 | import 'package:test/test.dart'; 4 | 5 | void main() => group('add_two_numbers', () { 6 | final f = Solution().addTwoNumbers; 7 | 8 | test('f(null,null)', () { 9 | expect( 10 | f(null, null)?.toList(), 11 | isNull, 12 | ); 13 | }); 14 | 15 | test('f([0],null)', () { 16 | expect( 17 | f(ListNode.of([0]), null)?.toList(), 18 | equals([0]), 19 | ); 20 | }); 21 | 22 | test('f(null,[0])', () { 23 | expect( 24 | f(null, ListNode.of([0]))?.toList(), 25 | equals([0]), 26 | ); 27 | }); 28 | 29 | test('f([2,4,3],[5,6,4])', () { 30 | expect( 31 | f(ListNode.of([2, 4, 3]), ListNode.of([5, 6, 4]))?.toList(), 32 | equals([7, 0, 8]), 33 | ); 34 | }); 35 | 36 | test('f([0],[0])', () { 37 | expect( 38 | f(ListNode.of([0]), ListNode.of([0]))?.toList(), 39 | equals([0]), 40 | ); 41 | }); 42 | 43 | test('f([9,9,9,9,9,9,9],[9,9,9,9])', () { 44 | expect( 45 | f(ListNode.of([9, 9, 9, 9, 9, 9, 9]), ListNode.of([9, 9, 9, 9]))?.toList(), 46 | equals([8, 9, 9, 9, 0, 0, 0, 1]), 47 | ); 48 | }); 49 | 50 | test('f([2,4,9],[5,6,4,9])', () { 51 | expect( 52 | f(ListNode.of([2, 4, 9]), ListNode.of([5, 6, 4, 9]))?.toList(), 53 | equals([7, 0, 4, 0, 1]), 54 | ); 55 | }); 56 | 57 | test('f([1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],[5,6,4])', () { 58 | expect( 59 | f( 60 | ListNode.of([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]), 61 | ListNode.of([5, 6, 4]), 62 | )?.toList(), 63 | equals([6, 6, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]), 64 | ); 65 | }); 66 | }); 67 | -------------------------------------------------------------------------------- /.github/workflows/checkout.yml: -------------------------------------------------------------------------------- 1 | name: CHECKOUT 2 | 3 | on: 4 | workflow_dispatch: 5 | push: 6 | branches: 7 | - "master" 8 | - "develop" 9 | - "feature/**" 10 | - "bugfix/**" 11 | - "hotfix/**" 12 | - "support/**" 13 | paths: 14 | - "lib/**.dart" 15 | - "test/**.dart" 16 | - "example/**.dart" 17 | pull_request: 18 | branches: 19 | - "master" 20 | - "develop" 21 | - "feature/**" 22 | - "bugfix/**" 23 | - "hotfix/**" 24 | - "support/**" 25 | paths: 26 | - "lib/**.dart" 27 | - "test/**.dart" 28 | - "example/**.dart" 29 | 30 | jobs: 31 | checkout: 32 | name: "Checkout" 33 | runs-on: ubuntu-latest 34 | defaults: 35 | run: 36 | working-directory: ./ 37 | container: 38 | image: dart:stable 39 | timeout-minutes: 10 40 | steps: 41 | - name: 🚂 Get latest code 42 | uses: actions/checkout@v2 43 | 44 | - name: 🚃 Cache pub modules 45 | uses: actions/cache@v2 46 | env: 47 | cache-name: cache-leetcode-pub 48 | with: 49 | path: | 50 | $PWD/.pub_cache/ 51 | key: ${{ runner.os }}-leetcode 52 | 53 | - name: 🗄️ Export pub cache directory 54 | run: export PUB_CACHE=$PWD/.pub_cache/ 55 | 56 | - name: 👷 Install Dependencies 57 | timeout-minutes: 1 58 | run: dart pub get 59 | 60 | - name: 🔎 Check format 61 | timeout-minutes: 1 62 | run: dart format --set-exit-if-changed -l 120 -o none . 63 | 64 | - name: 📈 Check analyzer 65 | timeout-minutes: 1 66 | run: dart analyze --fatal-infos --fatal-warnings lib 67 | 68 | - name: 🧪 Run tests 69 | timeout-minutes: 2 70 | run: | 71 | dart run coverage:test_with_coverage -fb -o coverage -- \ 72 | --concurrency=6 --platform vm --coverage=./coverage --reporter=expanded test/leetcode_test.dart 73 | 74 | - name: 📥 Upload coverage to Codecov 75 | timeout-minutes: 1 76 | uses: codecov/codecov-action@v2.1.0 77 | with: 78 | token: ${{ secrets.CODECOV_TOKEN }} # not required for public repos 79 | -------------------------------------------------------------------------------- /lib/src/problem/94.binary_tree_inorder_traversal.dart: -------------------------------------------------------------------------------- 1 | /* 2 | * 94. Binary Tree Inorder Traversal 3 | * 4 | * Given the root of a binary tree, 5 | * return the inorder traversal of its nodes' values. 6 | * 7 | * 8 | * Example 1: 9 | * 10 | * Input: root = [1,null,2,3] 11 | * Output: [1,3,2] 12 | * 13 | * 14 | * Example 2: 15 | * 16 | * Input: root = [] 17 | * Output: [] 18 | * 19 | * 20 | * Example 3: 21 | * 22 | * Input: root = [1] 23 | * Output: [1] 24 | * 25 | * 26 | * Constraints: 27 | * 28 | * The number of nodes in the tree is in the range [0, 100]. 29 | * -100 <= Node.val <= 100 30 | * 31 | * 32 | * Follow up: Recursive solution is trivial, could you do it iteratively? 33 | */ 34 | 35 | import '../structure/tree_node.dart'; 36 | 37 | class SolutionV1 { 38 | List inorderTraversal(TreeNode? root) { 39 | if (root == null) return []; 40 | final list = []; 41 | void visit(TreeNode node) { 42 | if (node.left != null) visit(node.left!); 43 | list.add(node.val); 44 | if (node.right != null) visit(node.right!); 45 | } 46 | 47 | visit(root); 48 | return list; 49 | } 50 | } 51 | 52 | class SolutionV2 { 53 | List inorderTraversal(TreeNode? root) { 54 | Iterable traversal() sync* { 55 | final stack = []; 56 | for (var node = root; node != null;) { 57 | final left = node.left; 58 | if (left != null) { 59 | stack.add(node..left = null); 60 | node = left; 61 | continue; 62 | } 63 | yield node.val; 64 | node = node.right ?? (stack.isEmpty ? null : stack.removeLast()); 65 | } 66 | } 67 | 68 | return traversal().toList(); 69 | } 70 | } 71 | 72 | class SolutionV3 { 73 | List inorderTraversal(TreeNode? root) { 74 | Iterable traversal() sync* { 75 | final stack = []; 76 | for (var node = root; node != null || stack.isNotEmpty;) { 77 | while (node != null) { 78 | stack.add(node); 79 | node = node.left; 80 | } 81 | node = stack.removeLast(); 82 | yield node.val; 83 | node = node.right; 84 | } 85 | } 86 | 87 | return traversal().toList(); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /lib/src/problem/383.ransom_note.dart: -------------------------------------------------------------------------------- 1 | /* 2 | * 383. Ransom Note 3 | * 4 | * Given two strings ransomNote and magazine, 5 | * return true if ransomNote can be constructed by using the letters from magazine and false otherwise. 6 | * 7 | * Each letter in magazine can only be used once in ransomNote. 8 | * 9 | * 10 | * Example 1: 11 | * 12 | * Input: ransomNote = "a", magazine = "b" 13 | * Output: false 14 | * 15 | * 16 | * Example 2: 17 | * 18 | * Input: ransomNote = "aa", magazine = "ab" 19 | * Output: false 20 | * 21 | * 22 | * Example 3: 23 | * 24 | * Input: ransomNote = "aa", magazine = "aab" 25 | * Output: true 26 | * 27 | * 28 | * Constraints: 29 | * 30 | * 1 <= ransomNote.length, magazine.length <= 10^5 31 | * ransomNote and magazine consist of lowercase English letters. 32 | */ 33 | 34 | class SolutionV1 { 35 | bool canConstruct(String ransomNote, String magazine) { 36 | final lettersCodeOffset = 'a'.codeUnits.first, lettersCount = 26; 37 | final indexes = List.filled(lettersCount, 0, growable: false); 38 | int getIndex(String char) => indexes[char.codeUnitAt(0) - lettersCodeOffset]; 39 | void setIndex(String char, int index) => indexes[char.codeUnitAt(0) - lettersCodeOffset] = index; 40 | for (var i = 0; i < ransomNote.length; i++) { 41 | final index = magazine.indexOf(ransomNote[i], getIndex(ransomNote[i])); 42 | if (index == -1) return false; 43 | setIndex(ransomNote[i], index + 1); 44 | } 45 | return true; 46 | } 47 | } 48 | 49 | class SolutionV2 { 50 | bool canConstruct(String ransomNote, String magazine) { 51 | final lettersCodeOffset = 'a'.codeUnits.first, lettersCount = 26; 52 | final ransomNoteLettersCount = List.filled(lettersCount, 0, growable: false); 53 | final magazineLettersCount = List.filled(lettersCount, 0, growable: false); 54 | void inc(List list, String char) => list[char.codeUnitAt(0) - lettersCodeOffset]++; 55 | for (var i = 0; i < ransomNote.length; i++) inc(ransomNoteLettersCount, ransomNote[i]); 56 | for (var i = 0; i < magazine.length; i++) inc(magazineLettersCount, magazine[i]); 57 | for (var i = 0; i < lettersCount; i++) if (magazineLettersCount[i] < ransomNoteLettersCount[i]) return false; 58 | return true; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /lib/src/problem/21.merge_two_sorted_lists.dart: -------------------------------------------------------------------------------- 1 | /* 2 | * 21. Merge Two Sorted Lists 3 | * 4 | * You are given the heads of two sorted linked lists list1 and list2. 5 | * 6 | * Merge the two lists in a one sorted list. 7 | * The list should be made by splicing together the nodes of the first two lists. 8 | * 9 | * Return the head of the merged linked list. 10 | * 11 | * 12 | * Example 1: 13 | * 14 | * Input: list1 = [1,2,4], list2 = [1,3,4] 15 | * Output: [1,1,2,3,4,4] 16 | * 17 | * 18 | * Example 2: 19 | * 20 | * Input: list1 = [], list2 = [] 21 | * Output: [] 22 | * 23 | * 24 | * Example 3: 25 | * 26 | * Input: list1 = [], list2 = [0] 27 | * Output: [0] 28 | * 29 | * 30 | * Constraints: 31 | * 32 | * The number of nodes in both lists is in the range [0, 50]. 33 | * -100 <= Node.val <= 100 34 | * Both list1 and list2 are sorted in non-decreasing order. 35 | */ 36 | 37 | import '../structure/list_node.dart'; 38 | 39 | class SolutionV1 { 40 | ListNode? mergeTwoLists(ListNode? node1, ListNode? node2) { 41 | final newHead = ListNode(0); 42 | var prev = newHead; 43 | while (node1 != null && node2 != null) { 44 | if (node1.val > node2.val) { 45 | prev = prev.next = node2; 46 | node2 = node2.next; 47 | } else { 48 | prev = prev.next = node1; 49 | node1 = node1.next; 50 | } 51 | } 52 | prev.next = node1 ?? node2; 53 | return newHead.next; 54 | } 55 | } 56 | 57 | class SolutionV2 { 58 | ListNode? mergeTwoLists(ListNode? node1, ListNode? node2) { 59 | if (node1 == null) return node2; 60 | if (node2 == null) return node1; 61 | 62 | ListNode innerMerge(ListNode node1, ListNode node2) { 63 | final head = node1; 64 | var prev = head; 65 | ListNode? first = node1.next, second = node2; 66 | while (first != null && second != null) { 67 | if (first.val > second.val) { 68 | prev = prev.next = second; 69 | second = second.next; 70 | } else { 71 | prev = prev.next = first; 72 | first = first.next; 73 | } 74 | } 75 | prev.next = first ?? second; 76 | return head; 77 | } 78 | 79 | return node1.val > node2.val ? innerMerge(node2, node1) : innerMerge(node1, node2); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /test/problem/707.design_linked_list.test.dart: -------------------------------------------------------------------------------- 1 | import 'package:leetcode/src/problem/707.design_linked_list.dart'; 2 | import 'package:test/test.dart'; 3 | 4 | void main() => group('design_linked_list', () { 5 | late MyLinkedList list; 6 | 7 | setUp(() { 8 | list = MyLinkedList() 9 | ..addAtTail(1) 10 | ..addAtTail(2) 11 | ..addAtTail(3); 12 | }); 13 | 14 | test('get', () { 15 | expect(list.get(0), equals(1)); 16 | expect(list.get(1), equals(2)); 17 | expect(list.get(2), equals(3)); 18 | }); 19 | 20 | test('addAtHead', () { 21 | list.addAtHead(9); 22 | expect(list.get(0), equals(9)); 23 | }); 24 | 25 | test('addAtTail', () { 26 | list.addAtTail(9); 27 | expect(list.get(3), equals(9)); 28 | }); 29 | 30 | test('addAtIndex', () { 31 | list.addAtIndex(2, 9); 32 | expect(list.get(2), equals(9)); 33 | }); 34 | 35 | test('deleteAtIndex', () { 36 | list.deleteAtIndex(1); 37 | expect(list.get(0), equals(1)); 38 | expect(list.get(1), equals(3)); 39 | }); 40 | 41 | test('get_out_of_range', () { 42 | expect(list.get(-1), equals(-1)); 43 | expect(list.get(9), equals(-1)); 44 | }); 45 | 46 | test('deleteAtIndex_head', () { 47 | expect(() => list.deleteAtIndex(0), returnsNormally); 48 | expect(list.get(0), equals(2)); 49 | }); 50 | 51 | test('addAtIndex_head', () { 52 | expect(() => list.addAtIndex(0, 9), returnsNormally); 53 | expect(list.get(0), equals(9)); 54 | }); 55 | 56 | test('reverse_add', () { 57 | final list = MyLinkedList() 58 | ..addAtHead(1) 59 | ..addAtHead(2) 60 | ..addAtHead(3); 61 | expect(list.get(0), equals(3)); 62 | expect(list.get(1), equals(2)); 63 | expect(list.get(2), equals(1)); 64 | }); 65 | 66 | test('addAtIndex_and_get', () { 67 | final list = MyLinkedList()..addAtIndex(1, 0); 68 | expect(list.get(0), equals(-1)); 69 | }); 70 | 71 | test('delete_negative_index', () { 72 | final list = MyLinkedList() 73 | ..addAtIndex(1, 0) 74 | ..deleteAtIndex(-10); 75 | expect(list.get(0), equals(-1)); 76 | }); 77 | }); 78 | -------------------------------------------------------------------------------- /lib/src/problem/88.merge_sorted_array.dart: -------------------------------------------------------------------------------- 1 | /* 2 | * 88. Merge Sorted Array 3 | * 4 | * You are given two integer arrays nums1 and nums2, 5 | * sorted in non-decreasing order, and two integers m and n, 6 | * representing the number of elements in nums1 and nums2 respectively. 7 | * 8 | * Merge nums1 and nums2 into a single array sorted in non-decreasing order. 9 | * 10 | * The final sorted array should not be returned by the function, 11 | * but instead be stored inside the array nums1. 12 | * To accommodate this, nums1 has a length of m + n, 13 | * where the first m elements denote the elements that should be merged, 14 | * and the last n elements are set to 0 and should be ignored. 15 | * nums2 has a length of n. 16 | * 17 | * 18 | * Example 1: 19 | * 20 | * Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3 21 | * Output: [1,2,2,3,5,6] 22 | * Explanation: The arrays we are merging are [1,2,3] and [2,5,6]. 23 | * The result of the merge is [1,2,2,3,5,6] with the underlined elements coming from nums1. 24 | * 25 | * 26 | * Example 2: 27 | * 28 | * Input: nums1 = [1], m = 1, nums2 = [], n = 0 29 | * Output: [1] 30 | * Explanation: The arrays we are merging are [1] and []. 31 | * The result of the merge is [1]. 32 | * 33 | * 34 | * Example 3: 35 | * 36 | * Input: nums1 = [0], m = 0, nums2 = [1], n = 1 37 | * Output: [1] 38 | * Explanation: The arrays we are merging are [] and [1]. 39 | * The result of the merge is [1]. 40 | * Note that because m = 0, there are no elements in nums1. 41 | * The 0 is only there to ensure the merge result can fit in nums1. 42 | * 43 | * 44 | * Constraints: 45 | * 46 | * nums1.length == m + n 47 | * nums2.length == n 48 | * 0 <= m, n <= 200 49 | * 1 <= m + n <= 200 50 | * -10^9 <= nums1[i], nums2[j] <= 10^9 51 | * 52 | * Follow up: Can you come up with an algorithm that runs in O(m + n) time? 53 | */ 54 | 55 | class SolutionV1 { 56 | void merge(List nums1, int m, List nums2, int n) { 57 | for (var i = 0; i < n; i++) { 58 | nums1[i + m] = nums2[i]; 59 | } 60 | nums1.sort(); 61 | } 62 | } 63 | 64 | class SolutionV2 { 65 | void merge(List nums1, int m, List nums2, int n) { 66 | var i = m - 1, j = n - 1, k = m + n - 1; 67 | while (i >= 0 && j >= 0) { 68 | nums1[k--] = nums1[i] > nums2[j] ? nums1[i--] : nums2[j--]; 69 | } 70 | while (j >= 0) { 71 | nums1[k--] = nums2[j--]; 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /lib/src/problem/26.remove_duplicates_from_sorted_array.dart: -------------------------------------------------------------------------------- 1 | /* 2 | * 26. Remove Duplicates from Sorted Array 3 | * 4 | * Given an integer array nums sorted in non-decreasing order, 5 | * remove the duplicates in-place such that each unique element appears only once. 6 | * The relative order of the elements should be kept the same. 7 | * 8 | * Since it is impossible to change the length of the array in some languages, 9 | * you must instead have the result be placed in the first part of the array nums. 10 | * More formally, if there are k elements after removing the duplicates, 11 | * then the first k elements of nums should hold the final result. 12 | * It does not matter what you leave beyond the first k elements. 13 | * 14 | * Return k after placing the final result in the first k slots of nums. 15 | * 16 | * Do not allocate extra space for another array. 17 | * You must do this by modifying the input array in-place with O(1) extra memory. 18 | * 19 | * Custom Judge: 20 | * 21 | * The judge will test your solution with the following code: 22 | * 23 | * ``` 24 | * int[] nums = [...]; // Input array 25 | * int[] expectedNums = [...]; // The expected answer with correct length 26 | * 27 | * int k = removeDuplicates(nums); // Calls your implementation 28 | * 29 | * assert k == expectedNums.length; 30 | * for (int i = 0; i < k; i++) { 31 | * assert nums[i] == expectedNums[i]; 32 | * } 33 | * ``` 34 | * 35 | * If all assertions pass, then your solution will be accepted. 36 | * 37 | * 38 | * Example 1: 39 | * 40 | * Input: nums = [1,1,2] 41 | * Output: 2, nums = [1,2,_] 42 | * Explanation: Your function should return k = 2, with the first two elements of nums being 1 and 2 respectively. 43 | * It does not matter what you leave beyond the returned k (hence they are underscores). 44 | * 45 | * 46 | * Example 2: 47 | * 48 | * Input: nums = [0,0,1,1,1,2,2,3,3,4] 49 | * Output: 5, nums = [0,1,2,3,4,_,_,_,_,_] 50 | * Explanation: Your function should return k = 5, with the first five elements of nums being 0, 1, 2, 3, and 4 respectively. 51 | * It does not matter what you leave beyond the returned k (hence they are underscores). 52 | * 53 | * 54 | * Constraints: 55 | * 56 | * 1 <= nums.length <= 3 * 10^4 57 | * -100 <= nums[i] <= 100 58 | * nums is sorted in non-decreasing order. 59 | */ 60 | 61 | class Solution { 62 | int removeDuplicates(List nums) { 63 | if (nums.isEmpty) return 0; 64 | var p = 1; 65 | for (var i = 1; i < nums.length; i++) { 66 | if (nums[i] == nums[p - 1]) { 67 | nums[i] = 0; 68 | continue; 69 | } else if (i != p) { 70 | nums[p] = nums[i]; 71 | nums[i] = 0; 72 | } 73 | p++; 74 | } 75 | return p; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /lib/src/problem/27.remove_element.dart: -------------------------------------------------------------------------------- 1 | /* 2 | * 27. Remove Element 3 | * 4 | * Given an integer array nums and an integer val, 5 | * remove all occurrences of val in nums in-place. 6 | * The relative order of the elements may be changed. 7 | * 8 | * Since it is impossible to change the length of the array in some languages, 9 | * you must instead have the result be placed in the first part of the array nums. 10 | * More formally, if there are k elements after removing the duplicates, 11 | * then the first k elements of nums should hold the final result. 12 | * It does not matter what you leave beyond the first k elements. 13 | * 14 | * Return k after placing the final result in the first k slots of nums. 15 | * 16 | * Do not allocate extra space for another array. 17 | * You must do this by modifying the input array in-place with O(1) extra memory. 18 | * 19 | * Custom Judge: 20 | * 21 | * The judge will test your solution with the following code: 22 | * 23 | * int[] nums = [...]; // Input array 24 | * int val = ...; // Value to remove 25 | * int[] expectedNums = [...]; // The expected answer with correct length. 26 | * // It is sorted with no values equaling val. 27 | * 28 | * int k = removeElement(nums, val); // Calls your implementation 29 | * 30 | * assert k == expectedNums.length; 31 | * sort(nums, 0, k); // Sort the first k elements of nums 32 | * for (int i = 0; i < actualLength; i++) { 33 | * assert nums[i] == expectedNums[i]; 34 | * } 35 | * If all assertions pass, then your solution will be accepted. 36 | * 37 | * 38 | * 39 | * Example 1: 40 | * 41 | * Input: nums = [3,2,2,3], val = 3 42 | * Output: 2, nums = [2,2,_,_] 43 | * Explanation: Your function should return k = 2, 44 | * with the first two elements of nums being 2. 45 | * It does not matter what you leave beyond the returned k (hence they are underscores). 46 | * 47 | * 48 | * Example 2: 49 | * 50 | * Input: nums = [0,1,2,2,3,0,4,2], val = 2 51 | * Output: 5, nums = [0,1,4,0,3,_,_,_] 52 | * Explanation: Your function should return k = 5, 53 | * with the first five elements of nums containing 0, 0, 1, 3, and 4. 54 | * Note that the five elements can be returned in any order. 55 | * It does not matter what you leave beyond the returned k (hence they are underscores). 56 | * 57 | * 58 | * Constraints: 59 | * 60 | * 0 <= nums.length <= 100 61 | * 0 <= nums[i] <= 50 62 | * 0 <= val <= 100 63 | */ 64 | 65 | class Solution { 66 | int removeElement(List nums, int val) { 67 | var i = 0, n = nums.length; 68 | while (i < n) { 69 | if (nums[i] != val) { 70 | i++; 71 | continue; 72 | } 73 | nums[i] = nums[n - 1]; 74 | nums[n - 1] = -1; 75 | n--; 76 | } 77 | return n; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /test/problem/21.merge_two_sorted_lists.test.dart: -------------------------------------------------------------------------------- 1 | import 'package:leetcode/src/problem/21.merge_two_sorted_lists.dart'; 2 | import 'package:leetcode/src/structure/list_node.dart'; 3 | import 'package:test/test.dart'; 4 | 5 | void main() => group('merge_two_sorted_lists', () { 6 | final f1 = SolutionV1().mergeTwoLists; 7 | final f2 = SolutionV2().mergeTwoLists; 8 | 9 | test('f([1,2,4], [1,3,4])', () { 10 | expect( 11 | f1(ListNode.of([1, 2, 4]), ListNode.of([1, 3, 4]))?.toList(), 12 | equals([1, 1, 2, 3, 4, 4]), 13 | ); 14 | expect( 15 | f2(ListNode.of([1, 2, 4]), ListNode.of([1, 3, 4]))?.toList(), 16 | equals([1, 1, 2, 3, 4, 4]), 17 | ); 18 | }); 19 | 20 | test('f([1,2,4], [3,4])', () { 21 | expect( 22 | f1(ListNode.of([1, 2, 4]), ListNode.of([3, 4]))?.toList(), 23 | equals([1, 2, 3, 4, 4]), 24 | ); 25 | expect( 26 | f2(ListNode.of([1, 2, 4]), ListNode.of([3, 4]))?.toList(), 27 | equals([1, 2, 3, 4, 4]), 28 | ); 29 | }); 30 | 31 | test('f([2,4], [1,3,4])', () { 32 | expect( 33 | f1(ListNode.of([2, 4]), ListNode.of([1, 3, 4]))?.toList(), 34 | equals([1, 2, 3, 4, 4]), 35 | ); 36 | expect( 37 | f2(ListNode.of([2, 4]), ListNode.of([1, 3, 4]))?.toList(), 38 | equals([1, 2, 3, 4, 4]), 39 | ); 40 | }); 41 | 42 | test('f([-2,1,4], [-4,-1,3])', () { 43 | expect( 44 | f1(ListNode.of([-2, 1, 4]), ListNode.of([-4, -1, 3]))?.toList(), 45 | equals([-4, -2, -1, 1, 3, 4]), 46 | ); 47 | expect( 48 | f2(ListNode.of([-2, 1, 4]), ListNode.of([-4, -1, 3]))?.toList(), 49 | equals([-4, -2, -1, 1, 3, 4]), 50 | ); 51 | }); 52 | 53 | test('f([], [])', () { 54 | expect(f1(null, null)?.toList(), isNull); 55 | expect(f2(null, null)?.toList(), isNull); 56 | }); 57 | 58 | test('f([], [0])', () { 59 | expect(f1(null, ListNode.of([0]))?.toList(), equals([0])); 60 | expect(f2(null, ListNode.of([0]))?.toList(), equals([0])); 61 | }); 62 | 63 | test('f([0], [])', () { 64 | expect(f1(ListNode.of([0]), null)?.toList(), equals([0])); 65 | expect(f2(ListNode.of([0]), null)?.toList(), equals([0])); 66 | }); 67 | 68 | test('f([-10,-10,-9,-4,1,6,6], [-7])', () { 69 | expect( 70 | f1(ListNode.of([-10, -10, -9, -4, 1, 6, 6]), ListNode.of([-7]))?.toList(), 71 | equals([-10, -10, -9, -7, -4, 1, 6, 6]), 72 | ); 73 | expect( 74 | f2(ListNode.of([-10, -10, -9, -4, 1, 6, 6]), ListNode.of([-7]))?.toList(), 75 | equals([-10, -10, -9, -7, -4, 1, 6, 6]), 76 | ); 77 | }); 78 | }); 79 | -------------------------------------------------------------------------------- /lib/src/problem/707.design_linked_list.dart: -------------------------------------------------------------------------------- 1 | /* 2 | * 707. Design Linked List 3 | * 4 | * Design your implementation of the linked list. 5 | * You can choose to use a singly or doubly linked list. 6 | * A node in a singly linked list should have two attributes: 7 | * val and next. val is the value of the current node, 8 | * and next is a pointer/reference to the next node. 9 | * If you want to use the doubly linked list, 10 | * you will need one more attribute prev to indicate the previous node in the linked list. 11 | * Assume all nodes in the linked list are 0-indexed. 12 | * 13 | * Implement the MyLinkedList class: 14 | * 15 | * MyLinkedList() Initializes the MyLinkedList object. 16 | * int get(int index) Get the value of the indexth node in the linked list. 17 | * If the index is invalid, return -1. 18 | * void addAtHead(int val) Add a node of value val before the first element of the linked list. 19 | * After the insertion, the new node will be the first node of the linked list. 20 | * void addAtTail(int val) Append a node of value val as the last element of the linked list. 21 | * void addAtIndex(int index, int val) Add a node of value val before the indexth node in the linked list. 22 | * If index equals the length of the linked list, the node will be appended to the end of the linked list. 23 | * If index is greater than the length, the node will not be inserted. 24 | * void deleteAtIndex(int index) Delete the indexth node in the linked list, if the index is valid. 25 | * 26 | * 27 | * Example 1: 28 | * 29 | * Input 30 | * ["MyLinkedList", "addAtHead", "addAtTail", "addAtIndex", "get", "deleteAtIndex", "get"] 31 | * [[], [1], [3], [1, 2], [1], [1], [1]] 32 | * Output 33 | * [null, null, null, null, 2, null, 3] 34 | * 35 | * Explanation 36 | * MyLinkedList myLinkedList = new MyLinkedList(); 37 | * myLinkedList.addAtHead(1); 38 | * myLinkedList.addAtTail(3); 39 | * myLinkedList.addAtIndex(1, 2); // linked list becomes 1->2->3 40 | * myLinkedList.get(1); // return 2 41 | * myLinkedList.deleteAtIndex(1); // now the linked list is 1->3 42 | * myLinkedList.get(1); // return 3 43 | * 44 | * 45 | * Constraints: 46 | * 47 | * 0 <= index, val <= 1000 48 | * Please do not use the built-in LinkedList library. 49 | * At most 2000 calls will be made to get, addAtHead, addAtTail, addAtIndex and deleteAtIndex. 50 | */ 51 | 52 | class MyLinkedList { 53 | _Node? _head; 54 | 55 | _Node? _nodeAt(int index) { 56 | if (index < 0) return null; 57 | var node = _head; 58 | for (var i = 0; i < index && node != null; i++) node = node.next; 59 | return node; 60 | } 61 | 62 | int get(int index) => _nodeAt(index)?.val ?? -1; 63 | 64 | void addAtHead(int val) { 65 | _head = _Node() 66 | ..val = val 67 | ..prev = null 68 | ..next = _head; 69 | _head?.next?.prev = _head; 70 | } 71 | 72 | void addAtTail(int val) { 73 | var node = _head; 74 | if (node == null) return addAtHead(val); 75 | while (node!.next != null) node = node.next; 76 | node.next = _Node() 77 | ..val = val 78 | ..prev = node 79 | ..next = null; 80 | } 81 | 82 | void addAtIndex(int index, int val) { 83 | if (index < 1) return addAtHead(val); 84 | final node = _nodeAt(index - 1); 85 | if (node == null) return; 86 | final newNode = _Node() 87 | ..val = val 88 | ..prev = node 89 | ..next = node.next; 90 | node.next?.prev = newNode; 91 | node.next = newNode; 92 | } 93 | 94 | void deleteAtIndex(int index) { 95 | if (index == 0) { 96 | _head = _head?.next; 97 | return; 98 | } 99 | final node = _nodeAt(index - 1); 100 | if (node == null) return; 101 | node.next = node.next?.next; 102 | node.next?.prev = node; 103 | } 104 | } 105 | 106 | class _Node { 107 | int val = 0; 108 | _Node? prev, next; 109 | } 110 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | We as members, contributors, and leaders pledge to make participation in our 6 | community a harassment-free experience for everyone, regardless of age, body 7 | size, visible or invisible disability, ethnicity, sex characteristics, gender 8 | identity and expression, level of experience, education, socio-economic status, 9 | nationality, personal appearance, race, religion, or sexual identity 10 | and orientation. 11 | 12 | We pledge to act and interact in ways that contribute to an open, welcoming, 13 | diverse, inclusive, and healthy community. 14 | 15 | ## Our Standards 16 | 17 | Examples of behavior that contributes to a positive environment for our 18 | community include: 19 | 20 | * Demonstrating empathy and kindness toward other people 21 | * Being respectful of differing opinions, viewpoints, and experiences 22 | * Giving and gracefully accepting constructive feedback 23 | * Accepting responsibility and apologizing to those affected by our mistakes, 24 | and learning from the experience 25 | * Focusing on what is best not just for us as individuals, but for the 26 | overall community 27 | 28 | Examples of unacceptable behavior include: 29 | 30 | * The use of sexualized language or imagery, and sexual attention or 31 | advances of any kind 32 | * Trolling, insulting or derogatory comments, and personal or political attacks 33 | * Public or private harassment 34 | * Publishing others' private information, such as a physical or email 35 | address, without their explicit permission 36 | * Other conduct which could reasonably be considered inappropriate in a 37 | professional setting 38 | 39 | ## Enforcement Responsibilities 40 | 41 | Community leaders are responsible for clarifying and enforcing our standards of 42 | acceptable behavior and will take appropriate and fair corrective action in 43 | response to any behavior that they deem inappropriate, threatening, offensive, 44 | or harmful. 45 | 46 | Community leaders have the right and responsibility to remove, edit, or reject 47 | comments, commits, code, wiki edits, issues, and other contributions that are 48 | not aligned to this Code of Conduct, and will communicate reasons for moderation 49 | decisions when appropriate. 50 | 51 | ## Scope 52 | 53 | This Code of Conduct applies within all community spaces, and also applies when 54 | an individual is officially representing the community in public spaces. 55 | Examples of representing our community include using an official e-mail address, 56 | posting via an official social media account, or acting as an appointed 57 | representative at an online or offline event. 58 | 59 | ## Enforcement 60 | 61 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 62 | reported to the community leaders responsible for enforcement at 63 | plugfox@gmail.com. 64 | All complaints will be reviewed and investigated promptly and fairly. 65 | 66 | All community leaders are obligated to respect the privacy and security of the 67 | reporter of any incident. 68 | 69 | ## Enforcement Guidelines 70 | 71 | Community leaders will follow these Community Impact Guidelines in determining 72 | the consequences for any action they deem in violation of this Code of Conduct: 73 | 74 | ### 1. Correction 75 | 76 | **Community Impact**: Use of inappropriate language or other behavior deemed 77 | unprofessional or unwelcome in the community. 78 | 79 | **Consequence**: A private, written warning from community leaders, providing 80 | clarity around the nature of the violation and an explanation of why the 81 | behavior was inappropriate. A public apology may be requested. 82 | 83 | ### 2. Warning 84 | 85 | **Community Impact**: A violation through a single incident or series 86 | of actions. 87 | 88 | **Consequence**: A warning with consequences for continued behavior. No 89 | interaction with the people involved, including unsolicited interaction with 90 | those enforcing the Code of Conduct, for a specified period of time. This 91 | includes avoiding interactions in community spaces as well as external channels 92 | like social media. Violating these terms may lead to a temporary or 93 | permanent ban. 94 | 95 | ### 3. Temporary Ban 96 | 97 | **Community Impact**: A serious violation of community standards, including 98 | sustained inappropriate behavior. 99 | 100 | **Consequence**: A temporary ban from any sort of interaction or public 101 | communication with the community for a specified period of time. No public or 102 | private interaction with the people involved, including unsolicited interaction 103 | with those enforcing the Code of Conduct, is allowed during this period. 104 | Violating these terms may lead to a permanent ban. 105 | 106 | ### 4. Permanent Ban 107 | 108 | **Community Impact**: Demonstrating a pattern of violation of community 109 | standards, including sustained inappropriate behavior, harassment of an 110 | individual, or aggression toward or disparagement of classes of individuals. 111 | 112 | **Consequence**: A permanent ban from any sort of public interaction within 113 | the community. 114 | 115 | ## Attribution 116 | 117 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], 118 | version 2.0, available at 119 | https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. 120 | 121 | Community Impact Guidelines were inspired by [Mozilla's code of conduct 122 | enforcement ladder](https://github.com/mozilla/diversity). 123 | 124 | [homepage]: https://www.contributor-covenant.org 125 | 126 | For answers to common questions about this code of conduct, see the FAQ at 127 | https://www.contributor-covenant.org/faq. Translations are available at 128 | https://www.contributor-covenant.org/translations. -------------------------------------------------------------------------------- /test/leetcode_test.dart: -------------------------------------------------------------------------------- 1 | import 'package:test/test.dart'; 2 | 3 | import 'problem/101.symmetric_tree.test.dart' as symmetric_tree; 4 | import 'problem/102.binary_tree_level_order_traversal.test.dart' as binary_tree_level_order_traversal; 5 | import 'problem/104.maximum_depth_of_binary_tree.test.dart' as maximum_depth_of_binary_tree; 6 | import 'problem/1051.height_checker.test.dart' as height_checker; 7 | import 'problem/106.construct_binary_tree_from_inorder_and_postorder_traversal.test.dart' 8 | as construct_binary_tree_from_inorder_and_postorder_traversal; 9 | import 'problem/1089.duplicate_zeros.test.dart' as duplicate_zeros; 10 | import 'problem/112.path_sum.test.dart' as path_sum; 11 | import 'problem/1295.find_numbers_with_even_number_of_digits.test.dart' as find_numbers_with_even_number_of_digits; 12 | import 'problem/1299.replace_elements_with_greatest_element_on_right_side.test.dart' 13 | as replace_elements_with_greatest_element_on_right_side; 14 | import 'problem/1342.number_of_steps_to_reduce_a_number_to_zero.test.dart' 15 | as number_of_steps_to_reduce_a_number_to_zero; 16 | import 'problem/1346.check_if_n_and_its_double_exist.test.dart' as check_if_n_and_its_double_exist; 17 | import 'problem/144.binary_tree_preorder_traversal.test.dart' as binary_tree_preorder_traversal; 18 | import 'problem/145.binary_tree_postorder_traversal.test.dart' as binary_tree_postorder_traversal; 19 | import 'problem/1480.running_sum_of_1d_array.test.dart' as running_sum_of_1d_array; 20 | import 'problem/1672.richest_customer_wealth.test.dart' as richest_customer_wealth; 21 | import 'problem/19.remove_nth_node_from_end_of_list.test.dart' as remove_nth_node_from_end_of_list; 22 | import 'problem/2.add_two_numbers.test.dart' as add_two_numbers; 23 | import 'problem/203.remove_linked_list_elements.test.dart' as remove_linked_list_elements; 24 | import 'problem/206.reverse_linked_list.test.dart' as reverse_linked_list; 25 | import 'problem/21.merge_two_sorted_lists.test.dart' as merge_two_sorted_lists; 26 | import 'problem/2235.add_two_integers.test.dart' as add_two_integers; 27 | import 'problem/234.palindrome_linked_list.test.dart' as palindrome_linked_list; 28 | import 'problem/250.count_univalue_subtrees.test.dart' as count_univalue_subtrees; 29 | import 'problem/26.remove_duplicates_from_sorted_array.test.dart' as remove_duplicates_from_sorted_array; 30 | import 'problem/27.remove_element.test.dart' as remove_element; 31 | import 'problem/283.move_zeroes.test.dart' as move_zeroes; 32 | import 'problem/300.longest_increasing_subsequence.test.dart' as longest_increasing_subsequence; 33 | import 'problem/328.odd_even_linked_list.test.dart' as odd_even_linked_list; 34 | import 'problem/383.ransom_note.test.dart' as ransom_note; 35 | import 'problem/412.fizz_buzz.test.dart' as fizz_buzz; 36 | import 'problem/414.third_maximum_number.test.dart' as third_maximum_number; 37 | import 'problem/448.find_all_numbers_disappeared_in_an_array.test.dart' as find_all_numbers_disappeared_in_an_array; 38 | import 'problem/485.max_consecutive_ones.test.dart' as max_consecutive_ones; 39 | import 'problem/487.max_consecutive_ones_ii.test.dart' as max_consecutive_ones_ii; 40 | import 'problem/61.rotate_list.test.dart' as rotate_list; 41 | import 'problem/707.design_linked_list.test.dart' as design_linked_list; 42 | import 'problem/876.middle_of_the_linked_list.test.dart' as middle_of_the_linked_list; 43 | import 'problem/88.merge_sorted_array.test.dart' as merge_sorted_array; 44 | import 'problem/9.palindrome_number.test.dart' as palindrome_number; 45 | import 'problem/905.sort_array_by_parity.test.dart' as sort_array_by_parity; 46 | import 'problem/94.binary_tree_inorder_traversal.test.dart' as binary_tree_inorder_traversal; 47 | import 'problem/941.valid_mountain_array.test.dart' as valid_mountain_array; 48 | import 'problem/977.squares_of_a_sorted_array.test.dart' as squares_of_a_sorted_array; 49 | import 'structure/list_node.test.dart' as list_node; 50 | import 'structure/tree_node.test.dart' as tree_node; 51 | 52 | void main() { 53 | group('structure', () { 54 | list_node.main(); 55 | tree_node.main(); 56 | }); 57 | group('problems', () { 58 | max_consecutive_ones.main(); 59 | find_numbers_with_even_number_of_digits.main(); 60 | valid_mountain_array.main(); 61 | add_two_integers.main(); 62 | richest_customer_wealth.main(); 63 | running_sum_of_1d_array.main(); 64 | number_of_steps_to_reduce_a_number_to_zero.main(); 65 | middle_of_the_linked_list.main(); 66 | ransom_note.main(); 67 | fizz_buzz.main(); 68 | longest_increasing_subsequence.main(); 69 | merge_sorted_array.main(); 70 | duplicate_zeros.main(); 71 | squares_of_a_sorted_array.main(); 72 | remove_element.main(); 73 | remove_duplicates_from_sorted_array.main(); 74 | check_if_n_and_its_double_exist.main(); 75 | replace_elements_with_greatest_element_on_right_side.main(); 76 | move_zeroes.main(); 77 | sort_array_by_parity.main(); 78 | height_checker.main(); 79 | max_consecutive_ones_ii.main(); 80 | third_maximum_number.main(); 81 | find_all_numbers_disappeared_in_an_array.main(); 82 | design_linked_list.main(); 83 | remove_nth_node_from_end_of_list.main(); 84 | reverse_linked_list.main(); 85 | remove_linked_list_elements.main(); 86 | odd_even_linked_list.main(); 87 | palindrome_linked_list.main(); 88 | merge_two_sorted_lists.main(); 89 | palindrome_number.main(); 90 | add_two_numbers.main(); 91 | rotate_list.main(); 92 | binary_tree_preorder_traversal.main(); 93 | binary_tree_inorder_traversal.main(); 94 | binary_tree_postorder_traversal.main(); 95 | binary_tree_level_order_traversal.main(); 96 | maximum_depth_of_binary_tree.main(); 97 | symmetric_tree.main(); 98 | path_sum.main(); 99 | count_univalue_subtrees.main(); 100 | construct_binary_tree_from_inorder_and_postorder_traversal.main(); 101 | }); 102 | } 103 | -------------------------------------------------------------------------------- /analysis_options.yaml: -------------------------------------------------------------------------------- 1 | #include: package:lints/recommended.yaml 2 | 3 | analyzer: 4 | exclude: 5 | # Build 6 | - "build/**" 7 | # Tests 8 | - "test/**.mocks.dart" 9 | - ".test_coverage.dart" 10 | # Assets 11 | - "assets/**" 12 | 13 | strong-mode: 14 | implicit-casts: false 15 | implicit-dynamic: false 16 | 17 | errors: 18 | # Info 19 | todo: info 20 | directives_ordering: info 21 | always_declare_return_types: info 22 | 23 | # Warning 24 | unsafe_html: warning 25 | no_logic_in_create_state: warning 26 | empty_catches: warning 27 | 28 | # Error 29 | prefer_relative_imports: error 30 | avoid_relative_lib_imports: error 31 | avoid_slow_async_io: error 32 | avoid_types_as_parameter_names: error 33 | cancel_subscriptions: error 34 | close_sinks: error 35 | valid_regexps: error 36 | always_require_non_null_named_parameters: error 37 | 38 | linter: 39 | rules: 40 | # Public packages 41 | public_member_api_docs: false 42 | lines_longer_than_80_chars: false 43 | 44 | # Flutter 45 | use_full_hex_values_for_flutter_colors: false 46 | flutter_style_todos: false 47 | sized_box_for_whitespace: false 48 | sized_box_shrink_expand: false 49 | use_decorated_box: false 50 | avoid_unnecessary_containers: false 51 | 52 | # Disabled 53 | avoid_multiple_declarations_per_line: false 54 | always_use_package_imports: false 55 | avoid_annotating_with_dynamic: false 56 | unnecessary_final: false 57 | always_specify_types: false 58 | avoid_as: false 59 | avoid_redundant_argument_values: false 60 | comment_references: true # Unused because https://github.com/dart-lang/sdk/issues/36974 61 | prefer_double_quotes: false 62 | sort_unnamed_constructors_first: false 63 | avoid_web_libraries_in_flutter: false 64 | always_put_control_body_on_new_line: false 65 | sort_pub_dependencies: false 66 | do_not_use_environment: false 67 | diagnostic_describe_all_properties: false 68 | depend_on_referenced_packages: false 69 | library_private_types_in_public_api: false 70 | prefer_final_parameters: false 71 | avoid_types_on_closure_parameters: false 72 | use_is_even_rather_than_modulo: false 73 | curly_braces_in_flow_control_structures: false 74 | parameter_assignments: false 75 | prefer_asserts_with_message: false 76 | 77 | # Enabled 78 | prefer_relative_imports: true 79 | sort_constructors_first: true 80 | use_named_constants: true 81 | unnecessary_await_in_return: true 82 | tighten_type_of_initializing_formals: true 83 | exhaustive_cases: true 84 | always_put_required_named_parameters_first: true 85 | avoid_bool_literals_in_conditional_expressions: true 86 | avoid_double_and_int_checks: true 87 | avoid_escaping_inner_quotes: true 88 | avoid_field_initializers_in_const_classes: true 89 | avoid_implementing_value_types: true 90 | avoid_js_rounded_ints: true 91 | avoid_print: true 92 | avoid_renaming_method_parameters: true 93 | avoid_returning_null_for_future: true 94 | avoid_returning_null_for_void: true 95 | avoid_single_cascade_in_expression_statements: true 96 | avoid_slow_async_io: true 97 | avoid_unused_constructor_parameters: true 98 | avoid_void_async: true 99 | await_only_futures: true 100 | cancel_subscriptions: true 101 | cascade_invocations: true 102 | close_sinks: true 103 | control_flow_in_finally: true 104 | empty_statements: true 105 | iterable_contains_unrelated_type: true 106 | join_return_with_assignment: true 107 | leading_newlines_in_multiline_strings: true 108 | list_remove_unrelated_type: true 109 | literal_only_boolean_expressions: true 110 | missing_whitespace_between_adjacent_strings: true 111 | no_adjacent_strings_in_list: true 112 | no_logic_in_create_state: true 113 | no_runtimeType_toString: true 114 | only_throw_errors: true 115 | overridden_fields: true 116 | package_names: true 117 | package_prefixed_library_names: true 118 | prefer_asserts_in_initializer_lists: true 119 | prefer_const_constructors: true 120 | prefer_const_constructors_in_immutables: true 121 | prefer_const_declarations: true 122 | prefer_const_literals_to_create_immutables: true 123 | prefer_constructors_over_static_methods: true 124 | prefer_expression_function_bodies: true 125 | prefer_final_in_for_each: true 126 | prefer_final_locals: true 127 | prefer_foreach: true 128 | prefer_if_elements_to_conditional_expressions: true 129 | prefer_inlined_adds: true 130 | prefer_int_literals: true 131 | prefer_is_not_operator: true 132 | prefer_null_aware_operators: true 133 | prefer_typing_uninitialized_variables: true 134 | prefer_void_to_null: true 135 | provide_deprecation_message: true 136 | sort_child_properties_last: true 137 | test_types_in_equals: true 138 | throw_in_finally: true 139 | unnecessary_null_aware_assignments: true 140 | unnecessary_overrides: true 141 | unnecessary_parenthesis: true 142 | unnecessary_raw_strings: true 143 | unnecessary_statements: true 144 | unnecessary_string_escapes: true 145 | unnecessary_string_interpolations: true 146 | unsafe_html: true 147 | use_raw_strings: true 148 | use_string_buffers: true 149 | valid_regexps: true 150 | void_checks: true 151 | always_declare_return_types: true 152 | always_require_non_null_named_parameters: true 153 | annotate_overrides: true 154 | avoid_empty_else: true 155 | avoid_init_to_null: true 156 | avoid_null_checks_in_equality_operators: true 157 | avoid_relative_lib_imports: true 158 | avoid_return_types_on_setters: true 159 | avoid_shadowing_type_parameters: true 160 | avoid_types_as_parameter_names: true 161 | camel_case_extensions: true 162 | empty_catches: true 163 | empty_constructor_bodies: true 164 | library_names: true 165 | library_prefixes: true 166 | no_duplicate_case_values: true 167 | null_closures: true 168 | omit_local_variable_types: true 169 | prefer_adjacent_string_concatenation: true 170 | prefer_collection_literals: true 171 | prefer_conditional_assignment: true 172 | prefer_contains: true 173 | prefer_equal_for_default_values: true 174 | prefer_final_fields: true 175 | prefer_for_elements_to_map_fromIterable: true 176 | prefer_generic_function_type_aliases: true 177 | prefer_if_null_operators: true 178 | prefer_is_empty: true 179 | prefer_is_not_empty: true 180 | prefer_iterable_whereType: true 181 | prefer_single_quotes: true 182 | prefer_spread_collections: true 183 | recursive_getters: true 184 | slash_for_doc_comments: true 185 | type_init_formals: true 186 | unawaited_futures: true 187 | unnecessary_const: true 188 | unnecessary_new: true 189 | unnecessary_null_in_if_null_operators: true 190 | unnecessary_this: true 191 | unrelated_type_equality_checks: true 192 | use_function_type_syntax_for_parameters: true 193 | use_rethrow_when_possible: true 194 | camel_case_types: true 195 | file_names: true 196 | non_constant_identifier_names: true 197 | constant_identifier_names: true 198 | directives_ordering: true 199 | package_api_docs: true 200 | implementation_imports: true 201 | prefer_interpolation_to_compose_strings: true 202 | unnecessary_brace_in_string_interps: true 203 | avoid_function_literals_in_foreach_calls: true 204 | prefer_function_declarations_over_variables: true 205 | unnecessary_lambdas: true 206 | unnecessary_getters_setters: true 207 | prefer_initializing_formals: true 208 | avoid_catches_without_on_clauses: true 209 | avoid_catching_errors: true 210 | use_to_and_as_if_applicable: true 211 | one_member_abstracts: true 212 | avoid_classes_with_only_static_members: true 213 | prefer_mixin: true 214 | use_setters_to_change_properties: true 215 | avoid_setters_without_getters: true 216 | avoid_returning_null: true 217 | avoid_returning_this: true 218 | type_annotate_public_apis: true 219 | avoid_private_typedef_functions: true 220 | avoid_positional_boolean_parameters: true 221 | hash_and_equals: true 222 | avoid_equals_and_hash_code_on_mutable_classes: true 223 | avoid_dynamic_calls: true 224 | avoid_type_to_string: true 225 | deprecated_consistency: true 226 | eol_at_end_of_file: true 227 | noop_primitive_operations: true 228 | prefer_null_aware_method_calls: true 229 | unnecessary_constructor_name: true 230 | use_if_null_to_convert_nulls_to_bools: true 231 | use_test_throws_matchers: true 232 | unnecessary_late: true 233 | conditional_uri_does_not_exist: true 234 | #avoid_final_parameters: true 235 | no_leading_underscores_for_library_prefixes: true 236 | no_leading_underscores_for_local_identifiers: true 237 | secure_pubspec_urls: true 238 | 239 | # Experimental 240 | use_late_for_private_fields_and_variables: true 241 | use_build_context_synchronously: true 242 | unnecessary_nullable_for_final_variable_declarations: true 243 | unnecessary_null_checks: true 244 | require_trailing_commas: true 245 | null_check_on_nullable_type_parameter: true 246 | cast_nullable_to_non_nullable: true 247 | invariant_booleans: true 248 | no_default_cases: false 249 | 250 | # Deprecated 251 | #avoid_as (deprecated) 252 | #prefer_bool_in_asserts (deprecated) 253 | #super_goes_last (deprecated) 254 | --------------------------------------------------------------------------------