Skip to content

Commit e421839

Browse files
committed
Added tasks 226-1143
1 parent 79617c3 commit e421839

File tree

24 files changed

+1679
-0
lines changed

24 files changed

+1679
-0
lines changed

README.md

Lines changed: 61 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Dart/LeetCode-in-Dart?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Dart/LeetCode-in-Dart)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Dart/LeetCode-in-Dart?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Dart/LeetCode-in-Dart/fork)
3+
4+
## 226\. Invert Binary Tree
5+
6+
Easy
7+
8+
Given the `root` of a binary tree, invert the tree, and return _its root_.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2021/03/14/invert1-tree.jpg)
13+
14+
**Input:** root = [4,2,7,1,3,6,9]
15+
16+
**Output:** [4,7,2,9,6,3,1]
17+
18+
**Example 2:**
19+
20+
![](https://assets.leetcode.com/uploads/2021/03/14/invert2-tree.jpg)
21+
22+
**Input:** root = [2,1,3]
23+
24+
**Output:** [2,3,1]
25+
26+
**Example 3:**
27+
28+
**Input:** root = []
29+
30+
**Output:** []
31+
32+
**Constraints:**
33+
34+
* The number of nodes in the tree is in the range `[0, 100]`.
35+
* `-100 <= Node.val <= 100`
36+
37+
## Solution
38+
39+
```dart
40+
/**
41+
* Definition for a binary tree node.
42+
* class TreeNode {
43+
* int val;
44+
* TreeNode? left;
45+
* TreeNode? right;
46+
* TreeNode([this.val = 0, this.left, this.right]);
47+
* }
48+
*/
49+
class Solution {
50+
TreeNode? invertTree(TreeNode? root) {
51+
if (root == null) {
52+
return root;
53+
}
54+
invertTree(root.left);
55+
invertTree(root.right);
56+
TreeNode? temp = root.left;
57+
root.left = root.right;
58+
root.right = temp;
59+
return root;
60+
}
61+
}
62+
```
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Dart/LeetCode-in-Dart?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Dart/LeetCode-in-Dart)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Dart/LeetCode-in-Dart?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Dart/LeetCode-in-Dart/fork)
3+
4+
## 230\. Kth Smallest Element in a BST
5+
6+
Medium
7+
8+
Given the `root` of a binary search tree, and an integer `k`, return _the_ <code>k<sup>th</sup></code> _smallest value (**1-indexed**) of all the values of the nodes in the tree_.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg)
13+
14+
**Input:** root = [3,1,4,null,2], k = 1
15+
16+
**Output:** 1
17+
18+
**Example 2:**
19+
20+
![](https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg)
21+
22+
**Input:** root = [5,3,6,2,4,null,null,1], k = 3
23+
24+
**Output:** 3
25+
26+
**Constraints:**
27+
28+
* The number of nodes in the tree is `n`.
29+
* <code>1 <= k <= n <= 10<sup>4</sup></code>
30+
* <code>0 <= Node.val <= 10<sup>4</sup></code>
31+
32+
**Follow up:** If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?
33+
34+
## Solution
35+
36+
```dart
37+
/**
38+
* Definition for a binary tree node.
39+
* class TreeNode {
40+
* int val;
41+
* TreeNode? left;
42+
* TreeNode? right;
43+
* TreeNode([this.val = 0, this.left, this.right]);
44+
* }
45+
*/
46+
class Solution {
47+
late int k;
48+
int count = 0;
49+
int? val;
50+
51+
int kthSmallest(TreeNode? root, int k) {
52+
this.k = k;
53+
_calculate(root);
54+
return val!;
55+
}
56+
57+
void _calculate(TreeNode? node) {
58+
if (node == null) return;
59+
60+
if (node.left != null) {
61+
_calculate(node.left);
62+
}
63+
64+
count++;
65+
if (count == k) {
66+
val = node.val;
67+
return;
68+
}
69+
70+
if (node.right != null) {
71+
_calculate(node.right);
72+
}
73+
}
74+
}
75+
```
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Dart/LeetCode-in-Dart?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Dart/LeetCode-in-Dart)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Dart/LeetCode-in-Dart?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Dart/LeetCode-in-Dart/fork)
3+
4+
## 234\. Palindrome Linked List
5+
6+
Easy
7+
8+
Given the `head` of a singly linked list, return `true` _if it is a palindrome or_ `false` _otherwise_.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg)
13+
14+
**Input:** head = [1,2,2,1]
15+
16+
**Output:** true
17+
18+
**Example 2:**
19+
20+
![](https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg)
21+
22+
**Input:** head = [1,2]
23+
24+
**Output:** false
25+
26+
**Constraints:**
27+
28+
* The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.
29+
* `0 <= Node.val <= 9`
30+
31+
**Follow up:** Could you do it in `O(n)` time and `O(1)` space?
32+
33+
## Solution
34+
35+
```dart
36+
/**
37+
* Definition for singly-linked list.
38+
* class ListNode {
39+
* int val;
40+
* ListNode? next;
41+
* ListNode([this.val = 0, this.next]);
42+
* }
43+
*/
44+
class Solution {
45+
bool isPalindrome(ListNode? head) {
46+
List<int> list = [];
47+
while (head != null) {
48+
list.add(head.val);
49+
head = head.next;
50+
}
51+
int l = 0;
52+
int r = list.length - 1;
53+
while (l < list.length && r >= 0 && list[l] == list[r]) {
54+
l++;
55+
r--;
56+
}
57+
return l == list.length;
58+
}
59+
}
60+
```
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Dart/LeetCode-in-Dart?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Dart/LeetCode-in-Dart)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Dart/LeetCode-in-Dart?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Dart/LeetCode-in-Dart/fork)
3+
4+
## 238\. Product of Array Except Self
5+
6+
Medium
7+
8+
Given an integer array `nums`, return _an array_ `answer` _such that_ `answer[i]` _is equal to the product of all the elements of_ `nums` _except_ `nums[i]`.
9+
10+
The product of any prefix or suffix of `nums` is **guaranteed** to fit in a **32-bit** integer.
11+
12+
You must write an algorithm that runs in `O(n)` time and without using the division operation.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [1,2,3,4]
17+
18+
**Output:** [24,12,8,6]
19+
20+
**Example 2:**
21+
22+
**Input:** nums = [-1,1,0,-3,3]
23+
24+
**Output:** [0,0,9,0,0]
25+
26+
**Constraints:**
27+
28+
* <code>2 <= nums.length <= 10<sup>5</sup></code>
29+
* `-30 <= nums[i] <= 30`
30+
* The product of any prefix or suffix of `nums` is **guaranteed** to fit in a **32-bit** integer.
31+
32+
**Follow up:** Can you solve the problem in `O(1) `extra space complexity? (The output array **does not** count as extra space for space complexity analysis.)
33+
34+
## Solution
35+
36+
```dart
37+
class Solution {
38+
List<int> productExceptSelf(List<int> nums) {
39+
int product = 1;
40+
List<int> ans = List.filled(nums.length, 0);
41+
42+
// Calculate total product of all elements
43+
for (int num in nums) {
44+
product *= num;
45+
}
46+
47+
// Fill in the result array
48+
for (int i = 0; i < nums.length; i++) {
49+
if (nums[i] != 0) {
50+
ans[i] = product ~/ nums[i]; // Integer division
51+
} else {
52+
int p = 1;
53+
for (int j = 0; j < nums.length; j++) {
54+
if (j != i) {
55+
p *= nums[j];
56+
}
57+
}
58+
ans[i] = p;
59+
}
60+
}
61+
62+
return ans;
63+
}
64+
}
65+
```
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Dart/LeetCode-in-Dart?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Dart/LeetCode-in-Dart)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Dart/LeetCode-in-Dart?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Dart/LeetCode-in-Dart/fork)
3+
4+
## 239\. Sliding Window Maximum
5+
6+
Hard
7+
8+
You are given an array of integers `nums`, there is a sliding window of size `k` which is moving from the very left of the array to the very right. You can only see the `k` numbers in the window. Each time the sliding window moves right by one position.
9+
10+
Return _the max sliding window_.
11+
12+
**Example 1:**
13+
14+
**Input:** nums = [1,3,-1,-3,5,3,6,7], k = 3
15+
16+
**Output:** [3,3,5,5,6,7]
17+
18+
**Explanation:**
19+
20+
Window position Max
21+
22+
--------------- -----
23+
24+
[1 3 -1] -3 5 3 6 7 **3**
25+
26+
1 [3 -1 -3] 5 3 6 7 **3**
27+
28+
1 3 [-1 -3 5] 3 6 7 **5**
29+
30+
1 3 -1 [-3 5 3] 6 7 **5**
31+
32+
1 3 -1 -3 [5 3 6] 7 **6**
33+
34+
1 3 -1 -3 5 [3 6 7] **7**
35+
36+
**Example 2:**
37+
38+
**Input:** nums = [1], k = 1
39+
40+
**Output:** [1]
41+
42+
**Constraints:**
43+
44+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
45+
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
46+
* `1 <= k <= nums.length`
47+
48+
## Solution
49+
50+
```dart
51+
import 'dart:collection';
52+
53+
class Solution {
54+
List<int> maxSlidingWindow(List<int> nums, int k) {
55+
int n = nums.length;
56+
List<int> res = List.filled(n - k + 1, 0);
57+
int x = 0;
58+
Queue<int> dq = Queue();
59+
int i = 0;
60+
int j = 0;
61+
62+
while (j < nums.length) {
63+
// Remove elements from the deque that are smaller than the current element
64+
while (dq.isNotEmpty && dq.last < nums[j]) {
65+
dq.removeLast();
66+
}
67+
68+
// Add the current element to the deque
69+
dq.addLast(nums[j]);
70+
71+
// When the window size reaches k
72+
if (j - i + 1 == k) {
73+
// The element at the front of the deque is the largest in the current window
74+
res[x] = dq.first;
75+
x++;
76+
77+
// If the element at the front of the deque is going out of the window, remove it
78+
if (dq.first == nums[i]) {
79+
dq.removeFirst();
80+
}
81+
82+
// Slide the window to the right
83+
i++;
84+
}
85+
86+
j++;
87+
}
88+
89+
return res;
90+
}
91+
}
92+
```

0 commit comments

Comments
 (0)