From 2612b9214c8ece2746246f4a27afa4bad2853b0c Mon Sep 17 00:00:00 2001 From: xuzhiwei Date: Sat, 15 Jun 2019 22:02:52 +0900 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=8C=E5=88=86=E6=B3=95?= =?UTF-8?q?=E7=9A=84=E5=8F=98=E7=A7=8D=E5=BA=94=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- typescript/15_binarysearch/BinaryFind.ts | 115 +++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 typescript/15_binarysearch/BinaryFind.ts diff --git a/typescript/15_binarysearch/BinaryFind.ts b/typescript/15_binarysearch/BinaryFind.ts new file mode 100644 index 0000000..152e6a1 --- /dev/null +++ b/typescript/15_binarysearch/BinaryFind.ts @@ -0,0 +1,115 @@ +/** + * 二分查找法在实际的场合使用不多 + * 我们更多的是看到一些查找法的变种 + */ +class BinaryFind { + /** + * 在数组中查找第一个等于给定值的位置 + * @param array 给定的要查找的范围数组 + * @param target 要查找的目标值 + * @return 目标在数组中的位置 + */ + static findFirstElement(array: number[], target: number): number { + const length = array ? array.length : null + if (!length || length === 0) return -1 + let low = 0 + let high = length - 1 + while (low <= high) { + const mid = low + ((high - low) >> 2) + if (array[mid] > target) { + high = mid - 1 + } else if (array[mid] < target) { + low = mid + 1 + } else { + if (mid === 0 || array[mid - 1] !== target) { + return mid + } else { + high = mid - 1 + } + } + } + return -1 + } + + /** + * 在数组中查找最后一个等于给定值的位置 + * @param array 给定的要查找的范围数组 + * @param target 要查找的目标值 + */ + static findLastElement(array: number[], target: number): number { + const length = array ? array.length : null + if (!length || length === 0) return -1 + let low = 0 + let high = length - 1 + while (low <= high) { + const mid = low + ((high - low) >> 2) + if (array[mid] > target) { + high = mid - 1 + } else if (array[mid] < target) { + low = mid + 1 + // 这里已经是找到相等的元素了 + } else { + if (mid === length - 1 || array[mid + 1] !== target) { + return mid + } else { + low = mid + 1 + } + } + } + return -1 + } + + /** + * 在数组中查找第一个大于等于给定值的位置 + * @param array 给定的要查找的范围数组 + * @param target 要查找的目标值 + */ + static findFirstElementGreaterThanTarget(array: number[], target: number): number { + const length = array ? array.length : null + if (!length || length === 0) return -1 + let low = 0 + let high = length - 1 + while (low <= high) { + const mid = low + ((high - low) >> 2) + if (array[mid] < target) { + low = mid + 1 + } else { + if (mid === 0 || array[mid - 1] < target) { + return mid + } else { + high = mid - 1 + } + } + } + return -1 + } + + /** + * 在数组中查找最后一个小于等于给定值的位置 + * @param array 给定的要查找的范围数组 + * @param target 要查找的目标值 + */ + static findLastElementLessThanTarget(array: number[], target: number): number { + const length = array ? array.length : null + if (!length || length === 0) return -1 + let low = 0 + let high = length - 1 + while (low <= high) { + const mid = low + ((high - low) >> 2) + if (array[mid] > target) { + high = mid - 1 + } else { + if (mid === length - 1 || array[mid + 1] > target) { + return mid + } else { + low = mid + 1 + } + } + } + return -1 + } +} + +const binaryFindTest = [1, 3, 4, 4, 5, 6, 8, 8, 8, 11, 18] +const target = BinaryFind.findLastElementLessThanTarget(binaryFindTest, -1) +console.log(target)