Skip to content

Commit 5b3b90e

Browse files
authored
Merge branch 'main' into main
2 parents e78c7fd + a7e5093 commit 5b3b90e

File tree

142 files changed

+4562
-313
lines changed

Some content is hidden

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

142 files changed

+4562
-313
lines changed

README.md

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

c/0067-add-binary.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
char *addBinary(const char *a, const char *b) {
2+
int maxLen = strlen(a) > strlen(b) ? strlen(a) : strlen(b);
3+
char *res = (char *)malloc((maxLen + 2) * sizeof(char));
4+
memset(res, 0, (maxLen + 2) * sizeof(char));
5+
unsigned int carry = 0;
6+
7+
for(int i = 0; i < maxLen; i++) {
8+
unsigned int bitA = i < strlen(a) ? a[strlen(a) - i - 1] - '0' : 0;
9+
unsigned int bitB = i < strlen(b) ? b[strlen(b) - i - 1] - '0' : 0;
10+
11+
unsigned int total = bitA + bitB + carry;
12+
char sum = '0' + total % 2;
13+
carry = total / 2;
14+
15+
// Add to the beginning of the string
16+
memmove(res + 1, res, strlen(res));
17+
res[0] = sum;
18+
}
19+
if(carry) {
20+
memmove(res + 1, res, strlen(res));
21+
res[0] = '1';
22+
}
23+
return res;
24+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* struct ListNode *next;
6+
* };
7+
*/
8+
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
9+
struct ListNode *currA = headA;
10+
struct ListNode *currB = headB;
11+
12+
while (currA && currB){
13+
if (currA == currB){
14+
return currA;
15+
}
16+
currA = currA->next;
17+
currB = currB->next;
18+
19+
if (!currA && currB){
20+
currA = headB;
21+
}
22+
if (!currB && currA){
23+
currB = headA;
24+
}
25+
}
26+
return NULL;
27+
}

c/0179-largest-number.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Compare two numbers in a concatenated form.
2+
// eg: 12 and 34 will be compared as 1234 vs 3412
3+
int compareInt(const void *a, const void *b) {
4+
int i = 10;
5+
int j = 10;
6+
int x = *(int*)a;
7+
int y = *(int*)b;
8+
9+
while (x /= 10) i *= 10;
10+
while (y /= 10) j *= 10;
11+
12+
return (((unsigned int)*(int*)b * i) + *(int*)a) > (((unsigned int)*(int*)a * j) + *(int*)b);
13+
}
14+
15+
char * largestNumber(int* nums, int numsSize){
16+
char *res = NULL;
17+
int i, len, pos;
18+
19+
if (numsSize < 0) {
20+
return res;
21+
}
22+
23+
// Sort the array with specified comparaotor.
24+
qsort(nums, numsSize, sizeof(int), compareInt);
25+
26+
// Caculate the length of the return string.
27+
len = 1;
28+
for (i = 0; i < numsSize; i++) len += snprintf(NULL, 0, "%d", nums[i]);
29+
res = calloc(len, sizeof(char));
30+
31+
// If the firs element of sorted array is 0,
32+
// return a single digit of 0 no matter how long is the string.
33+
if (nums[0] == 0) {
34+
res[0] = '0';
35+
return res;
36+
}
37+
38+
// Print all nums to the return string.
39+
pos = 0;
40+
for (i = 0; i < numsSize; i++) {
41+
pos += snprintf(res + pos, len, "%d", nums[i]);
42+
}
43+
44+
return res;
45+
}

c/0234-palindrome-linked-list.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* struct ListNode *next;
6+
* };
7+
*/
8+
bool isPalindrome(struct ListNode* head){
9+
10+
// find middle of linked list
11+
struct ListNode *slow = head, *fast = head;
12+
while (fast && fast->next){
13+
slow = slow->next;
14+
fast = fast->next->next;
15+
}
16+
// reverse second half
17+
struct ListNode* prev = NULL;
18+
while (slow){
19+
struct ListNode* nxt = slow->next;
20+
slow->next = prev;
21+
prev = slow;
22+
slow = nxt;
23+
}
24+
25+
// compare left and right
26+
struct ListNode *left=head, *right=prev;
27+
while (right){
28+
if (left->val != right->val){
29+
return false;
30+
}
31+
left = left->next;
32+
right = right->next;
33+
}
34+
return true;
35+
36+
}

