Skip to content

Commit 919c528

Browse files
committedNov 9, 2024·
Added C solutions
1 parent ad803ac commit 919c528

File tree

105 files changed

+9475
-380
lines changed

Some content is hidden

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

105 files changed

+9475
-380
lines changed
 

‎README.md

+380-380
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-All?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-All)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-All?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-All/fork)
3+
4+
## 1\. Two Sum
5+
6+
Easy
7+
8+
Given an array of integers `nums` and an integer `target`, return _indices of the two numbers such that they add up to `target`_.
9+
10+
You may assume that each input would have **_exactly_ one solution**, and you may not use the _same_ element twice.
11+
12+
You can return the answer in any order.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [2,7,11,15], target = 9
17+
18+
**Output:** [0,1]
19+
20+
**Output:** Because nums[0] + nums[1] == 9, we return [0, 1].
21+
22+
**Example 2:**
23+
24+
**Input:** nums = [3,2,4], target = 6
25+
26+
**Output:** [1,2]
27+
28+
**Example 3:**
29+
30+
**Input:** nums = [3,3], target = 6
31+
32+
**Output:** [0,1]
33+
34+
**Constraints:**
35+
36+
* <code>2 <= nums.length <= 10<sup>4</sup></code>
37+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
38+
* <code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code>
39+
* **Only one valid answer exists.**
40+
41+
**Follow-up:** Can you come up with an algorithm that is less than <code>O(n<sup>2</sup>) </code>time complexity?
42+
43+
## Solution
44+
45+
```c
46+
/**
47+
* Note: The returned array must be malloced, assume caller calls free().
48+
*/
49+
int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
50+
returnSize[0] = 2;
51+
int* output = (int*)malloc(sizeof(int) * 2);
52+
53+
for (int offset = 1; offset < numsSize; offset++) {
54+
int i = 0;
55+
while (i + offset < numsSize) {
56+
if (nums[i] + nums[i + offset] == target) {
57+
output[0] = i;
58+
output[1] = i + offset;
59+
return output;
60+
}
61+
i++;
62+
}
63+
}
64+
return (void*)0;
65+
}
66+
```

0 commit comments

Comments
 (0)
Please sign in to comment.