Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
wwwanlingxiao
LeetCodeAnimation
Commits
129f9a8f
Unverified
Commit
129f9a8f
authored
Jul 29, 2020
by
Zong
Committed by
GitHub
Jul 29, 2020
Browse files
Update 0167-Two-Sum-II-Input-array-is-sorted.md
添加Java、Python代码实现
parent
3daa1877
Changes
1
Hide whitespace changes
Inline
Side-by-side
0167-Two-Sum-II-Input-array-is-sorted/Article/0167-Two-Sum-II-Input-array-is-sorted.md
View file @
129f9a8f
...
...
@@ -40,8 +40,8 @@

### 代码实现
```
#### C++
```
c++
// 对撞指针
// 时间复杂度: O(n)
// 空间复杂度: O(1)
...
...
@@ -63,6 +63,50 @@ public:
```
#### Java
```
java
class
Solution
{
public
int
[]
twoSum
(
int
[]
numbers
,
int
target
)
{
int
n
=
numbers
.
length
;
int
left
=
0
;
int
right
=
n
-
1
;
while
(
left
<=
right
)
{
if
(
numbers
[
left
]
+
numbers
[
right
]
==
target
)
{
return
new
int
[]{
left
+
1
,
right
+
1
};
}
else
if
(
numbers
[
left
]
+
numbers
[
right
]
>
target
)
{
right
--;
}
else
{
left
++;
}
}
return
new
int
[]{-
1
,
-
1
};
}
}
```
#### Python
```
python
class
Solution
(
object
):
def
twoSum
(
self
,
numbers
,
target
):
n
=
len
(
numbers
)
left
,
right
=
0
,
n
-
1
while
left
<=
right
:
if
numbers
[
left
]
+
numbers
[
right
]
==
target
:
return
[
left
+
1
,
right
+
1
]
elif
numbers
[
left
]
+
numbers
[
right
]
>
target
:
right
-=
1
else
:
left
+=
1
return
[
-
1
,
-
1
]
```
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment