leetcode菜鸡斗智斗勇系列(8)--- Find N Unique Integers Sum up to Zero
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的更多相关文章
- leetcode菜鸡斗智斗勇系列(5)--- 寻找拥有偶数数位的数字
1.原题: https://leetcode.com/problems/find-numbers-with-even-number-of-digits/ Given an array nums of ...
- leetcode菜鸡斗智斗勇系列(4)--- 单一数字的乘积和总合的减法
1.原题: https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/ Given an i ...
- leetcode菜鸡斗智斗勇系列(3)--- Jewels and Stones珠宝和钻石
1.原题: https://leetcode.com/problems/jewels-and-stones/ You're given strings J representing the types ...
- leetcode菜鸡斗智斗勇系列(2)--- 把一个ipv4地址转换成一串数字
1.原题: https://leetcode.com/problems/defanging-an-ip-address/ 这道题本身很简单, Given a valid (IPv4) IP addre ...
- leetcode菜鸡斗智斗勇系列(1)---把一个链表中的二进制数字转换为一个整型数(int)
Convert Binary Number in a Linked List to Integer这道题在leetcode上面算作是“easy”,然而小生我还是不会做,于是根据大佬的回答来整理一下思路 ...
- leetcode菜鸡斗智斗勇系列(10)--- Decrypt String from Alphabet to Integer Mapping
1.原题: https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/submissions/ Giv ...
- 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 ...
- leetcode菜鸡斗智斗勇系列(7)--- 用最小的时间访问所有的节点
1.原题: https://leetcode.com/problems/minimum-time-visiting-all-points/ On a plane there are n points ...
- leetcode菜鸡斗智斗勇系列(6)--- 检查一个string里面有几个对称的字段
1.原题: https://leetcode.com/problems/split-a-string-in-balanced-strings/ Split a String in Balanced S ...
随机推荐
- spring自动装配和通过java实现装配
1.组建扫描 在类上添加注解@Component注解可以实现组建扫描 @Component public class A{ ... } 2.自动装配 通过在属性上或者方法上添加@Autowired注解 ...
- SaltStack常用的模块
目录 1. SaltStack模块介绍 2. SaltStack常用模块 2.1 SaltStack常用模块之network 2.1.1 network.active_tcp 2.1.2 networ ...
- 「题解」「UOJ-164」「清华集训2015」V
目录 题目 原题目 简要题目 正解 这道题题目简洁新颖,吸引读者阅读兴趣... 题目 原题目 点这里 简要题目 需要你维护长度为n的序列并支持下列操作: 区间加法: 区间赋值: 区间每个 \(a_i\ ...
- 短网址(t.cn、url.cn)生成,网址缩短接口API
简要说明 短网址api接口有很多格式,不同的接口生成的短网址格式也不同,比如常见的t.cn.url.cn.w.url.cn等格式.总而言之短网址接口就是用来将一个冗长的链接缩短成10个字符以内的短链接 ...
- 【音乐欣赏】《In The End》 - Taska Black / Aviella
曲名:In The End 作者:Taska Black .Aviella [00:00.00] 作曲 : Joachim Gorrebeeck/Aviella Winder [00:11.48]La ...
- CentOS7修改主机名的三种方法
在CentOS7中,有三种定义的主机名: 静态的(Static hostname) “静态”主机名也称为内核主机名,是系统在启动时从/etc/hostname自动初始化的主机名. 瞬态的(Tansie ...
- 通过python代码连接MySQL以及插入和显示数据
通过python代码连接MySQL以及插入和显示数据: 数据库huahui创建一个shibie的表,里面有两个varcahr(100)的字段,num和result. import pymysql im ...
- django template 模板
九.Template模板 Template 模板是根据view传过来数据在html展示的功能,典型python 模板jinjia2库提供丰富的上下文展示func 创建template位置在项目下与ap ...
- confluence-工具安装
wiki 企业级的应用知识库,个人感觉还是很不错的,所以自己本地也搭一个玩玩: 1.下载confluence wget https://downloads.atlassian.com/software ...
- Spring-postConstruct参考-转载
Spring@PostConstruct注解和构造方法的调用顺序 先看下@PostConstruct的注解 * The PostConstruct annotation is used on a me ...