Consecutive Numbers Sum
Given a positive integer N, how many ways can we write it as a sum of consecutive positive integers?
Example 1:
Input: 5
Output: 2
Explanation: 5 = 5 = 2 + 3
Example 2:
Input: 9
Output: 3
Explanation: 9 = 9 = 4 + 5 = 2 + 3 + 4
Example 3:
Input: 15
Output: 4
Explanation: 15 = 15 = 8 + 7 = 4 + 5 + 6 = 1 + 2 + 3 + 4 + 5
分析:https://leetcode.com/problems/consecutive-numbers-sum/discuss/209317/topic
这道题的要求的另一种说法: 把N表示成一个等差数列(公差为1)的和
我们不妨设这个数列的首项是x,项数为m,则这个数列的和就是[x + (x + (m-1))]m / 2 = mx + m(m-1)/2 = N
接下来,一个很自然的想法就是,枚举m,通过上式判断对于相应的m是否存在合法的x。
x = ((N - m(m-1)/2)) / m
显然枚举的复杂度是O(sqrt(N))。因为m能取到的最大值显然是sqrt(n)数量级的
class Solution {
int consecutiveNumbersSum(int N) {
int ans = ;
for (int m = ; ; m++) {
int mx = N - m * (m-) / ;
if (mx <= )
break;
if (mx % m == )
ans++;
}
return ans;
}
}
Consecutive Numbers Sum的更多相关文章
- 829. Consecutive Numbers Sum
Given a positive integer N, how many ways can we write it as a sum of consecutive positive integers? ...
- [Swift]LeetCode829. 连续整数求和 | Consecutive Numbers Sum
Given a positive integer N, how many ways can we write it as a sum of consecutive positive integers? ...
- [LeetCode] 829. Consecutive Numbers Sum 连续数字之和
Given a positive integer N, how many ways can we write it as a sum of consecutive positive integers? ...
- 【LeetCode】829. Consecutive Numbers Sum 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学方法 日期 题目地址:https://leetc ...
- leetcode Database3(Nth Highest Salary<—>Consecutive Numbers<—>Department Highest Salary)
一.Nth Highest Salary Write a SQL query to get the nth highest salary from the Employee table. +----+ ...
- LeetCode Database: Consecutive Numbers
Consecutive Numbers Write a SQL query to find all numbers that appear at least three times consecuti ...
- [LeetCode] Consecutive Numbers 连续的数字 --数据库知识(mysql)
1. 题目名称 Consecutive Numbers 2 .题目地址 https://leetcode.com/problems/consecutive-numbers/ 3. 题目内容 写一个 ...
- 【leetcode】1296. Divide Array in Sets of K Consecutive Numbers
题目如下: Given an array of integers nums and a positive integer k, find whether it's possible to divide ...
- [SQL]LeetCode180. 连续出现的数字 | Consecutive Numbers
SQL架构: Create table If Not Exists Logs (Id int, Num int) Truncate table Logs insert into Logs (Id, N ...
随机推荐
- Maximum GCD(UVA 11827)
Problem:Given the N integers, you have to find the maximum GCD (greatest common divisor) of every po ...
- C语言实现简单的哈希表
这是一个简单的哈希表的实现,用c语言做的. 哈希表原理 这里不讲高深理论,只说直观感受.哈希表的目的就是为了根据数据的部分内容(关键字),直接计算出存放完整数据的内存地址. 试想一下,如果从链表中根据 ...
- Codeforces 955C Sad powers(数论)
Codeforces 955C Sad powers 题意 q组询问,每次询问给定L,R,求[L,R]区间内有多少个数可以写成ap的形式,其中a>0,p>1,1 ≤ L ≤ R ≤ 1e1 ...
- addEventListener 事件监听器 冒泡事件)
addEventListener 事件监听器 (冒泡事件) 1.后面绑定的事件照样会执行 2.不会被覆盖 3.调用者是事件源but.addEventListener 4.参数1,事件名(不带on ...
- shell脚本备份当前日期文件
#!/bin/bash #一月前 historyTime=$(date "+%Y-%m-%d %H" -d '1 month ago') echo ${historyTime} h ...
- LeetCode 221. 最大正方形(Maximal Square)
题目描述 在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积. 示例: 输入: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 输出: ...
- [Mysql]一对多关系是如何发挥作用的?
一个孩子只有一个妈妈,而一个妈妈可以有多个孩子,这是典型的一对多的关系,这里采用navicat图形化界面建立二者的关系. 第一步:创建mother表,如下图: 第二步:创建children表,在ch ...
- 【转】APIJSON,让接口见鬼去吧!
我: APIJSON,让接口和文档见鬼去吧! https://github.com/TommyLemon/APIJSON 服务端: 什么鬼? 客户端: APIJSON是啥? 我: APIJSON是一种 ...
- RabbitMQ 使用参考
http://www.zouyesheng.com/rabbitmq.html 安装 基本概念 基本形式 持久化 调度策略 5.1. fanout 5.2. direct 5.3. topic 5.4 ...
- 纯CSS实现加载转圈样式
不同的项目中对于等待加载时转圈圈的样式是不同的,有的是传统的转圈的gif图片,见得比较多的是将转圈圈的换成了可爱的图标.有时候项目中加入等待加载的图片会很违和,不符合美观,所以需要用CSS做一个.下面 ...