LeetCode解题笔记1 Two Sum
2018-02-01 18:21
489 查看
题目:
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, and you may not use the same element twice.
Example:
python解法一:
def twoSum(self, nums, target):
for i in range(len(nums)) :
for j in range(1,len(nums)-i) :
if nums[i] + nums[i+j]==target :
return i,j+i
C语言解法一:
int* twoSum(int* nums, int numsSize, int target) {
int i,j;
static int res[2];
for (i=0; i<numsSize; i++) {
for (j=i+1; j<numsSize; j++) {
if (nums[i]+nums[j]==target) {
res[0]=i;
res[1]=j;
break;
} else {
continue;
}
}
}
return res;
}
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, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].
python解法一:
def twoSum(self, nums, target):
for i in range(len(nums)) :
for j in range(1,len(nums)-i) :
if nums[i] + nums[i+j]==target :
return i,j+i
C语言解法一:
int* twoSum(int* nums, int numsSize, int target) {
int i,j;
static int res[2];
for (i=0; i<numsSize; i++) {
for (j=i+1; j<numsSize; j++) {
if (nums[i]+nums[j]==target) {
res[0]=i;
res[1]=j;
break;
} else {
continue;
}
}
}
return res;
}
相关文章推荐
- Leetcode解题笔记(string)
- leetcode解题笔记-Trapping Rain Water
- 123. Best Time to Buy and Sell Stock (三) leetcode解题笔记
- leetcode解题笔记:Regular Expression Matching
- Leetcode解题笔记 63. Unique Paths II [Medium] 动态规划
- Leetcode解题笔记 1.Two Sum [Easy]
- LeetCode 1 Two Sum 解题报告
- LeetCode笔记:1. Two Sum
- Leetcode解题笔记-Merge sorted Array
- leetcode解题笔记-Text Justification
- leetcode解题笔记:84. Largest Rectangle in Histogram
- LeetCode解题笔记136 Single Number
- [Leetcode] [Database] Employees Earning More Than Their Managers解题笔记
- Leetcode解题笔记(Array)
- LeetCode-Two Sum-解题报告
- Leetcode解题笔记 70. Climbing Stairs [Easy] 动态规划
- LeetCode解题笔记2 Add Two Numbers
- Leetcode解题笔记-Combination Sum
- Leetcode解题笔记,basic calculator 1&&basic calculator 2
- 【LeetCode】Reverse Integer解题笔记