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. HTTP请求消息的数据格式

    servletRequest获取请求消息 Request 分为4部分1.请求行 格式:请求方式 请求url 请求协议/版本GET /login.html HTTP/1.1 特点:行和头之间没有任何分隔 ...

  2. vs2008编译错误fatal error C1902: 程序数据库管理器不匹配;请检查安装解决

    重装了本本上的Xp系统,如往常一样,升级,装杀毒软件,开发工具.一些进行的非常顺利.然而,在我打开VS2008准备耕作的时候,尽然出现了一邪恶的错误提示:vs2008编译错误fatal error C ...

  3. layui下拉框数据过万渲染渲染问题解决方案

    方案一:layui下拉框分页插件 https://fly.layui.com/jie/29002/ 此插件我用了下浏览器缓存有问题,而且当下拉框数据量过万后,会一直渲染不出来,期待后期作者优化 如图下 ...

  4. OPGL+VS2017+GLFW+GLEW配置详细步骤

    OPGL+VS2017+GLFW+GLEW配置详细步骤: https://blog.csdn.net/weixin_40921421/article/details/80211813 原博客地址:ht ...

  5. Python记:索引操作示例:将以数指定年,月,日的日期打印出来

    ————————————————————————————————————不要停止奔跑,不要回顾来路,来路无可眷恋,值得期待的只有前方. months=[ 'January', 'February', ...

  6. java基础(十)之向上转型/向下转型

    向上转型:将子类的对象赋值给父类的引用. 向下转型:将父类的对象赋值给子类的引用. 向上转型 Student a = new Student(); Person zhang = a; 或者 Perso ...

  7. 如和针对CPU时间百分比,Mem使用bytes,以及Network RecvBytes/SendBytes指标性能压测数据可视化

    设计思路:通过jmeter5.1压测获取cpu,Mem,Network的压测指标数据利用pandas+openpyxl进行数据可视化: 涉及添加jar包:下载地址:https://files.cnbl ...

  8. c#项目使用webrtc的降噪模块方法

    分离webrtc的降噪(Noise Suppression)部分 webrtc是Google开源的优秀音视频处理及传输代码库,其中包含了audio processing.video processin ...

  9. 6_16 单词(UVa10129)<欧拉回路>

    考古学家有时候遇到一些神秘的门,这些门需要解开特定的谜题才能打开.因为没有其他方法可以打开门,这谜题对我们来说非常重要.在门上有许多磁盘,每个盘子上有一个英文单字在上面.这些盘子必须被安排,使得盘子上 ...

  10. 【音乐欣赏】《Forget》 - The Tech Thieves

    曲名:Forget 作者:The Tech Thieves [00:00.000] 作曲 : Mark Emmanuel/Alia May Plesa-Topham [00:01.000] 作词 : ...