Skip to content

Commit ceea62c

Browse files
committed
Added dart
1 parent 8995220 commit ceea62c

File tree

198 files changed

+7204
-309
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

198 files changed

+7204
-309
lines changed

README.md

Lines changed: 308 additions & 308 deletions
Large diffs are not rendered by default.

pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<groupId>com.github.javadev</groupId>
66
<artifactId>leetcode-in-all</artifactId>
77
<packaging>jar</packaging>
8-
<version>1.3</version>
8+
<version>1.4</version>
99
<name>leetcode-in-all</name>
1010
<description>104 LeetCode algorithm problem solutions</description>
1111
<url>https://github.com/javadev/LeetCode-in-All</url>
@@ -92,6 +92,7 @@
9292
<source>src/main/swift</source>
9393
<source>src/main/elixir</source>
9494
<source>src/main/rust</source>
95+
<source>src/main/dart</source>
9596
</sources>
9697
</configuration>
9798
</execution>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table
2+
// #Data_Structure_I_Day_2_Array #Level_1_Day_13_Hashmap #Udemy_Arrays #Big_O_Time_O(n)_Space_O(n)
3+
// #AI_can_be_used_to_solve_the_task #2024_09_28_Time_305_ms_(99.57%)_Space_146.2_MB_(99.70%)
4+
5+
class Solution {
6+
List<int> twoSum(List<int> numbers, int target) {
7+
Map<int, int> indexMap = {};
8+
for (int i = 0; i < numbers.length; i++) {
9+
int requiredNum = target - numbers[i];
10+
if (indexMap.containsKey(requiredNum)) {
11+
return [indexMap[requiredNum]!, i];
12+
}
13+
indexMap[numbers[i]] = i;
14+
}
15+
return [-1, -1];
16+
}
17+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
1\. Two Sum
2+
3+
Easy
4+
5+
Given an array of integers `nums` and an integer `target`, return _indices of the two numbers such that they add up to `target`_.
6+
7+
You may assume that each input would have **_exactly_ one solution**, and you may not use the _same_ element twice.
8+
9+
You can return the answer in any order.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [2,7,11,15], target = 9
14+
15+
**Output:** [0,1]
16+
17+
**Output:** Because nums[0] + nums[1] == 9, we return [0, 1].
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [3,2,4], target = 6
22+
23+
**Output:** [1,2]
24+
25+
**Example 3:**
26+
27+
**Input:** nums = [3,3], target = 6
28+
29+
**Output:** [0,1]
30+
31+
**Constraints:**
32+
33+
* <code>2 <= nums.length <= 10<sup>4</sup></code>
34+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
35+
* <code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code>
36+
* **Only one valid answer exists.**
37+
38+
**Follow-up:** Can you come up with an algorithm that is less than <code>O(n<sup>2</sup>) </code>time complexity?
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Math #Linked_List #Recursion
2+
// #Data_Structure_II_Day_10_Linked_List #Programming_Skills_II_Day_15
3+
// #Big_O_Time_O(max(N,M))_Space_O(max(N,M)) #AI_can_be_used_to_solve_the_task
4+
// #2024_09_28_Time_386_ms_(97.18%)_Space_151.9_MB_(38.24%)
5+
6+
/**
7+
* Definition for singly-linked list.
8+
* class ListNode {
9+
* int val;
10+
* ListNode? next;
11+
* ListNode([this.val = 0, this.next]);
12+
* }
13+
*/
14+
class Solution {
15+
ListNode addTwoNumbers(ListNode? l1, ListNode? l2) {
16+
ListNode dummyHead = ListNode(0);
17+
ListNode? p = l1;
18+
ListNode? q = l2;
19+
ListNode curr = dummyHead;
20+
int carry = 0;
21+
22+
while (p != null || q != null) {
23+
int x = (p != null) ? p.val : 0;
24+
int y = (q != null) ? q.val : 0;
25+
int sum = carry + x + y;
26+
carry = sum ~/ 10;
27+
curr.next = ListNode(sum % 10);
28+
curr = curr.next!;
29+
30+
if (p != null) p = p.next;
31+
if (q != null) q = q.next;
32+
}
33+
34+
if (carry > 0) {
35+
curr.next = ListNode(carry);
36+
}
37+
38+
return dummyHead.next!;
39+
}
40+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2\. Add Two Numbers
2+
3+
Medium
4+
5+
You are given two **non-empty** linked lists representing two non-negative integers. The digits are stored in **reverse order**, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
6+
7+
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2020/10/02/addtwonumber1.jpg)
12+
13+
**Input:** l1 = [2,4,3], l2 = [5,6,4]
14+
15+
**Output:** [7,0,8]
16+
17+
**Explanation:** 342 + 465 = 807.
18+
19+
**Example 2:**
20+
21+
**Input:** l1 = [0], l2 = [0]
22+
23+
**Output:** [0]
24+
25+
**Example 3:**
26+
27+
**Input:** l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
28+
29+
**Output:** [8,9,9,9,0,0,0,1]
30+
31+
**Constraints:**
32+
33+
* The number of nodes in each linked list is in the range `[1, 100]`.
34+
* `0 <= Node.val <= 9`
35+
* It is guaranteed that the list represents a number that does not have leading zeros.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Hash_Table #Sliding_Window
2+
// #Algorithm_I_Day_6_Sliding_Window #Level_2_Day_14_Sliding_Window/Two_Pointer #Udemy_Strings
3+
// #Big_O_Time_O(n)_Space_O(1) #2024_09_28_Time_336_ms_(99.68%)_Space_149.1_MB_(64.29%)
4+
5+
class Solution {
6+
int lengthOfLongestSubstring(String s) {
7+
List<int> lastIndices = List.filled(256, -1);
8+
int maxLen = 0;
9+
int curLen = 0;
10+
int start = 0;
11+
12+
for (int i = 0; i < s.length; i++) {
13+
int cur = s.codeUnitAt(i); // Getting ASCII value of the character
14+
if (lastIndices[cur] < start) {
15+
lastIndices[cur] = i;
16+
curLen++;
17+
} else {
18+
int lastIndex = lastIndices[cur];
19+
start = lastIndex + 1;
20+
curLen = i - start + 1;
21+
lastIndices[cur] = i;
22+
}
23+
if (curLen > maxLen) {
24+
maxLen = curLen;
25+
}
26+
}
27+
return maxLen;
28+
}
29+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
3\. Longest Substring Without Repeating Characters
2+
3+
Medium
4+
5+
Given a string `s`, find the length of the **longest substring** without repeating characters.
6+
7+
**Example 1:**
8+
9+
**Input:** s = "abcabcbb"
10+
11+
**Output:** 3
12+
13+
**Explanation:** The answer is "abc", with the length of 3.
14+
15+
**Example 2:**
16+
17+
**Input:** s = "bbbbb"
18+
19+
**Output:** 1
20+
21+
**Explanation:** The answer is "b", with the length of 1.
22+
23+
**Example 3:**
24+
25+
**Input:** s = "pwwkew"
26+
27+
**Output:** 3
28+
29+
**Explanation:** The answer is "wke", with the length of 3. Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
30+
31+
**Example 4:**
32+
33+
**Input:** s = ""
34+
35+
**Output:** 0
36+
37+
**Constraints:**
38+
39+
* <code>0 <= s.length <= 5 * 10<sup>4</sup></code>
40+
* `s` consists of English letters, digits, symbols and spaces.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Array #Binary_Search #Divide_and_Conquer
2+
// #Big_O_Time_O(log(min(N,M)))_Space_O(1)
3+
// #2024_09_28_Time_393_ms_(100.00%)_Space_151.2_MB_(78.38%)
4+
5+
import 'dart:math';
6+
7+
class Solution {
8+
double findMedianSortedArrays(List<int> nums1, List<int> nums2) {
9+
if (nums2.length < nums1.length) {
10+
return findMedianSortedArrays(nums2, nums1);
11+
}
12+
13+
int n1 = nums1.length;
14+
int n2 = nums2.length;
15+
int low = 0;
16+
int high = n1;
17+
18+
int minValue = -pow(2, 31).toInt(); // substitute for Integer.MIN_VALUE
19+
int maxValue = pow(2, 31).toInt() - 1; // substitute for Integer.MAX_VALUE
20+
21+
while (low <= high) {
22+
int cut1 = (low + high) ~/ 2;
23+
int cut2 = ((n1 + n2 + 1) ~/ 2) - cut1;
24+
25+
int l1 = cut1 == 0 ? minValue : nums1[cut1 - 1];
26+
int l2 = cut2 == 0 ? minValue : nums2[cut2 - 1];
27+
int r1 = cut1 == n1 ? maxValue : nums1[cut1];
28+
int r2 = cut2 == n2 ? maxValue : nums2[cut2];
29+
30+
if (l1 <= r2 && l2 <= r1) {
31+
if ((n1 + n2) % 2 == 0) {
32+
return (max(l1, l2) + min(r1, r2)) / 2.0;
33+
}
34+
return max(l1, l2).toDouble();
35+
} else if (l1 > r2) {
36+
high = cut1 - 1;
37+
} else {
38+
low = cut1 + 1;
39+
}
40+
}
41+
return 0.0;
42+
}
43+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
4\. Median of Two Sorted Arrays
2+
3+
Hard
4+
5+
Given two sorted arrays `nums1` and `nums2` of size `m` and `n` respectively, return **the median** of the two sorted arrays.
6+
7+
The overall run time complexity should be `O(log (m+n))`.
8+
9+
**Example 1:**
10+
11+
**Input:** nums1 = [1,3], nums2 = [2]
12+
13+
**Output:** 2.00000
14+
15+
**Explanation:** merged array = [1,2,3] and median is 2.
16+
17+
**Example 2:**
18+
19+
**Input:** nums1 = [1,2], nums2 = [3,4]
20+
21+
**Output:** 2.50000
22+
23+
**Explanation:** merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.
24+
25+
**Example 3:**
26+
27+
**Input:** nums1 = [0,0], nums2 = [0,0]
28+
29+
**Output:** 0.00000
30+
31+
**Example 4:**
32+
33+
**Input:** nums1 = [], nums2 = [1]
34+
35+
**Output:** 1.00000
36+
37+
**Example 5:**
38+
39+
**Input:** nums1 = [2], nums2 = []
40+
41+
**Output:** 2.00000
42+
43+
**Constraints:**
44+
45+
* `nums1.length == m`
46+
* `nums2.length == n`
47+
* `0 <= m <= 1000`
48+
* `0 <= n <= 1000`
49+
* `1 <= m + n <= 2000`
50+
* <code>-10<sup>6</sup> <= nums1[i], nums2[i] <= 10<sup>6</sup></code>

0 commit comments

Comments
 (0)