Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 62658cc

Browse files
Merge pull request MisterBooo#108 from ztianming/patch-1
Update 0001-Two-Sum.md
2 parents 3559b30 + d2b72e5 commit 62658cc

File tree

1 file changed

+83
-5
lines changed

1 file changed

+83
-5
lines changed

‎0001-Two-Sum/Article/0001-Two-Sum.md‎

Lines changed: 83 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
![](../Animation/Animation.gif)
3838

3939
### 代码实现
40-
40+
#### C++
4141
```
4242
// 1. Two Sum
4343
// https://leetcode.com/problems/two-sum/description/
@@ -62,9 +62,87 @@ public:
6262
};
6363
6464
```
65-
66-
67-
68-
65+
#### C
66+
```c
67+
// 1. Two Sum
68+
// https://leetcode.com/problems/two-sum/description/
69+
// 时间复杂度:O(n)
70+
// 空间复杂度:O(n)
71+
/**
72+
* Note: The returned array must be malloced, assume caller calls free().
73+
*/
74+
int* twoSum(int* nums, int numsSize, int target, int* returnSize){
75+
int *ans=(int *)malloc(2 * sizeof(int));
76+
int i,j;
77+
bool flag=false;
78+
for(i=0;i<numsSize-1;i++)
79+
{
80+
for(j=i+1;j<numsSize;j++)
81+
{
82+
if(nums[i]+nums[j] == target)
83+
{
84+
ans[0]=i;
85+
ans[1]=j;
86+
flag=true;
87+
}
88+
}
89+
}
90+
if(flag){
91+
*returnSize = 2;
92+
}
93+
else{
94+
*returnSize = 0;
95+
}
96+
return ans;
97+
}
98+
```
99+
#### Java
100+
```
101+
// 1. Two Sum
102+
// https://leetcode.com/problems/two-sum/description/
103+
// 时间复杂度:O(n)
104+
// 空间复杂度:O(n)
105+
class Solution {
106+
public int[] twoSum(int[] nums, int target) {
107+
int l = nums.length;
108+
int[] ans=new int[2];
109+
int i,j;
110+
for(i=0;i<l-1;i++)
111+
{
112+
for(j=i+1;j<l;j++)
113+
{
114+
if(nums[i]+nums[j] == target)
115+
{
116+
ans[0]=i;
117+
ans[1]=j;
118+
}
119+
}
120+
}
121+
122+
return ans;
123+
124+
}
125+
}
126+
```
127+
#### Python
128+
```
129+
# 1. Two Sum
130+
# https://leetcode.com/problems/two-sum/description/
131+
# 时间复杂度:O(n)
132+
# 空间复杂度:O(n)
133+
class Solution(object):
134+
def twoSum(self, nums, target):
135+
l = len(nums)
136+
print(nums)
137+
ans=[]
138+
for i in range(l-1):
139+
for j in range(i+1,l):
140+
if nums[i]+nums[j] == target:
141+
ans.append(i)
142+
ans.append(j)
143+
print([i,j])
144+
break
145+
return ans
146+
```
69147
70148
![](../../Pictures/qrcode.jpg)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /