https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/description/
Find N Unique Integers Sum up to Zero - LeetCode
Can you solve this real interview question? Find N Unique Integers Sum up to Zero - Given an integer n, return any array containing n unique integers such that they add up to 0. Example 1: Input: n = 5 Output: [-7,-1,1,3,4] Explanation: These arrays als
leetcode.com
1
2
3
4
5
6
7
8
9
10
|
class Solution:
def sumZero(self, n: int) -> List[int]:
if n % 2 == 0:
answer = []
else:
answer = [0]
for i in range(1, n//2 + 1):
answer.append(i)
answer.append(-i)
return answer
|
cs |
n이 홀수, 짝수일때 구분해서 좌우 대칭으로 숫자 입력하면 될듯
반응형
'python-algorithm' 카테고리의 다른 글
leetcode 3005. Count Elements With Maximum Frequency (0) | 2024.01.15 |
---|---|
leetcode 917. Reverse Only Letters (0) | 2024.01.13 |
leetcode 1636. Sort Array by Increasing Frequency (0) | 2024.01.09 |
leetcode 2278. Percentage of Letter in String (0) | 2024.01.08 |
leetcode 2500. Delete Greatest Value in Each Row (0) | 2024.01.07 |
댓글