---
# System prepended metadata

title: 題目

---

# [題目](https://leetcode.com/problems/binary-search)
## Liner Search 解法
```
class Solution {
    public int search(int[] nums, int target) {
        Arrays.sort(nums);
        for (var i =0; i < nums.length; i++) {
            if(nums[i] == target) {
                return i;
            }
        }
         return -1;
    }
}
```
## Binary Search 解法
```
class Solution {
    public int search(int[] nums, int target) {
        Arrays.sort(nums);
        int min  = 0;
        int max = nums.length -1;
        while (min <= max) {
            int middle = (min + max) / 2;
            if (target > nums[middle]) {
                min = middle + 1;
            } 
            else if (target < nums[middle]) {
                max = middle - 1;
            } 
            else {
                return middle;
            }
        }
         return -1;
    }
}
```
