## 1.01^365=31.78 (Lucene、ES、ELK开发交流群: 370734940)

Engineers are versatile minds who create links between science, technology, and society.

# LeetCode数组习题

## 26.Remove Duplicates from Sorted Array

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this in place with constant mem- ory.
For example, Given input array A = [1,1,2],
Your function should return length = 2, and A is now [1,2].


一个有序数组，返回不重复的元素的个数。

Java代码

 public  int removeDuplicates(int[] nums) {

int index=0;
for(int i=1;i<nums.length;++i){
if (nums[i]!=nums[index]){
index=index+1;
nums[index]=nums[i];
}
}
return index+1;
}

O(n) O(1)

## 80. Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates":
What if duplicates are allowed at most twice?

For example,
Given sorted array nums = [1,1,1,2,2,3],

Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn't matter what you leave beyond the new length.

Java代码

public int removeDuplicates(int[] nums) {
int index=0;
int occur=1;
for(int i=1;i<nums.length;i++){
if(nums[index]==nums[i]){
if(occur<2){
index=index+1;
nums[index]=nums[i];
occur++;
}
}else{
index=index+1;
nums[index]=nums[i];
occur=1;
}
}
return index+1;
}

O(n) O(1)

## 1. Two Sum

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

给定一个整型数组和一个目标值，返回和为目标值的2个数的位置。

 public  int[] twoSum(int[] nums, int target) {
int[] index = new int[2];

for (int i = 0; i < nums.length-1; i++) {

for (int j=i+1;j<nums.length;j++){
if (nums[i] +nums[j]==target){
index[0]=i;
index[1]=j;
break;
}
}

}
return index;

}

O(n^2) O(1)

 public  int[] twoSum(int[] nums, int target) {
int[] index = new int[2];

HashMap<Integer,Integer> map=new HashMap<>();

for (int i = 0; i < nums.length; i++) {

if (map.containsKey(target-nums[i])){
index[0]=map.get(target-nums[i]);
index[1]=i;
}
map.put(nums[i],i);
}
return index;

}

O(n) O(n)

## 7. Reverse Integer

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

click to show spoilers.

Note:
The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.

整数逆序输出，超过32位无符号数的返回0.

Java代码：

public int inverse(int x) {
long result = 0;
while (x != 0) {
result = result * 10 + x % 10;
if (result > Integer.MAX_VALUE || result < Integer.MIN_VALUE) {
return 0;
}
x = x / 10;
}
return (int) result;
}

O(n) O(1)

## 4. Median of Two Sorted Arrays

nums1 = [1, 3]
nums2 = [2]

The median is 2.0

nums1 = [1, 2]
nums2 = [3, 4]

The median is (2 + 3)/2 = 2.5

    public double findMedianSortedArrays(int[] nums1, int[] nums2) {

int m=nums1.length;
int n=nums2.length;
int N=m+n;
int[] nums=new int[N];
int i=0,j=0,k=0;
while (i<m&&j<n){
if (nums1[i]<nums2[j]){
nums[k++]=nums1[i++];
}else{
nums[k++]=nums2[j++];
}
}
while (i<m) nums[k++]=nums1[i++];
while (j<n) nums[k++]=nums2[j++];
double  result=0;
if (N%2!=0){
result=(double) nums[N/2];
}else{
result =(double)(nums[N/2-1]+nums[N/2])/2;
}
return result;
}

O(log(m+n)) O(log(m+n))

## 136. Single Number

### 解法一

Java代码：

    public int singleNumber(int[] nums) {
Map<Integer,Integer> map=new HashMap<>();
for (int a:nums){
if (map.containsKey(a)){
map.remove(a);
}else{
map.put(a,1);
}
}

return (int)map.keySet().toArray()[0];
}

### 解法二

  public int singleNumber(int[] nums) {
int n = nums.length;
int goal=nums[0];
for (int i=1;i<n;i++){
goal^=nums[i];
}

return goal;
}

# 189. Rotate Array

For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].

java代码：

class Solution {

public void rotate(int[] nums, int k) {
k%=nums.length;
reverseArr(nums,0,nums.length-1);
reverseArr(nums,0,k-1);
reverseArr(nums,k,nums.length-1);

}

public  void reverseArr(int[] nums,int from,int to){
while(from<to){
int temp=nums[from];
nums[from]=nums[to];
nums[to]=temp;
from++;
to--;
}
}

}

O(n) O(1)

《编程之法第二章》

## 2. 最小的k个数

napoay

#### Merge Sorted Array合并排序数组leetcode

2014年10月24日 492B 下载

#### [leetcode] 384. Shuffle an Array

2016-08-17 16:32:44

#### 【LeetCode】Rotate Array 旋转数组

2015-04-23 15:38:43

#### Leetcode 189: Rotate Array

2015-03-06 07:51:14

#### 【Leetcode】Shuffle an Array

2016-08-12 21:39:35

#### LeetCode697. Degree of an Array解答

2017-10-17 10:27:48

#### Leetcode_88_Merge Sorted Array

2014-11-30 23:37:26

#### Leetcode 410. Split Array Largest Sum 划分数组 解题报告

2016-10-02 17:04:16

#### 【leetcode】88. Merge Sorted Array（Python & C++）

2017-09-05 21:39:01

#### LeetCode 238： Product of Array Except Self

2015-07-27 18:51:41