File tree Expand file tree Collapse file tree 1 file changed +51
-1
lines changed Expand file tree Collapse file tree 1 file changed +51
-1
lines changed Original file line number Diff line number Diff line change @@ -1127,4 +1127,54 @@ var findDuplicate = function (nums) {
11271127};
11281128// o(n)
11291129// o(1)
1130- ```
1130+ ```
1131+
1132+ ## 二分查找
1133+ ### [ 搜索插入位置] ( https://leetcode.cn/problems/search-insert-position/description/?envType=study-plan-v2&envId=top-100-liked )
1134+ ``` js
1135+ var searchInsert = function (nums , target ) {
1136+ let left = 0 ;
1137+ let right = nums .length - 1 ;
1138+ while (left <= right) {
1139+ let mid = Math .floor ((left + right) / 2 );
1140+ if (nums[mid] === target) {
1141+ return mid;
1142+ }
1143+ if (target < nums[mid]) {
1144+ right = mid - 1 ;
1145+ } else {
1146+ left = mid + 1 ;
1147+ }
1148+ }
1149+ return left;
1150+ };
1151+ ```
1152+ ### [ 搜索旋转排序数组] ( https://leetcode.cn/problems/search-in-rotated-sorted-array/description/ )
1153+ 每次二分后,子数组 [ left, mid] 或 [ mid, right] 至少有一个是有序的
1154+ ``` js
1155+ var search = function (nums , target ) {
1156+ let left = 0 ;
1157+ let right = nums .length - 1 ;
1158+ while (left <= right) {// 子数组长度为 1
1159+ let mid = Math .floor ((left + right) / 2 );
1160+ if (nums[mid] === target) {
1161+ return mid;
1162+ }
1163+ if (nums[left] <= nums[mid]) {// 不加=会以为右边是有序的
1164+ if (target >= nums[left] && target <= nums[mid]) {
1165+ right = mid - 1 ;
1166+ } else {
1167+ left = mid + 1 ;
1168+ }
1169+ } else {
1170+ if (target >= nums[mid] && target <= nums[right]) {
1171+ left = mid + 1 ;
1172+ } else {
1173+ right = mid - 1 ;
1174+ }
1175+ }
1176+ }
1177+ return - 1 ;
1178+ };
1179+ ```
1180+ ## 快速排序
You can’t perform that action at this time.
0 commit comments