[ 問題: ]
given a sorted array 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. you may assume no duplicates in the array.
翻譯:給你一個排好序的數組和一個目标值,請找出目标值可以插入數組的位置。
[ 分析: ]
here are few examples.
[1,3,5,6], 5 → 2
[1,3,5,6], 2 → 1
[1,3,5,6], 7 → 4
[1,3,5,6], 0 → 0
注意:一定要考慮一些特殊情況,如數組為null等。
[ 解法: ]
①. 正常解法:從數組索引為0的位置開始找,時間複雜度為o(n),accepted
②. 二分查找:時間複雜度log2n
前提條件:一定是有序數組。