631人阅读 评论(0)

# 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个数

1
0

《从Lucene到Elasticsearch：全文检索实战》
Lucene、ES、ELK开发交流群:370734940
个人资料
• 访问：929799次
• 积分：9210
• 等级：
• 排名：第2389名
• 原创：209篇
• 转载：2篇
• 译文：6篇
• 评论：467条
我的课程

### Elasticsearch 5.4新闻搜索项目实战

StackOverFlow
http://stackoverflow.com/users/6526424
统计
阅读排行
博客专栏
 Spring全家桶 文章：10篇 阅读：12643
 MapReduce编程 文章：7篇 阅读：26706
 Lucene&Elasticsearch&ELK专栏 文章：45篇 阅读：512762
 数据库 文章：9篇 阅读：19732
 J2EE 文章：24篇 阅读：101081
 ruby on rails 文章：18篇 阅读：40119
 数据结构与算法 文章：13篇 阅读：18701
文章分类
最新评论