rotatedArraySearch

Written on July 25, 2019

문제

Given a sorted array that has been rotated some number of items right or left, i.e. [0, 1, 2, 3, 4, 5, 6, 7] might become [4, 5, 6, 7, 0, 1, 2, 3] how can you efficiently find an element? For simplicity, you can assume that there are no duplicate elements in the array.

rotatedArraySearch should return the index of the element if it is in the array and should return null otherwise.

풀이

var rotatedArraySearch = function(rotated, target) {
  if (rotated[0] === target) {
    return 0;
  }
  if (rotated.length - Math.abs(target - rotated[0]) > 0) {
    if (rotated[0] < target) {
      return target - rotated[0];
    }
    if (rotated[0] > target) {
      return rotated.length - Math.abs(target - rotated[0]);
    }
  }
  return null;
};

👩🏻‍💻 배우는 것을 즐기는 프론트엔드 개발자 입니다
부족한 블로그에 방문해 주셔서 감사합니다 🙇🏻‍♀️

in the process of becoming the best version of myself