Leetcode-Arrays(2017-06-17)

Summary Ranges(228)

Given a sorted integer array without duplicates, return the summary of its ranges.

For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"].

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

public class Solution {
    public List<String> summaryRanges(int[] nums) {
        List<String> list=new ArrayList();
        for(int i=0;i<nums.length;i++){
            int a=nums[i];
            while(i+1<nums.length&&(nums[i+1]-nums[i])==1){
                i++;
            }
            if(a!=nums[i]){
                list.add(a+"->"+nums[i]);
            }else{
                list.add(a+"");
            }
        }
        return list;
        
    }
}

238.Product of Array Except Self

Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].

Solve it without division and in O(n).

For example, given [1,2,3,4], return [24,12,8,6].

Follow up:
Could you solve it with constant space complexity? (Note: The output array does not count as extra space for the purpose of space complexity analysis.)

Subscribe to see which companies asked this question.

Show Tags

Show Similar Problems

public class Solution {
    public int[] productExceptSelf(int[] nums) {
    int n = nums.length;
    int[] res = new int[n];
    res[0] = 1;
    for (int i = 1; i < n; i++) {
        res[i] = res[i - 1] * nums[i - 1];
    }
    int right = 1;
    for (int i = n - 1; i >= 0; i--) {
        res[i] *= right;
        right *= nums[i];
    }
    return res;
}
}

思路:

最開始是想全部乘,然后除以自身。但是有0的話行不通。

上述解法是對(duì)于第i哥元素。先求 1 * arr[0] * … * arr[i-1];

然后再乘以arr[n-1] * ..arr[i+1];

268.Missing Number

Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.

For example,
Given nums = [0, 1, 3] return 2.

Note:
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

Subscribe to see which companies asked this question.

思路 :

求和相見。差值就是缺少的數(shù)字。另外一種是異或。a^a=0.

public class Solution {
    public int missingNumber(int[] nums) {
        int sum=0;
        int n=nums.length;
        for(int i=0;i<n;i++){
            sum+=nums[i];
        }
        int pre=n*(n+1)/2;
        return pre-sum==0?0:pre-sum;
  
    }
}

解法2

The basic idea is to use XOR operation. We all know that abb =a, which means two xor operations with the same number will eliminate the number and reveal the original number.
In this solution, I apply XOR operation to both the index and value of the array. In a complete array with no missing numbers, the index and value should be perfectly corresponding( nums[index] = index), so in a missing array, what left finally is the missing number.

public int missingNumber(int[] nums) {

    int xor = 0, i = 0;
    for (i = 0; i < nums.length; i++) {
        xor = xor ^ i ^ nums[i];
    }

    return xor ^ i;
}

283.Move Zeroes

Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.

For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].

Note:

  1. You must do this in-place without making a copy of the array.
  2. Minimize the total number of operations.

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

Subscribe to see which companies asked this question.

思路:盡可能少的移動(dòng),那就是一個(gè)元素最好只移動(dòng)一次。將第一個(gè)非o的放在第一個(gè)位置,第二個(gè)非零的放在第二個(gè)位置,然后剩下的全部設(shè)為0.

public class Solution {
    public void moveZeroes(int[] nums) {
        if(nums.length==0 ||nums==null) return ;
        int pos=0;
        for(int num:nums){
            if(num!=0){
                nums[pos]=num;
                pos++;
            }
        }
        for(;pos<nums.length;pos++){
            nums[pos]=0;
        }
        return;
    }
}

287. Find the Duplicate Number

Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.

Note:

  1. You must not modify the array (assume the array is read only).
  2. You must use only constant, O(1) extra space.
  3. Your runtime complexity should be less than O(n2).
  4. There is only one duplicate number in the array, but it could be repeated more than once.

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

Subscribe to see which companies asked this question.

思路:類似于一個(gè)連表有環(huán),求環(huán)的交叉點(diǎn)。

The main idea is the same with problem Linked List Cycle II,https://leetcode.com/problems/linked-list-cycle-ii/. Use two pointers the fast and the slow. The fast one goes forward two steps each time, while the slow one goes only step each time. They must meet the same item when slow==fast. In fact, they meet in a circle, the duplicate number must be the entry point of the circle when visiting the array from nums[0]. Next we just need to find the entry point. We use a point(we can use the fast one before) to visit form begining with one step each time, do the same job to slow. When fast==slow, they meet at the entry point of the circle. The easy understood code is as follows.

public class Solution {
    public int findDuplicate(int[] nums) {
        if(nums.length>1){
            int slow=nums[0];
            int fast=nums[nums[0]];
            while(slow!=fast){
                slow=nums[slow];
                fast=nums[nums[fast]];
            }
            fast=0;
            while(fast!=slow){
                fast=nums[fast];
                slow=nums[slow];
            }
            return slow;
        }
        return -1;
        
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問題, 分享了一些自己做題目的經(jīng)驗(yàn)。 張土汪:刷leetcod...
    土汪閱讀 12,899評(píng)論 0 33
  • 七夕算是躲過去了。終于可以名正言順地扒一扒我一直想扒的她了。 她叫曾小花,是個(gè)大美女,在高中時(shí)期就是我們班最美的那...
    船長Captain閱讀 537評(píng)論 0 0
  • iOS9 3D Touch 使用第二篇 字?jǐn)?shù)712閱讀908評(píng)論2喜歡3 前一篇文章介紹了給圖片添加快捷方式,這篇...
    Charming_Zhang閱讀 475評(píng)論 0 2
  • 就這樣畢業(yè)了,就這樣出來了,踏入社會(huì),走上工作崗位。沒有一絲的波瀾,一切都來得那么自然,好像上天早就安排好了一樣。...
    憐小竹閱讀 365評(píng)論 0 1
  • 再次見面,他的笑仍舊像棉花糖,身上散發(fā)的時(shí)有時(shí)無的面包的味道悄悄鉆進(jìn)她的鼻子,嘴巴里,如之前一般挑逗著她的味蕾。 ...
    愚歌閱讀 466評(píng)論 3 6

友情鏈接更多精彩內(nèi)容