728x90
Above is the link to the problem.
Given a sorted array of distinct integers and a target value, return the index if the target is found.
If not, return the index where it would be if it were inserted in order.
Write an algorithm with O(log n) runtime complexity.
For example,
Input: nums = [1,3,5,6], target = 5
Output: 2
Input: nums = [1,3,5,6], target = 2
Output: 1
I solved this problem by writing the code below.
If the target doesn't exist in nums, then append the target and resort nums.
If the target exists in nums, no process is necessary.
Finally, return the index number of the target.
This code gets the top 8.62% of time efficiency and the top 17.24% of space efficiency.
728x90
반응형
'Code > LeetCode' 카테고리의 다른 글
[LeetCode] 58. Length of Last Word (Easy/Python) (0) | 2022.09.07 |
---|---|
[LeetCode] 48. Rotate Image (Medium/Python) (0) | 2022.09.05 |
[LeetCode] 20.Valid Parentheses (Easy/Python) (0) | 2022.09.02 |
[LeetCode] 13. Roman to Integer (Easy/Python) (0) | 2022.08.31 |
[LeetCode] 9.Palindrome Number (Easy/Python3) (0) | 2022.08.30 |