Skip to content

Instantly share code, notes, and snippets.

@making
Created February 3, 2014 01:13
Show Gist options
  • Save making/8777551 to your computer and use it in GitHub Desktop.
Save making/8777551 to your computer and use it in GitHub Desktop.
二分探索

vはleft以上、right未満の範囲に存在する

va[left] <= v < va[right]

function contaions(int v, int[] va) {
  if (va.length == 0) {
    return false;
  }
  int left = 0, int right = va.length;
  
  while (left + 1 < right) {
    int m = left + (right - left) / 2;
    int x = va[m];
    if (v >= x) {
      // va[m] <= v < va[right]
      left = m;
    } else {
      // va[left] <= v < v[m]
      right = m;
    }
  }
  return va[left] == v;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment