找LeetCode上简单的题目来撑过30天啦(DAY15)

题号:2007 标题:Find Original Array From Doubled Array 难度Medium

An integer array original is transformed into a doubled array changed by appending twice the value of every element in original, and then randomly shuffling the resulting array.
Given an array changed, return original if changed is a doubled array. If changed is not a doubled array, return an empty array. The elements in original may be returned in any order.

Example 1:
Input: changed = [1,3,4,2,6,8]
Output: [1,3,4]
Explanation: One possible original array could be [1,3,4]:

  • Twice the value of 1 is 1 * 2 = 2.
  • Twice the value of 3 is 3 * 2 = 6.
  • Twice the value of 4 is 4 * 2 = 8.
    Other original arrays could be [4,3,1] or [3,1,4].

Example 2:
Input: changed = [6,3,0,1]
Output: []
Explanation: changed is not a doubled array.


Example 3:
Input: changed = [1]
Output: []
Explanation: changed is not a doubled array.


Constraints:
• 1 <= changed.length <= 105
• 0 <= changed[i] <= 105


我的程序码

class Solution {
    public int[] findOriginalArray(int[] changed) {
        int[] result = new int[(changed.length)/2];
        List<Integer> result2 = new ArrayList<>();
        int i,j=0,k=0,count0=0,count;
        //k = (changed.length)/2-1;
        if((changed.length)%2!=0){
        //    System.out.println(changed.length);
            return new int[0]; 
        }
        Arrays.sort(changed);
        int temp;
        // for(i=0;i<(changed.length);i++){
        //     System.out.print(changed[i]+ "," );
        // }
        
        j=0;
        for(i=0;i<changed.length-1;i++){
            if(changed[i]==0){
                    if(changed[i]==changed[i+1]){
                        result[j]=changed[i];
                        j++;
                        i++;
                        count0++;
                    }
                count0++;
            }else{
                break;
            }
        }
        if(count0%2!=0){
                    return new int[0]; 
        }
        if(count0==0){
            i=0;
        }else{
            i=count0;
        }
        for(;i<changed.length-1;i++){
            if(changed[i] != -1){
            int chk = 0;
            for(k=i+1;k<changed.length;k++){
                if(changed[i]*2==changed[k]){
                    System.out.print(changed[i]+ ","+ changed[k]);
                    result[j]=changed[i];
                    j++;
                    changed[k] = -1;
                    chk =1;
                    break;
                }
            }   
            if(chk==0){
                return new int[0];
            }}
        }
        return result;
    }
}

DAY15心得
这几天就先这样吧,没什麽特别的


<<:  Kotlin Android 第25天,从 0 到 ML - TensorFlow Lite 功能与特色

>>:  Day30 测试写起乃 - 完赛感言!

[Day 24] IIOT资讯安全规划

调查 查其他同业是否有案例 台积电产线中毒大当机,52亿元资安震撼教育 工业电脑大厂研华证实部分服务...

[Python 爬虫这样学,一定是大拇指拉!] DAY21 - 实战演练:JSON Response - 抓取个股日成交资讯

好的,讲解完 Requests 套件的基本介绍後,终於要进入真实情况的爬虫应用拉! 但我们一步一步来...

Day21 xib传值的小教室2

接续昨天。 到到二个页面的程序码中,新增一字串变数,也在生命周期中,使此变数会等於第二页的文字格变数...

[13th][Day16] tamplete

golang template golang stdlib(标准函式库)中提供两种跟 templat...

[Day19] Flutter with GetX something else

这篇主要讲GetX所提供, 自己有接触过的额外功能 为大家介绍 多国语系, 萤幕长宽, snackb...