1.原题:

https://leetcode.com/problems/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.

翻译:给你一个int数n,返回一个包含n个唯一的int的array,其和sum必须为0.

Input: n = 5

Output: [-7,-1,1,3,4]

2.解题思路:

这题的解法大体上有两种思路,一种是比较傻的办法就是从0开始,向两侧扩散,比如说n=3,你的 output 就是 [-1,0,1].如果是n=5,就是[-2,-1,0,1,2]。

    def sumZero(self, n: int) -> List[int]:
ans = [0] * n
start, end = 0, n - 1
while start < end:
ans[start], ans[end] = -end, end
start, end = start + 1, end - 1
return ans

另一种比较聪明,就是像这位大佬一样:https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/discuss/465585/JavaC%2B%2BPython-Find-the-Rule

找到规则: A[i] = i * 2 - n + 1  , A[]就是指Output的这个array,举个例子就说,n=3,你的结果可以是 [-2,-1,0,1,2],那这个规则是符合的。

    vector<int> sumZero(int n) {
vector<int> A(n);
for (int i = 0; i < n; ++i)
A[i] = i * 2 - n + 1;
return A;
}

leetcode菜鸡斗智斗勇系列(8)--- Find N Unique Integers Sum up to Zero的更多相关文章

  1. leetcode菜鸡斗智斗勇系列(5)--- 寻找拥有偶数数位的数字

    1.原题: https://leetcode.com/problems/find-numbers-with-even-number-of-digits/ Given an array nums of ...

  2. leetcode菜鸡斗智斗勇系列(4)--- 单一数字的乘积和总合的减法

    1.原题: https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/ Given an i ...

  3. leetcode菜鸡斗智斗勇系列(3)--- Jewels and Stones珠宝和钻石

    1.原题: https://leetcode.com/problems/jewels-and-stones/ You're given strings J representing the types ...

  4. leetcode菜鸡斗智斗勇系列(2)--- 把一个ipv4地址转换成一串数字

    1.原题: https://leetcode.com/problems/defanging-an-ip-address/ 这道题本身很简单, Given a valid (IPv4) IP addre ...

  5. leetcode菜鸡斗智斗勇系列(1)---把一个链表中的二进制数字转换为一个整型数(int)

    Convert Binary Number in a Linked List to Integer这道题在leetcode上面算作是“easy”,然而小生我还是不会做,于是根据大佬的回答来整理一下思路 ...

  6. leetcode菜鸡斗智斗勇系列(10)--- Decrypt String from Alphabet to Integer Mapping

    1.原题: https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/submissions/ Giv ...

  7. leetcode菜鸡斗智斗勇系列(9)--- Range Sum of BST

    1.原题: https://leetcode.com/problems/range-sum-of-bst/ Given the root node of a binary search tree, r ...

  8. leetcode菜鸡斗智斗勇系列(7)--- 用最小的时间访问所有的节点

    1.原题: https://leetcode.com/problems/minimum-time-visiting-all-points/ On a plane there are n points ...

  9. leetcode菜鸡斗智斗勇系列(6)--- 检查一个string里面有几个对称的字段

    1.原题: https://leetcode.com/problems/split-a-string-in-balanced-strings/ Split a String in Balanced S ...

随机推荐

  1. 用OpenCV读取摄像头

    首先插入摄像头 在电脑中查看 #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp ...

  2. AcWing 差分一维加二维

    一维 #include<bits/stdc++.h> using namespace std ; ; int n,m; int a[N],b[N]; //a为前缀和,b为差分 差分和前缀和 ...

  3. k8s集群问题记录

    k8s集群问题记录 k8s学习方案 问题解决思路 主要学习路径: rancher(k8s)->rke->helm->kubectl->k8s(k8s中文api) 常见问题总结: ...

  4. Flink流处理(四)- 时间语义

    4. 时间语义(Time Semantics) 这章我们会介绍时间语义,以及在流中,对于时间的各种不同的概念的描述.同时我们也会讨论一个流处理器在事件乱序的情况下,如何能提供精准的结果,以及如何使用流 ...

  5. bistoury的源码启动(二)

    bistoury.conf这个东东就是我们代码中的 -Dbistoury.conf=D:\openSource\bistoury\bistoury-proxy\conf 这样就能搞定了,一下子就能启动 ...

  6. Java - 集合 - List

    1.List实现类:ArrayList.LinkedList.Vector ArrayList使用: void test() { //声明 List<String> testlist = ...

  7. 用eclipse发布springboot项目

    使用eclipse打包springboot项目时一直报错 [ERROR] No compiler is provided in this environment. Perhaps you are ru ...

  8. kvm增加磁盘容量

    一.qcow2格式 查看镜像文件实际占用空间 ls -alh t.qcow2 加容量(只能加不能减) qemu-img resize t.qcow2 +1G 查看qcow2信息 qemu-img in ...

  9. jmeter巧用自增长型变量

    实现目的 在进行性能测试时,某些请求中的参数值并不允许被重复使用,比如账号的创建.开通授权等服务,这时就需要在jmeter中构造一些自增长型的变量,供后续请求使用,以解决参数值重复的问题. 脚本实现 ...

  10. C语言报错:“gets”: 找不到标识符。解决方法

    C语言报错:“gets”: 找不到标识符. 把“gets”改成“gets_s”即可.