# [leetcode question brushing day 34] 540 Unique element in array, 384 Disrupt array, 202 Happy number, 149 Maximum number of points on a line

Posted by atyndall on Mon, 14 Feb 2022 14:33:23 +0100

## Day 34

### 540 a single element in an ordered array

Give you an ordered array of integers, where each element will appear twice and only one number will appear once.

Please find and return the number that appears only once.

The solution you design must meet O(log n) time complexity and O(1) space complexity.

#### method

Since all elements in the array appear twice, only one element appears once. If we don't consider the element that appears only once, according to the subscript of the array, the element in even position always repeats the first element of the element, while the element in odd position always repeats the second element of the element. When we introduce this element that appears only once, after this element, the law becomes that the odd position is the first element of the repeated element, and the even position is the second element of the repeated element. Using this feature, we can use binary search to deal with this problem. When mid is even, let's compare whether mid and mid+1 are equal. If they are equal, it means that the only element that occurs once is behind mid, otherwise it is in front of mid; Similarly, when mid is an odd number, we can compare mid and mid-1. If the elements at mid and mid-1 are equal, it means that the only element that occurs once is behind mid, otherwise it is in front of mid.

```class Solution {
public int singleNonDuplicate(int[] nums) {
int l = 0, r = nums.length - 1;
while (l <= r){
int mid = (l + r) >> 1;
if ((mid & 1) == 1){
if (nums[mid - 1] == nums[mid]) l = mid + 1;
else r = mid - 1;
}
else{
if (mid + 1 < nums.length && nums[mid] == nums[mid + 1]) l = mid + 1;
else r = mid - 1;
}
}
return nums[l];
}
}
```

### 384 scramble array

Give you an integer array nums and design an algorithm to disrupt an array without duplicate elements. After scrambling, all permutations of the array should be possible.

Implement Solution class:

• Solution(int[] nums) initializes the object with an integer array nums

• int[] reset() resets the array to its initial state and returns

• int[] shuffle() returns the result of random array scrambling

#### method

For the number at each position, we generate a random number from 0 to length - 1, and then exchange it with the number at that position. We can do this for each number in the array.

```class Solution {

private int[] nums;
private int length;
private Random rand;

public Solution(int[] nums) {
this.nums = nums;
rand = new Random(2333);
}

public int[] reset() {
return nums;
}

public int[] shuffle() {
int[] res = nums.clone();
for (int i = 0; i < res.length; ++i){
int exchange = rand.nextInt(res.length);
int temp = 0;
temp = res[i];res[i] = res[exchange];res[exchange] = temp;
}
return res;
}
}
```

### 202 happy number

Write an algorithm to judge whether a number n is a happy number.

"Happy number" is defined as:

• For a positive integer, replace the number with the sum of the squares of the numbers at each position each time.

• Then repeat the process until the number becomes 1, or it may be an infinite loop, but it never becomes 1.

• If the result of this process is 1, then this number is the number of happiness.

If n is a happy number, return true; If not, false is returned.

#### method

Simulate the operation for 6 times. If it is not a happy number, return false, otherwise return true.

```class Solution {
public boolean isHappy(int n) {
int res = 0;
for (int i = 0; i < 6; ++i){
res = 0;
while (n > 0){
res += (n % 10) * (n % 10);
n /= 10;
}
n = res;
if (n == 1) return true;
}
return false;
}
}
```

### 149 maximum number of points on a line

Give you an array of points, where points[i] = [xi, yi] represents a point on the X-Y plane. Find the maximum number of points on the same line.

#### method

Just use the most direct method. We maintain a map of line - > int, which is used to record the number of points on each line and return the maximum value in the map.

```class Solution {
public int maxPoints(int[][] points) {
if (points.length == 1) return 1;
Map<Line, Integer> map = new HashMap<>();
for (int i = 0; i < points.length - 1; ++i){
for (int j =  i + 1; j < points.length; ++j){
int deltaA = points[i][0] - points[j][0];
int deltaB = points[i][1] - points[j][1];
if (deltaA == 0) {
Line k = new Line(0,Integer.MAX_VALUE, 0, points[i]);
map.put(k, map.getOrDefault(k, 1) + 1);
continue;
}
if (deltaB == 0){
Line k = new Line(0,0, Integer.MAX_VALUE, points[i]);
map.put(k, map.getOrDefault(k, 1) + 1);
continue;
}
int flag = getSign(deltaA, deltaB);
int common = GCD(Math.abs(deltaA), Math.abs(deltaB));
Line k = new Line(flag,Math.abs(deltaB) / common, Math.abs(deltaA) / common, points[i]);
map.put(k, map.getOrDefault(k, 1) + 1);
}
}
int res = 0;
for (int i : map.values()) res = Math.max(res, i);
return res;
}
public int GCD(int m, int n){
if (m % n == 0) return n;
return GCD(n, m % n);
}
public int getSign(int a, int b){
if (a * b < 0) return -1;
return 1;
}
}

class Point{
int x;int y;
Point(int[] a){x = a[0]; y = a[1];}
@Override public boolean equals(Object p){
Point cmp = (Point) p;
return cmp.x == x && cmp.y == y;
}
@Override public int hashCode(){return x | y;}
}

class Line{
Point startPoint;
int a;
int b;
int flag;
public Line(int f, int _a, int _b, int[] s){flag = f;a = _a;b = _b;startPoint = new Point(s);}
@Override
public boolean equals(Object k){
Line cmp = (Line)k;
return flag == cmp.flag && cmp.a == a && cmp.b == b && startPoint.equals(cmp.startPoint);
}
@Override
public int hashCode(){
return flag | a | b | startPoint.hashCode();
}
@Override
public String toString(){
return flag + startPoint.x + " " + startPoint.y + " " + a + "/" + b;
}
}
```