LeetCode Array

标签: leetcodearray
570人阅读 评论(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)

解法二:使用HashMap

 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和nums2长度分别为m和n,返回中位数。要求时间复杂度为O(log(m+n))。
例子1:

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

The median is 2.0

例子2:

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

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

先合并有序数组,再返回中位数,java代码如下:

    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

给定一个整型数组,数组里的元素都出现2次,只有1个除外。找出只出现1次的元素:

解法一

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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    《从Lucene到Elasticsearch:全文检索实战》
    Lucene、ES、ELK开发交流群:370734940
    Lucene、ES、ELK开发交流
    个人资料
    • 访问:811083次
    • 积分:8389
    • 等级:
    • 排名:第2473名
    • 原创:196篇
    • 转载:2篇
    • 译文:6篇
    • 评论:446条
    StackOverFlow
    http://stackoverflow.com/users/6526424
    统计
    博客专栏
    文章分类
    最新评论