[LeetCode]1. Two Sum
題目
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
方法
對陣列排序後,第一個數字從陣列開始遍歷,二分查詢找到滿足sum == target
的第二個數字,返回2個數字的位置。
由於排序導致數字位置發生了變換,因此需要一個陣列記錄變化後第i個數字之前的位置numsLocation[i]。
C程式碼
#include <assert.h>
#include <stdlib.h>
int* sort(int* nums, int numsSize) {
int *numsLocations = (int *)malloc(sizeof(int) * numsSize);
int i = 0, j = 0;
for(i = 0; i < numsSize; i++)
numsLocations[i] = i;
for(i = 0; i < numsSize; i++) {
int sorted = 1;
for(j = 0; j < numsSize-1; j++) {
if(nums[j] > nums[j+1]) {
int temp = nums[j];
nums[j] = nums[j+1];
nums[j+1] = temp;
sorted = 0;
temp = numsLocations[j];
numsLocations[j] = numsLocations[j+1];
numsLocations[j+1] = temp;
}
}
if(sorted)
break;
}
return numsLocations;
}
int find(int* nums, int num, int start, int end) {
if(start > end)
return -1;
int i = (start+end)/2;
if(num > nums[i])
return find(nums, num, i+1, end);
else if(num < nums[i])
return find(nums, num, start, i-1);
return i;
}
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* twoSum(int* nums, int numsSize, int target) {
int *numsLocations = sort(nums, numsSize);
int i = 0;
int num;
int *locations = (int *)malloc(sizeof(int) * 2);
int j;
for(i = 0; i < numsSize; i++) {
num = nums[i];
if((j=find(nums, target-num, i+1, numsSize-1)) >= 0) {
locations[1] = numsLocations[j];
locations[0] = numsLocations[i];
return locations;
}
}
return NULL;
}
int main() {
int nums[4] = {2, 7, 11, 15};
int *locations = twoSum(nums, 4, 9);
assert(locations[0] == 0);
assert(locations[1] == 1);
int nums2[3] = {5, 75, 25};
locations = twoSum(nums2, 3, 100);
assert(locations[0] == 2);
assert(locations[1] == 1);
return 0;
}
相關文章
- Leetcode Two SumLeetCode
- Leetcode 1 two sumLeetCode
- Leetcode-Two SumLeetCode
- Two Sum leetcode javaLeetCodeJava
- LeetCode | 1 Two SumLeetCode
- [LeetCode]1.Two SumLeetCode
- LeetCode----1. Two SumLeetCode
- LeetCode1:Two SumLeetCode
- LeetCode OJ : 1 Two SumLeetCode
- LeetCode-1 Two SumLeetCode
- LeetCode: Two sum(兩數之和)LeetCode
- python: leetcode - 1 Two SumPythonLeetCode
- LeetCode Problem-Sum of Two IntegersLeetCode
- [leetCode][013] Two Sum 2LeetCode
- [LeetCode] Two Sum 兩數之和LeetCode
- LeetCode #1:Two Sum(簡單題)LeetCode
- leetcode 371. Sum of Two IntegersLeetCode
- [leetCode][012] Two Sum (1)LeetCode
- Leetcode-1 Two Sum(Java) -by zzyLeetCodeJava
- LeetCode Two Sum(001)解法總結LeetCode
- python leetcode 之兩數之和(two sum)PythonLeetCode
- LeetCode-Two Sum III - Data structure designLeetCodeStruct
- 371. Sum of Two Integers--LeetCode RecordLeetCode
- 【Leetcode】167. Two Sum II - Input array is sortedLeetCode
- 【LeetCode 1_陣列_雜湊表】Two SumLeetCode陣列
- LeetCode-two sum:python解答陣列問題LeetCodePython陣列
- LeetCode 之 JavaScript 解答第一題 —— 兩數之和(Two Sum)LeetCodeJavaScript
- 653-Two Sum IV - Input is a BST
- JavaScript的two-sum問題解法JavaScript
- Leetcode Power of TwoLeetCode
- Leetcode Path SumLeetCode
- leetcode Sum系列LeetCode
- leetcode15&16_3Sum&4SumLeetCode
- Leetcode 3SumLeetCode
- Leetcode 4SumLeetCode
- Leetcode Path Sum IILeetCode
- Leetcode-Path SumLeetCode
- Leetcode-3SumLeetCode