Skip to content

Commit 1b65318

Browse files
committed
Added C solutions
1 parent 1b66125 commit 1b65318

File tree

210 files changed

+9058
-383
lines changed

Some content is hidden

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

210 files changed

+9058
-383
lines changed

README.md

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

pom.xml

Lines changed: 3 additions & 2 deletions
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.4</version>
8+
<version>1.5</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>
@@ -93,6 +93,7 @@
9393
<source>src/main/elixir</source>
9494
<source>src/main/rust</source>
9595
<source>src/main/dart</source>
96+
<source>src/main/c</source>
9697
</sources>
9798
</configuration>
9899
</execution>
@@ -131,7 +132,7 @@
131132
<plugin>
132133
<groupId>org.apache.maven.plugins</groupId>
133134
<artifactId>maven-gpg-plugin</artifactId>
134-
<version>3.2.7</version>
135+
<version>1.6</version>
135136
<executions>
136137
<execution>
137138
<id>sign-artifacts</id>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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_10_16_Time_2_ms_(99.56%)_Space_8.6_MB_(40.96%)
4+
5+
/**
6+
* Note: The returned array must be malloced, assume caller calls free().
7+
*/
8+
int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
9+
returnSize[0] = 2;
10+
int* output = (int*)malloc(sizeof(int) * 2);
11+
12+
for (int offset = 1; offset < numsSize; offset++) {
13+
int i = 0;
14+
while (i + offset < numsSize) {
15+
if (nums[i] + nums[i + offset] == target) {
16+
output[0] = i;
17+
output[1] = i + offset;
18+
return output;
19+
}
20+
i++;
21+
}
22+
}
23+
return (void*)0;
24+
}
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: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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_10_16_Time_12_ms_(70.50%)_Space_12.6_MB_(56.73%)
5+
6+
/**
7+
* Definition for singly-linked list.
8+
* struct ListNode {
9+
* int val;
10+
* struct ListNode *next;
11+
* };
12+
*/
13+
// Function to create a new ListNode
14+
struct ListNode* createNode(int val) {
15+
struct ListNode* newNode = (struct ListNode*)malloc(sizeof(struct ListNode));
16+
newNode->val = val;
17+
newNode->next = NULL;
18+
return newNode;
19+
}
20+
21+
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
22+
struct ListNode dummyHead;
23+
dummyHead.val = 0;
24+
dummyHead.next = NULL;
25+
26+
struct ListNode* p = l1;
27+
struct ListNode* q = l2;
28+
struct ListNode* curr = &dummyHead;
29+
int carry = 0;
30+
31+
while (p != NULL || q != NULL) {
32+
int x = (p != NULL) ? p->val : 0;
33+
int y = (q != NULL) ? q->val : 0;
34+
int sum = carry + x + y;
35+
carry = sum / 10;
36+
37+
curr->next = createNode(sum % 10);
38+
curr = curr->next;
39+
40+
if (p != NULL) {
41+
p = p->next;
42+
}
43+
if (q != NULL) {
44+
q = q->next;
45+
}
46+
}
47+
48+
if (carry > 0) {
49+
curr->next = createNode(carry);
50+
}
51+
52+
return dummyHead.next;
53+
}
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: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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_10_20_Time_15_ms_(29.93%)_Space_9.7_MB_(47.88%)
4+
5+
#include <stdio.h>
6+
#include <string.h>
7+
8+
int lengthOfLongestSubstring(const char* s) {
9+
int lastIndices[256];
10+
for (int i = 0; i < 256; i++) {
11+
lastIndices[i] = -1;
12+
}
13+
14+
int maxLen = 0;
15+
int curLen = 0;
16+
int start = 0;
17+
18+
for (int i = 0; i < strlen(s); i++) {
19+
char cur = s[i];
20+
if (lastIndices[(unsigned char)cur] < start) {
21+
lastIndices[(unsigned char)cur] = i;
22+
curLen++;
23+
} else {
24+
int lastIndex = lastIndices[(unsigned char)cur];
25+
start = lastIndex + 1;
26+
curLen = i - start + 1;
27+
lastIndices[(unsigned char)cur] = i;
28+
}
29+
30+
if (curLen > maxLen) {
31+
maxLen = curLen;
32+
}
33+
}
34+
35+
return maxLen;
36+
}
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: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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) #2024_10_20_Time_0_ms_(100.00%)_Space_11.1_MB_(60.94%)
3+
4+
#include <stdio.h>
5+
#include <limits.h>
6+
7+
double findMedianSortedArrays(int* nums1, int n1, int* nums2, int n2) {
8+
if (n1 > n2) {
9+
return findMedianSortedArrays(nums2, n2, nums1, n1);
10+
}
11+
12+
int low = 0, high = n1;
13+
while (low <= high) {
14+
int cut1 = (low + high) / 2;
15+
int cut2 = (n1 + n2 + 1) / 2 - cut1;
16+
17+
int l1 = (cut1 == 0) ? INT_MIN : nums1[cut1 - 1];
18+
int l2 = (cut2 == 0) ? INT_MIN : nums2[cut2 - 1];
19+
int r1 = (cut1 == n1) ? INT_MAX : nums1[cut1];
20+
int r2 = (cut2 == n2) ? INT_MAX : nums2[cut2];
21+
22+
if (l1 <= r2 && l2 <= r1) {
23+
if ((n1 + n2) % 2 == 0) {
24+
return (double)(fmax(l1, l2) + fmin(r1, r2)) / 2.0;
25+
} else {
26+
return (double)fmax(l1, l2);
27+
}
28+
} else if (l1 > r2) {
29+
high = cut1 - 1;
30+
} else {
31+
low = cut1 + 1;
32+
}
33+
}
34+
35+
return 0.0;
36+
}
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)