c/0523-continuous-subarray-sum.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#define FREE_AND_RETURN(x, y) \
2+
free(x); \
3+
return y;
4+
5+
bool checkSubarraySum(int* nums, int numsSize, int k){
6+
unsigned int* sums = NULL;
7+
int i, j;
8+
9+
// Early return edge cases.
10+
if (numsSize < 2) {
11+
return false;
12+
}
13+
if (k == 1) {
14+
return true;
15+
}
16+
17+
sums = (unsigned int*)malloc(numsSize * sizeof(unsigned int));
18+
sums[0] = nums[0];
19+
for (i = 1; i < numsSize; i++) {
20+
// Return true, when there are two continuous nums are times of k.
21+
if (nums[i] % k == 0 && nums[i - 1] % k == 0) {
22+
FREE_AND_RETURN(sums, true);
23+
}
24+
25+
sums[i] = nums[i] + sums[i - 1];
26+
27+
// Return true, when the current subarray sum is times of k.
28+
if (sums[i] % k == 0) {
29+
FREE_AND_RETURN(sums, true);
30+
}
31+
}
32+
33+
for (i = 0; i < numsSize; i++) {
34+
if (sums[i] < k) {
35+
continue;
36+
}
37+
38+
for (j = 0; j < i - 1; j++) {
39+
// Return true, when the any subarray sum is times of k.
40+
if ((sums[i] - sums[j]) % k == 0) {
41+
FREE_AND_RETURN(sums, true);
42+
}
43+
}
44+
}
45+
46+
FREE_AND_RETURN(sums, false);
47+
}

c/0707-design-linked-list.c

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
typedef struct Node {
2+
struct Node* next;
3+
struct Node* prev;
4+
int val;
5+
} Node;
6+
7+
typedef struct {
8+
Node* head;
9+
Node* tail;
10+
int length;
11+
} MyLinkedList;
12+
13+
Node* createNode(int val) {
14+
Node* new_node = (Node*)malloc(sizeof(Node) );
15+
new_node->next = NULL;
16+
new_node->prev = NULL;
17+
new_node->val = val;
18+
return new_node;
19+
}
20+
21+
MyLinkedList* myLinkedListCreate() {
22+
MyLinkedList* list = (MyLinkedList*)malloc(sizeof(MyLinkedList) );
23+
list->head = createNode(-1);
24+
list->tail = list->head;
25+
list->length = 0;
26+
return list;
27+
}
28+
29+
Node* traverseList(MyLinkedList* obj, int index) {
30+
if (index < 0 || index >= obj->length)
31+
return NULL;
32+
Node* trev = obj->head;
33+
while (index >= 0) {
34+
trev = trev->next;
35+
index -= 1;
36+
}
37+
return trev;
38+
}
39+
40+
int myLinkedListGet(MyLinkedList* obj, int index) {
41+
Node *node = traverseList(obj, index);
42+
if (node != NULL)
43+
return node->val;
44+
return -1;
45+
}
46+
47+
void myLinkedListAddAtHead(MyLinkedList* obj, int val) {
48+
Node* new_node = createNode(val);
49+
new_node->next = obj->head->next;
50+
new_node->prev = obj->head;
51+
obj->head->next = new_node;
52+
if (new_node->next != NULL)
53+
new_node->next->prev = new_node;
54+
else
55+
obj->tail = new_node;
56+
obj->length += 1;
57+
return;
58+
}
59+
60+
void myLinkedListAddAtTail(MyLinkedList* obj, int val) {
61+
Node* new_node = createNode(val);
62+
obj->tail->next = new_node;
63+
new_node->prev = obj->tail;
64+
obj->tail = obj->tail->next;
65+
obj->length += 1;
66+
return;
67+
}
68+
69+
void myLinkedListAddAtIndex(MyLinkedList* obj, int index, int val) {
70+
if (index == 0) {
71+
myLinkedListAddAtHead(obj, val);
72+
return;
73+
}
74+
else if (index == obj->length) {
75+
myLinkedListAddAtTail(obj, val);
76+
return;
77+
}
78+
Node* prev_node = traverseList(obj, index - 1);
79+
if (prev_node == NULL)
80+
return;
81+
Node* new_node = createNode(val);
82+
new_node->next = prev_node->next;
83+
new_node->prev = prev_node;
84+
prev_node->next = new_node;
85+
if (new_node->next != NULL)
86+
new_node->next->prev = new_node;
87+
obj->length += 1;
88+
return;
89+
}
90+
91+
void myLinkedListDeleteAtIndex(MyLinkedList* obj, int index) {
92+
Node *node = traverseList(obj, index);
93+
if (node == NULL)
94+
return;
95+
Node *temp = node;
96+
node->prev->next = node->next;
97+
if (node->next != NULL)
98+
node->next->prev = node->prev;
99+
else
100+
obj->tail = node->prev;
101+
obj->length -= 1;
102+
free(temp);
103+
return;
104+
}
105+
106+
void myLinkedListFree(MyLinkedList* obj) {
107+
Node *prev = NULL, *cur = obj->head;
108+
while (cur != NULL) {
109+
prev = cur;
110+
cur = cur->next;
111+
free(prev);
112+
}
113+
free(obj);
114+
}
115+
116+
/**
117+
* Your MyLinkedList struct will be instantiated and called as such:
118+
* MyLinkedList* obj = myLinkedListCreate();
119+
* int param_1 = myLinkedListGet(obj, index);
120+
121+
* myLinkedListAddAtHead(obj, val);
122+
123+
* myLinkedListAddAtTail(obj, val);
124+
125+
* myLinkedListAddAtIndex(obj, index, val);
126+
127+
* myLinkedListDeleteAtIndex(obj, index);
128+
129+
* myLinkedListFree(obj);
130+
*/

cpp/0018-4sum.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Time Complexity = O(n^3)
2+
// Space Complexity = O(n)
3+
4+
class Solution {
5+
public:
6+
vector<vector<int>> fourSum(vector<int>& nums, int target) {
7+
sort(nums.begin(), nums.end());
8+
9+
vector<vector<int> > res;
10+
int n = nums.size();
11+
12+
for (int i = 0; i < n; i++) {
13+
if (i > 0 && nums[i] == nums[i - 1])
14+
continue;
15+
for (int j = i + 1; j < n; j++) {
16+
if (j > (i + 1) && nums[j] == nums[j - 1])
17+
continue;
18+
int l = j + 1, r = n - 1;
19+
while (l < r) {
20+
long sm = (long)nums[i] + (long)nums[j] + (long)nums[l] + (long)nums[r];
21+
if (sm == target) {
22+
res.push_back(vector<int>{nums[i], nums[j], nums[l], nums[r]});
23+
l += 1;
24+
while (l < r && nums[l] == nums[l - 1])
25+
l += 1;
26+
}
27+
else if (sm > target)
28+
r -= 1;
29+
else
30+
l += 1;
31+
}
32+
}
33+
}
34+
return res;
35+
}
36+
};

cpp/0061-rotate-list.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
Given the head of a linked list, rotate the list to the right by k places.
3+
Example: list = [1,2,3,4,5] and k = 2
4+
Output: [4,5,1,2,3]
5+
6+
Time complexity: O(n)
7+
Space complexity: O(1)
8+
*/
9+
10+
class Solution {
11+
private:
12+
int findLen(ListNode* head){
13+
int len = 0;
14+
while(head!=NULL){
15+
len++;
16+
head = head->next;
17+
}
18+
return len;
19+
}
20+
ListNode* findNewHead(ListNode* head,int k){
21+
int i=0;
22+
while(i+1<k){
23+
i++;
24+
head = head->next;
25+
}
26+
ListNode* ret = head->next;
27+
head->next = NULL;
28+
return ret;
29+
}
30+
ListNode* findLast(ListNode* head){
31+
while(head->next!=NULL) head = head->next;
32+
return head;
33+
}
34+
public:
35+
ListNode* rotateRight(ListNode* head, int k) {
36+
int len = findLen(head); // Finds the length of the Linked List
37+
if(len==0) return head;
38+
k = k%len;
39+
if(k==0) return head;
40+
ListNode* newHead = findNewHead(head,len-k); // Finds the node that is the new head
41+
ListNode* last = findLast(newHead); // Finds the last node from the new head and connects it to the previous head
42+
last->next = head;
43+
return newHead;
44+
}
45+
};

0 commit comments

Comments
 (0)