找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%把握的,呜呜呜


<<:  Day 12 Develop audio player

>>:  Re: 新手让网页 act 起来: Day12 - React hooks 之 useEffect

day18 : kafka服务应用 on K8S (下)

昨天介绍了kafka的机制,今天将会透过strimzi的方式配置kafka cluster,同样的会...

LeetCode 387. First Unique Character in a String

题目 Given a string, find the first non-repeating ch...

[Day4] Web 小花花

[Day4] Web 小花花 不要问我为啥我的标题这麽傻白甜 我也不知道,取名好难 路上看到可爱小植...

Day2:安装Azure AD Connector同步至M365遇到TLS 1.2卡卡要怎麽办

当我们正准备将企业AD帐号透过传送门(Azure AD Connector)运送到Microsoft...

焦虑与压力

前言 昨天分享了关於拒绝的两三事,是因为它容易出现在日常生活与团体协作当中,後续带来的影响也不容小觑...