Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
anqiwa
myLeetcodeSolu
Commits
65a15124
Commit
65a15124
authored
Dec 02, 2021
by
anqiwa
😲
Browse files
added 35 code
parent
85c66a1e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Binary Search/34. Find First and Last Position of Element in Sorted Array/Readme.md
View file @
65a15124
# 34. Find First and Last Position of Element in Sorted Array
此题好
像
但是难写。核心无非是写两遍二分搜索,找元素的第一个和左后一个的位置,但是很难BugFree。所以这题用来锻炼二分搜索很好。我一个朋友面某faang的ng就遇到了这个题。
此题好
想
但是难写。核心无非是写两遍二分搜索,找元素的第一个和左后一个的位置,但是很难BugFree。所以这题用来锻炼二分搜索很好。我一个朋友面某faang的ng就遇到了这个题。
## 核心要点:
...
...
Binary Search/35. Search Insert Position/35. Search Insert Position.cpp
0 → 100644
View file @
65a15124
class
Solution
{
public:
int
searchInsert
(
vector
<
int
>
&
nums
,
int
target
)
{
int
l
=
0
;
int
r
=
nums
.
size
();
while
(
l
<
r
)
{
int
mid
=
l
+
(
r
-
l
)
/
2
;
// [0,1] -> 0
if
(
nums
[
mid
]
<
target
)
{
l
=
mid
+
1
;
}
else
if
(
nums
[
mid
]
>
target
)
{
r
=
mid
;
}
else
{
return
mid
;
}
}
return
r
;
}
};
\ No newline at end of file
Write
Preview
Markdown
is supported
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