Skip to content

Commit 1e06e47

Browse files
committed
Leetcode and Binary Search
1 parent 95eab30 commit 1e06e47

7 files changed

+159
-0
lines changed
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
int solve(vector<int> &rooms, int target)
2+
{
3+
if (rooms.empty())
4+
return -1;
5+
6+
int n = rooms.size();
7+
for (int i = 0; i < n; i++)
8+
{
9+
if (rooms[i] >= target)
10+
return rooms[i];
11+
}
12+
return -1;
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
vector<int> solve(string s)
2+
{
3+
vector<int> ans;
4+
int n = s.size();
5+
int last[26];
6+
for (int i = 0; i < n; ++i)
7+
{
8+
last[s[i] - 'a'] = i;
9+
}
10+
11+
int i = 0;
12+
while (i < n)
13+
{
14+
int k = last[s[i] - 'a'];
15+
int j = i + 1;
16+
while (j <= k)
17+
{
18+
k = max(k, last[s[j] - 'a']);
19+
j += 1;
20+
}
21+
ans.push_back(j - i);
22+
i = j;
23+
}
24+
return ans;
25+
}
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* class Tree {
3+
* public:
4+
* int val;
5+
* Tree *left;
6+
* Tree *right;
7+
* };
8+
*/
9+
Tree *solve(Tree *root)
10+
{
11+
if (root == nullptr)
12+
{
13+
return root;
14+
}
15+
16+
queue<Tree *> q;
17+
q.push(root);
18+
while (!q.empty())
19+
{
20+
Tree *curr = q.front();
21+
q.pop();
22+
Tree *temp = curr->left;
23+
curr->left = curr->right;
24+
curr->right = temp;
25+
if (curr->left)
26+
{
27+
q.push(curr->left);
28+
}
29+
if (curr->right)
30+
{
31+
q.push(curr->right);
32+
}
33+
}
34+
return root;
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* class LLNode {
3+
* public:
4+
* int val;
5+
* LLNode *next;
6+
* };
7+
*/
8+
int solve(LLNode *node)
9+
{
10+
LLNode *fast = node, *slow = node;
11+
while (fast != nullptr and fast->next != nullptr)
12+
{
13+
slow = slow->next;
14+
fast = fast->next->next;
15+
}
16+
return slow->val;
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
class Solution
12+
{
13+
public:
14+
ListNode *removeElements(ListNode *head, int val)
15+
{
16+
if (head == NULL)
17+
{
18+
return head;
19+
}
20+
ListNode *prev = NULL, *tmp = head;
21+
22+
while (head)
23+
{
24+
if (head->val != val)
25+
{
26+
prev = head;
27+
}
28+
else
29+
{
30+
if (prev != NULL)
31+
{
32+
prev->next = head->next;
33+
}
34+
else
35+
{
36+
tmp = head->next;
37+
}
38+
}
39+
head = head->next;
40+
}
41+
return tmp;
42+
}
43+
};

Leet Code/C++/231. Power of Two.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution
2+
{
3+
public:
4+
bool isPowerOfTwo(int n)
5+
{
6+
if (n == 1)
7+
return true;
8+
if (n % 2 != 0 || n == 0)
9+
return false;
10+
if (isPowerOfTwo(n / 2))
11+
return true;
12+
return false;
13+
}
14+
};
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution
2+
{
3+
public:
4+
int fib(int n)
5+
{
6+
if (n == 0 || n == 1)
7+
{
8+
return n;
9+
}
10+
return fib(n - 1) + fib(n - 2);
11+
}
12+
};

0 commit comments

Comments
 (0)