iT邦幫忙

2021 iThome 鐵人賽

DAY 12
0
AI & Data

想到甚麼打甚麼系列 第 12

找LeetCode上簡單的題目來撐過30天啦(DAY12)

題號:15 標題:3Sum 難度:Medium

Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.
Notice that the solution set must not contain duplicate triplets.

Example 1:
Input: nums = [-1,0,1,2,-1,-4]
Output: [[-1,-1,2],[-1,0,1]]

Example 2:
Input: nums = []
Output: []

Example 3:
Input: nums = [0]
Output: []

Constraints:
• 0 <= nums.length <= 3000
• -105 <= nums[i] <= 105

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        int[] temp = new int[3];
        List<Integer> result2 = new ArrayList<Integer>();
        int i,j,z;
        
        for(i=0;i<nums.length-2;i++){
            for(j=i+1;j<nums.length-1;j++){
                for(z=j+1;z<nums.length;z++){
                     if (nums[i] +nums[j]+nums[z]==0 ){
                         System.out.println(nums[i]+ ","+nums[j]+","+nums[z]);
                         temp[0] = nums[i];
                         temp[1] = nums[j]; 
                         temp[2] = nums[z];
                         Arrays.sort(temp);
                         result2 = Arrays.asList(temp[0],temp[1],temp[2]);
                         if(!result.contains(result2)){
                             result.add(result2);
                         }
                     }
                }
            }
        }
        
        return result;
    }
}

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        int[] temp = new int[3];
        List<Integer> result2 = new ArrayList<Integer>();
        int i,j,z;
        
        Arrays.sort(nums);
        
        if(nums.length < 3){
            return result;
        }
        
         
        for(i=0;i<nums.length-2;i++){
           
            if(i!=0 && nums[i]== nums[i-1]){
                //i++;
                continue;
            }
            j=i+1; z = ((nums.length)-1);
            //System.out.println("1:"+i+","+j+","+z);
            int target;
            while(true){
                target  = nums[i]+nums[j]+nums[z];
                if(j >= z){
                    break;
                }
                if(target>0){
                    z--;
                }else if(target<0){
                    j++;
                }else if(target==0){
                    result.add(Arrays.asList(nums[i],nums[j],nums[z]));
                    //System.out.println("2:"+i+","+j+","+z);
                    z--;
                    j++;
                    while(nums[z]== nums[z+1]){
                        if(j >= z){
                            break;
                        }    
                        z--;
                    }
                    while(nums[j]== nums[j-1]){
                        if(j >= z){
                            break;
                        }
                            j++;
                    }
                }
            }
        }
        
        return result;
    }
}

花比較久的時間
用三層for會超時,感謝團長台積電辣妹提示此題解
是說知道解法後原本想把nums陣列存成arraylist做判斷,結果就爆炸ㄌ,邏輯沒換,用原本得nums陣列直接下去判讀,一切正常,就當我改錯了甚麼吧,懶的抓bug了

DAY12心得
明日的我得去看醫生,後天的後天得去考照,我覺得我完蛋了,沒一項目有100%把握的,嗚嗚嗚


上一篇
找LeetCode上簡單的題目來撐過30天啦(DAY11)
下一篇
找LeetCode上簡單的題目來撐過30天啦(DAY13)
系列文
想到甚麼打甚麼30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
0
阿瑜
iT邦研究生 4 級 ‧ 2021-10-15 23:29:52

啊呀為呀~
好好奇 那位台積電辣妹是誰XD

soft_soft iT邦新手 5 級 ‧ 2021-10-16 19:22:19 檢舉

辣妹您今晚不醉不歸嗎

0
阿瑜
iT邦研究生 4 級 ‧ 2021-10-15 23:32:21

BTW: soft_soft 是考照王,任何證照都難不倒他!
好想知道 tips 喔 ~~~

我要留言

立即登入留言