aboutsummaryrefslogtreecommitdiffstats
path: root/leetcode/0001/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'leetcode/0001/main.c')
-rw-r--r--leetcode/0001/main.c24
1 files changed, 24 insertions, 0 deletions
diff --git a/leetcode/0001/main.c b/leetcode/0001/main.c
new file mode 100644
index 0000000..d847e1b
--- /dev/null
+++ b/leetcode/0001/main.c
@@ -0,0 +1,24 @@
+/**
+ * Note: The returned array must be malloced, assume caller calls free().
+ * 这是一个暴力解法
+ */
+#include <stdio.h>
+#include <stdlib.h>
+
+int *twoSum(int *nums, int numsSize, int target, int *returnSize) {
+ int nums_max_idx = numsSize - 1;
+ for (int i = 0; i < nums_max_idx; i++) {
+ for (int j = i + 1; j <= nums_max_idx; j++) {
+ if (nums[i] + nums[j] == target) {
+ int *rtn = malloc(sizeof(int) * 2);
+ rtn[0] = i;
+ rtn[1] = j;
+ *returnSize = 2;
+ return rtn;
+ }
+ }
+ }
+
+ *returnSize = 0;
+ return NULL;
+}