Skip to content

Commit c1015ab

Browse files
authored
Merge pull request #3569 from bansalgaurav852/main
create 0028-find-the-index-of-the-first-occurrence-in-a-string.dart
2 parents 4b778ab + 9c38b83 commit c1015ab

4 files changed

+87
-0
lines changed

dart/0002-add-two-numbers.dart

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* int val;
5+
* ListNode? next;
6+
* ListNode([this.val = 0, this.next]);
7+
* }
8+
*/
9+
class Solution {
10+
ListNode? addTwoNumbers(ListNode? l1, ListNode? l2) {
11+
ListNode ?listnode;
12+
int carry = 0;
13+
while (l1 != null || l2 != null) {
14+
int val = (l1?.val ?? 0) + (l2?.val ?? 0) + carry;
15+
l1 = l1?.next;
16+
l2 = l2?.next;
17+
if (val > 9){
18+
val = val - 10;
19+
carry = 1;
20+
} else {
21+
carry = 0;
22+
}
23+
24+
if (listnode != null)
25+
listnode = ListNode(val, listnode);
26+
27+
else
28+
listnode = ListNode(val);
29+
}
30+
if (carry != 0){
31+
listnode = ListNode(carry, listnode);
32+
}
33+
var list;
34+
while (listnode != null) {
35+
list = ListNode(listnode.val, list);;
36+
listnode = listnode?.next;
37+
}
38+
return list;
39+
}
40+
41+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
int lengthOfLongestSubstring(String s) {
3+
int max = 0;
4+
Map<String, int> map = {};
5+
6+
List<String> list = s.split('');
7+
List<String> result = [];
8+
9+
for (int i = 0; i < list.length; i++) {
10+
if (!result.contains(list[i])) {
11+
result.add(list[i]);
12+
} else {
13+
map.putIfAbsent(result.join(), () => result.length);
14+
while (result.contains(list[i])) {
15+
result.removeAt(0);
16+
}
17+
result.add(list[i]);
18+
}
19+
}
20+
21+
map.putIfAbsent(result.join(), () => result.length);
22+
map.forEach((key, value) {
23+
max = max > value ? max : value;
24+
});
25+
return max;
26+
}
27+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
double findMedianSortedArrays(List<int> nums1, List<int> nums2) {
3+
List<int>list = nums1 + nums2;
4+
5+
list.sort();
6+
int index = list.length ~/ 2;
7+
bool isodd = (list.length & 0x01) != 0;
8+
if (!isodd) {
9+
return (list[index] + list[index - 1]) / 2;
10+
}
11+
return double.parse(list[index].toString());
12+
}
13+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution {
2+
int strStr(String haystack, String needle) {
3+
var result = haystack.replaceAll(needle, '0').indexOf('0');
4+
return result;
5+
}
6+
}

0 commit comments

Comments
 (0)