关闭

LeetCode Array

标签: leetcodearray
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)

解法二:使用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
查看评论

leetcode532~K-diff Pairs in an Array

Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an inte...
  • stone_yw
  • stone_yw
  • 2017-03-08 15:20
  • 1173

Leetcode:88. Merge Sorted Array(JAVA)

【问题描述】 Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: ...
  • u010183658
  • u010183658
  • 2016-03-13 09:49
  • 983

LeetCode 665. Non-decreasing Array

Non-decreasing Array 题目描述: Given an array with n integers, your task is to check if it could become non-decreasing by modifying ...
  • qq_34594236
  • qq_34594236
  • 2017-09-09 10:49
  • 1171

Leetcode 457. Circular Array Loop

题目链接:  Leetcode 457. Circular Array Loop 第一次遇到用  slow/fast  指针来解决存在循环网络的问题,新奇。 心得:既然是写代码,机器处理数据,应该尽量从机器的角度去思考,而非以人的角度去思考:如果人自己解决它...
  • chengdezhi2011
  • chengdezhi2011
  • 2017-02-10 22:58
  • 516

leetcode 88 Merge Sorted Array

Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: You may assume that nums1 has enough space (size th...
  • wangyaninglm
  • wangyaninglm
  • 2015-06-24 22:47
  • 2942

LeetCode --- 26. Remove Duplicates from Sorted Array

题目链接:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that each element appear only once and return th...
  • makuiyu
  • makuiyu
  • 2015-02-02 11:52
  • 2196

LeetCode: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 ex...
  • fytain
  • fytain
  • 2013-09-02 20:42
  • 2757

【LeetCode-330】Patching Array

原题: Given a sorted positive integer array nums and an integer n, add/patch elements to the array such that any number in range...
  • zhky126349z
  • zhky126349z
  • 2016-03-30 22:46
  • 526

[leetcode] 384. Shuffle an Array

Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] nums = {1,2,3}; Solution solution = new Solution(n...
  • TstsUgeg
  • TstsUgeg
  • 2016-08-17 16:32
  • 546

leetcode题解-532. K-diff Pairs in an Array

题目:Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an i...
  • liuchonge
  • liuchonge
  • 2017-04-15 15:52
  • 1148
    《从Lucene到Elasticsearch:全文检索实战》
    Lucene、ES、ELK开发交流群:370734940
    Lucene、ES、ELK开发交流
    个人资料
    • 访问:929799次
    • 积分:9210
    • 等级:
    • 排名:第2389名
    • 原创:209篇
    • 转载:2篇
    • 译文:6篇
    • 评论:467条
    StackOverFlow
    http://stackoverflow.com/users/6526424
    统计
    博客专栏
    文章分类
    最新评论