Skip to content

Latest commit

 

History

History
177 lines (151 loc) · 4.47 KB

File metadata and controls

177 lines (151 loc) · 4.47 KB

中文文档

Description

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

Solutions

Python3

class Solution:
    def findOriginalArray(self, changed: List[int]) -> List[int]:
        if len(changed) % 2 != 0:
            return []
        n = 100010
        counter = [0] * n
        for x in changed:
            counter[x] += 1
        if counter[0] % 2 != 0:
            return []
        res = [0] * (counter[0] // 2)
        for i in range(1, n):
            if counter[i] == 0:
                continue
            if i * 2 > n or counter[i] > counter[i*2]:
                return []
            res.extend([i] * counter[i])
            counter[i*2] -= counter[i]
        return res

Java

class Solution {
    public int[] findOriginalArray(int[] changed) {
        if (changed.length % 2 != 0) {
            return new int[]{};
        }
        int n = 100010;
        int[] counter = new int[n];
        for (int x : changed) {
            ++counter[x];
        }
        if (counter[0] % 2 != 0) {
            return new int[]{};
        }
        int[] res = new int[changed.length / 2];
        int j = counter[0] / 2;
        for (int i = 1; i < n; ++i) {
            if (counter[i] == 0) {
                continue;
            }
            if (i * 2 >= n || counter[i] > counter[i * 2]) {
                return new int[]{};
            }
            counter[i * 2] -= counter[i];
            while (counter[i]-- > 0) {
                res[j++] = i;
            }
        }
        return res;
    }
}

C++

class Solution {
public:
    vector<int> findOriginalArray(vector<int>& changed) {
        if (changed.size() % 2 != 0) return {};
        int n = 100010;
        vector<int> counter(n);
        for (int x : changed) ++counter[x];
        if (counter[0] % 2 != 0) return {};
        vector<int> res(changed.size() / 2);
        int j = counter[0] / 2;
        for (int i = 1; i < n; ++i)
        {
            if (counter[i] == 0) continue;
            if (i * 2 >= n || counter[i] > counter[i * 2]) return {};
            counter[i * 2] -= counter[i];
            while (counter[i]--) res[j++] = i;
        }
        return res;
    }
};

Go

func findOriginalArray(changed []int) []int {
	if len(changed)%2 != 0 {
		return []int{}
	}
	n := 100010
	counter := make([]int, n)
	for _, x := range changed {
		counter[x]++
	}
	if counter[0]%2 != 0 {
		return []int{}
	}
	var res []int
	for j := 0; j < counter[0]/2; j++ {
		res = append(res, 0)
	}
	for i := 1; i < n; i++ {
		if counter[i] == 0 {
			continue
		}
		if i*2 >= n || counter[i] > counter[i*2] {
			return []int{}
		}
		for j := 0; j < counter[i]; j++ {
			res = append(res, i)
		}
		counter[i*2] -= counter[i]
	}
	return res
}

...