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的話行不通。

上述解法是對于第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.

思路:盡可能少的移動氛谜,那就是一個元素最好只移動一次掏觉。將第一個非o的放在第一個位置,第二個非零的放在第二個位置值漫,然后剩下的全部設(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.

思路:類似于一個連表有環(huán),求環(huá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)容合作請聯(lián)系作者
  • 序言:七十年代末杨何,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子沥邻,更是在濱河造成了極大的恐慌危虱,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,602評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件唐全,死亡現(xiàn)場離奇詭異埃跷,居然都是意外死亡蕊玷,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,442評論 2 382
  • 文/潘曉璐 我一進(jìn)店門弥雹,熙熙樓的掌柜王于貴愁眉苦臉地迎上來垃帅,“玉大人,你說我怎么就攤上這事剪勿∶吵希” “怎么了?”我有些...
    開封第一講書人閱讀 152,878評論 0 344
  • 文/不壞的土叔 我叫張陵厕吉,是天一觀的道長酱固。 經(jīng)常有香客問我,道長头朱,這世上最難降的妖魔是什么运悲? 我笑而不...
    開封第一講書人閱讀 55,306評論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮项钮,結(jié)果婚禮上扇苞,老公的妹妹穿的比我還像新娘。我一直安慰自己寄纵,他們只是感情好鳖敷,可當(dāng)我...
    茶點故事閱讀 64,330評論 5 373
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著程拭,像睡著了一般定踱。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上恃鞋,一...
    開封第一講書人閱讀 49,071評論 1 285
  • 那天崖媚,我揣著相機(jī)與錄音,去河邊找鬼恤浪。 笑死畅哑,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的水由。 我是一名探鬼主播荠呐,決...
    沈念sama閱讀 38,382評論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼砂客!你這毒婦竟也來了泥张?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,006評論 0 259
  • 序言:老撾萬榮一對情侶失蹤鞠值,失蹤者是張志新(化名)和其女友劉穎媚创,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體彤恶,經(jīng)...
    沈念sama閱讀 43,512評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡钞钙,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 35,965評論 2 325
  • 正文 我和宋清朗相戀三年鳄橘,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片芒炼。...
    茶點故事閱讀 38,094評論 1 333
  • 序言:一個原本活蹦亂跳的男人離奇死亡瘫怜,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出焕议,到底是詐尸還是另有隱情宝磨,我是刑警寧澤,帶...
    沈念sama閱讀 33,732評論 4 323
  • 正文 年R本政府宣布盅安,位于F島的核電站唤锉,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏别瞭。R本人自食惡果不足惜窿祥,卻給世界環(huán)境...
    茶點故事閱讀 39,283評論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望蝙寨。 院中可真熱鬧晒衩,春花似錦、人聲如沸墙歪。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,286評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽虹菲。三九已至靠胜,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間毕源,已是汗流浹背浪漠。 一陣腳步聲響...
    開封第一講書人閱讀 31,512評論 1 262
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留霎褐,地道東北人址愿。 一個月前我還...
    沈念sama閱讀 45,536評論 2 354
  • 正文 我出身青樓,卻偏偏與公主長得像冻璃,于是被迫代替她去往敵國和親响谓。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 42,828評論 2 345

推薦閱讀更多精彩內(nèi)容

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