# Algorithm problem

Given an integer array nums arranged in ascending order and a target value target. Find the start and end positions of the given target value in the array.

If the target value target does not exist in the array, return [- 1, - 1].

Can you design and implement an algorithm with time complexity of O(log n) to solve this problem?

```Example 1:

Input: nums = [5,7,7,8,8,10], target = 8
Output:[3,4]
```
```Example 2:

Input: nums = [5,7,7,8,8,10], target = 6
Output:[-1,-1]
```
```Example 3:

Input: nums = [], target = 0
Output:[-1,-1]
```
```Tips:

0 <= nums.length <= 105
-109 <= nums[i] <= 109
nums Is a non decreasing group
-109 <= target <= 109
```

## A little thought

This problem seems to be matched with yesterday's events. Yesterday, I introduced the common search algorithm, and I won't say it today. Today we still use the binary search method. If you don't have ideas or want to see how I implement it, you can look below.

## Source code and problem solving

```// Find left border
private int left_bound(int [] nums, int target) {

int low = 0, high = nums.length - 1;
while (low <= high) {

int mid = low + (high - low) / 2;
if (nums[mid] < target) {

low = mid + 1;
} else if (nums[mid] > target) {

high = mid - 1;
} else {

high = mid - 1;
}
}

```
```  // Finally, check the left out of bounds
if (low >= nums.length || nums[low] != target) return -1;
return low;
}
```
``` // Find right border
private int right_bound(int [] nums, int target) {

int low = 0, high = nums.length - 1;
while (low <= high) {

int mid = low + (high - low) / 2;
if (nums[mid] < target) {

low = mid + 1;
} else if (nums[mid] > target) {

high = mid - 1;
} else {

low = mid + 1;
}
}
```
```// Finally, check the right crossing
if (high < 0 || nums[high] != target) return -1;
return high;
}
```
``` public int[] searchRange(int[] nums, int target) {

int[] res = new int[] {-1,-1};
if (nums.length == 0 || nums == null) return res;

res[0] = left_bound(nums, target);
res[1] = right_bound(nums, target);
return res;
}
```

# Interview questions

## Hash table principle and test points

• Why hash tables are used: in the other two similar data structure arrays and linked lists in Java, they are either easy to read, inconvenient to 'Save', 'Delete', or just the opposite. The appearance of hash table just solves the characteristics of fast reading and storage. There are two kinds of hash tables in Java. hashmap and hashset, as I said before, you can look through the chat records.

• There is a hash function (hash function) in the hash table. That is to build a certain mapping, which can map keywords to a unique storage location. This mapping should be something we can calculate. Given the keyword, we should be able to calculate its address; On the contrary, given the address, we can retrieve the corresponding keyword. Once this relationship is established, given a keyword, I can directly use this mapping (the so-called hash function) to directly calculate its address and address it. This can greatly reduce the time spent determining the location of keyword storage.

• Common test sites are nothing more than how to solve hash conflicts, solve problems flexibly and master basic principles.

• Let's focus on how to solve hash conflicts: 1. Open Hashing. It refers to the array on which the hash table is based. Each position is the head node of a Linked List. In this way, the conflicting < key, value > tuples are placed in the same Linked List. 2. Closed Hashing means that when a conflict occurs, subsequent elements look for vacancies in the next position

# Special introduction

📣 Xiaobai training column is suitable for newcomers who have just started. Welcome to subscribe Programming Xiaobai advanced

📣 The interesting Python hand training project includes interesting articles such as robot awkward chat and spoof program, which can make you happy to learn python Training project column

📣 In addition, students who want to learn java web can take a look at this column: Teleporters

📣 This is an algorithm exercise for interview and postgraduate entrance examination. Let's refuel together The way ashore

In addition, I'm also preparing a four piece set of basic computer knowledge (computer network, computer composition principle, computer operating system and data structure). I'll send it together later. I'll learn it together at that time.

Posted on Fri, 19 Nov 2021 17:52:45 -0500 by Ty44ler