File tree 1 file changed +83
-5
lines changed
1 file changed +83
-5
lines changed Original file line number Diff line number Diff line change 37
37
![ ] ( ../Animation/Animation.gif )
38
38
39
39
### 代码实现
40
-
40
+ #### C++
41
41
```
42
42
// 1. Two Sum
43
43
// https://leetcode.com/problems/two-sum/description/
@@ -62,9 +62,87 @@ public:
62
62
};
63
63
64
64
```
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
+ ```
69
147
70
148

You can’t perform that action at this time.
0 commit comments