# Sword finger offer week 5 incomplete

Keywords: REST less

# Sword finger offer week 5 incomplete

## 73. Two numbers that only appear once in the array

Except for two numbers in an integer array, other numbers appear twice.

Please write a program to find out the two numbers that only appear once.

You can assume that these two numbers must exist.

#### Example

```Input: [1,2,3,3,4,4]

Output: [1,2]
```

Only two numbers appear once in the array, and the rest appear twice.

According to a ^ a = 0, if all the numbers in the array are exclusive or, the exclusive exclusive exclusive or of two numbers will be obtained;

Then group them according to the bits;

```class Solution {
public int[] findNumsAppearOnce(int[] nums) {
int res = 0;
for(int num : nums)res ^= num;
res &= (-res);

int[] ans = new int;
for(int i : nums){
if((i & res) == res) ans ^= i;
else ans ^= i;
}
return ans;
}
}
```

## 74. The only number in the array that only appears once

In an array, all numbers appear three times except one.

Please find out the number that only appears once.

You can assume that the number that meets the condition must exist.

Thinking questions:

• If only O(n)O(n) time and extra O(1)O(1) space are required, what should be done?

#### Example

```Input: [1,1,1,2,2,2,3,4,4,4]

Output: 3
```

Method 1: divide all numbers in nums array into different digits. One number appears once and the rest three times. The number of times other numbers appear on its digits must be three;

For example: 3-11, then 3 must appear 3 times at 0 and 1

In 5-101, the last digit appears six times, the middle three times, and the first three times; if there is only one number, the balance will be broken, and it is no longer a multiple of three

```class Solution {
public int findNumberAppearingOnce(int[] nums) {
// Record the number of 1 in each bit
int[] count = new int;
for(int num : nums){
for(int i = 0; i < 32; i ++){
count[i] += num & 1;
num >>>= 1;
}
}
int res = 0, m = 3;
// Take 1 from the bit whose occurrence times are not integral multiples of 3;
for(int i = 0; i < 32; i ++){
res <<= 1;
// If there is 4 or 1 level, this is 1 for res;
res |= count[31 - i] % m;
}
return res;
}
}
```

## 75. And are two numbers of S

Enter an array and a number s, and look up two numbers in the array so that their sum is exactly s.

If the sum of many pairs of numbers is equal to s, output any pair.

You can think of each input as having at least one set of outputs that meet the criteria.

#### Example

```Input: [1,2,3,4], sum = 7

Output: [3,4]
```

Using ma to store the number needed for each value

```class Solution {
public int[] findNumbersWithSum(int[] nums, int target) {
Map<Integer,Integer> map = new HashMap<>();
for(int num : nums){
int need = target - num;
Integer temp = map.get(need);
if(temp == null)map.put(num,need);
else return new int[] {need,temp};
}
return null;
}
}
```

## 76. Continuous positive sequence with sum as S

Enter a positive number s and print out all the continuous positive number sequences with sum s (at least two numbers).

For example, input 15, because 1 + 2 + 3 + 4 + 5 = 4 + 5 + 6 = 7 + 8 = 15, the result prints out three consecutive sequences 1-5, 4-6 and 7-8.

#### Example

```Input: 15

Output: [[1,2,3,4,5], [4,5,6], [7,8]]
```

Double pointer

```class Solution {
public List<List<Integer> > findContinuousSequence(int sum) {
int i = 1, total = 0;
List<List<Integer>> res = new ArrayList<>();
List<Integer> temp = new ArrayList<>();
for(int k = 1; k < sum; k ++){
total += k;
while(total > sum){
temp.remove((Integer)i);
total -= i;
i++;
}
}
return res;
}
}
```

## 77. Flip word order

Input an English sentence, turn the order of the words in the sentence, but the order of the characters in the word does not change.

For the sake of simplicity, punctuation is treated just like ordinary letters.

For example, if the input string "I am a student.", "student. a am I" is output.

#### Example

```Input: "I am a student."

Output: "student. a am I"
```
```class Solution {
public String reverseWords(String s) {
String[] str = s.trim().split(" ");
StringBuilder sb = new StringBuilder();
for(int i = str.length - 1; i > 0; i--){
if(str[i].trim().length() == 0) continue;
sb.append(str[i].trim()).append(" ");
}
sb.append(str.trim());
return sb.toString();
}
}
```

## 78. Left rotation string

The left rotation operation of a string transfers several characters in front of the string to the end of the string.

Please define a function to implement the left rotation operation of string.

For example, enter the string "abcdefg" and the number 2, and the function will return the result "cdefgab" obtained by rotating 2 bits to the left.

be careful:

• The data guarantees that n is less than or equal to the length of the input string.

#### Example

```Input: "ABCDEFG", n = 2

Output: "cdefgab"
```

Direct string

```class Solution {
public String leftRotateString(String str,int n) {
String temp = str.substring(0,n),res = str.substring(n);
res += temp;
return res;
}
}
```

Posted by pixy on Fri, 19 Jun 2020 21:35:32 -0700