From 3bebdf81fb700a181041642ba1044a142d32d85e Mon Sep 17 00:00:00 2001 From: HamSungJun Date: Mon, 21 Jun 2021 21:22:48 +0900 Subject: [PATCH] HamSungJun-392,704,852 --- HamSungJun/BinarySearch/Solution_392.ts | 11 +++++++++++ HamSungJun/BinarySearch/Solution_704.ts | 15 +++++++++++++++ HamSungJun/BinarySearch/Solution_852.ts | 8 ++++++++ 3 files changed, 34 insertions(+) create mode 100644 HamSungJun/BinarySearch/Solution_392.ts create mode 100644 HamSungJun/BinarySearch/Solution_704.ts create mode 100644 HamSungJun/BinarySearch/Solution_852.ts diff --git a/HamSungJun/BinarySearch/Solution_392.ts b/HamSungJun/BinarySearch/Solution_392.ts new file mode 100644 index 0000000..0a5ff3a --- /dev/null +++ b/HamSungJun/BinarySearch/Solution_392.ts @@ -0,0 +1,11 @@ +function isSubsequence (s: string, t: string): boolean { + let ps = 0 + let pt = 0 + while (pt < t.length) { + if (s[ps] === t[pt]) { + ps += 1 + } + pt += 1 + } + return ps > s.length - 1 +}; diff --git a/HamSungJun/BinarySearch/Solution_704.ts b/HamSungJun/BinarySearch/Solution_704.ts new file mode 100644 index 0000000..a3937ab --- /dev/null +++ b/HamSungJun/BinarySearch/Solution_704.ts @@ -0,0 +1,15 @@ +function search (nums: number[], target: number): number { + let left = 0 + let right = nums.length - 1 + while (left <= right) { + const mid = Math.floor((left + right) / 2) + if (nums[mid] === target) { + return mid + } else if (nums[mid] < target) { + left = mid + 1 + } else if (nums[mid] > target) { + right = mid - 1 + } + } + return -1 +}; diff --git a/HamSungJun/BinarySearch/Solution_852.ts b/HamSungJun/BinarySearch/Solution_852.ts new file mode 100644 index 0000000..2e0b1ac --- /dev/null +++ b/HamSungJun/BinarySearch/Solution_852.ts @@ -0,0 +1,8 @@ +function peakIndexInMountainArray (arr: number[]): number { + for (let i = 1; i < arr.length; i++) { + if (arr[i - 1] < arr[i] && arr[i] > arr[i + 1]) { + return i + } + } + return -1 +};