作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/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

Note: 1 <= N <= 10 ^ 9.

题目大意

问给定一个数字N,有多少种把它展开成连续正整数之和的方案。

解题方法

数学方法

有些题会让人陷入常规做法不能自拔,比如这个题就可能想成回溯或者O(M*N)的循环。事实上可以通过数学方法进行优化。

因为是连续正整数之和,所以可以使用数列求和公式直接求和,把O(M*N)的方案降到O(M)。

N = a + (a + 1) + (a + 2) + (a + 3) + ... + (a + i)得,

N = a * (i + 1) + (i * (i + 1)) / 2,其中,a > 0并且i>= 0

所以,我们先求出prod = (i * (i + 1)) / 2 , 如果这个数字大于N了,就立刻结束。否则,remain = N - prod ==> a * (i + 1). 所以,remain应该能够整除(i + 1),而且余数为a > 0。

这个题用python代码会超时,改用C++就能通过。

C++代码如下:

class Solution {
public:
int consecutiveNumbersSum(int N) {
int res = 0;
for (int i = 0; i < N; i++) {
int prod = (i + 1) * i / 2;
if (prod > N) break;
int remain = N - prod;
if (remain % (i + 1) != 0)
continue;
if (remain / (i + 1) > 0)
res ++;
}
return res;
}
};

日期

2019 年 1 月 5 日 —— 美好的周末又开始了

【LeetCode】829. Consecutive Numbers Sum 解题报告(C++)的更多相关文章

  1. [LeetCode] 829. Consecutive Numbers Sum 连续数字之和

    Given a positive integer N, how many ways can we write it as a sum of consecutive positive integers? ...

  2. LeetCode 2 Add Two Sum 解题报告

    LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...

  3. 829. Consecutive Numbers Sum

    Given a positive integer N, how many ways can we write it as a sum of consecutive positive integers? ...

  4. 【LeetCode】39. Combination Sum 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:[htt ...

  5. 【LeetCode】494. Target Sum 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  6. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  7. 【LeetCode】201. Bitwise AND of Numbers Range 解题报告(Python)

    [LeetCode]201. Bitwise AND of Numbers Range 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/prob ...

  8. 【LeetCode】306. Additive Number 解题报告(Python)

    [LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

  9. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

随机推荐

  1. Zabbix源码安装,使用service命令管理zabbix进程

    1.       前期环境: Zabbix源代码解压包:/root/zabbix-3.0.27 Zabbix安装路径:/usr/local/zabbix-3.0.27 2.       复制启动脚本到 ...

  2. MEGA软件——系统发育树构建方法(图文讲解) 转载

    转载:http://www.plob.org/2012/12/02/4927.html 一.序列文本的准备 构树之前先将目标基因序列都分别保存为txt文本文件中(或者把所有序列保存在同一个txt文本中 ...

  3. Apache RocketMQ分布式消息传递和流数据平台及大厂面试宝典v4.9.2

    概述 **本人博客网站 **IT小神 www.itxiaoshen.com 定义 Apache RocketMQ官网地址 https://rocketmq.apache.org/ Latest rel ...

  4. 10 — springboot整合mybatis — 更新完毕

    1.xml版 -- 复杂sql使用xml,简单sql使用注解 1).导入依赖 <!-- mybatis-spring-boot-starter是第三方( mybatis )jar包,不是spri ...

  5. 日常Java测试第二段 2021/11/12

    第二阶段 package word_show; import java.io.*;import java.util.*;import java.util.Map.Entry; public class ...

  6. pow()是如何实现的?

    如1.5 ** 2.5,如何计算?似乎是这样的: 1. cmath calculates pow(a,b) by performing exp(b * log(a)). stackoverflow 2 ...

  7. 零基础学习java------20---------反射

    1. 反射和动态代理 参考博文:https://blog.csdn.net/sinat_38259539/article/details/71799078 1.0 什么是Class: 我们都知道,对象 ...

  8. Oracle—回车、换行符

    1.回车换行符 chr(10)是换行符, chr(13)是回车, 增加换行符: select ' update ' || table_name || ' set VALID_STATE =''0A'' ...

  9. Android权限级别(protectionLevel)

    通常情况下,对于需要付费的操作以及可能涉及到用户隐私的操作,我们都会格外敏感. 出于上述考虑以及更多的安全考虑,Android中对一些访问进行了限制,如网络访问(需付费)以及获取联系人(涉及隐私)等. ...

  10. GCD的补充

    1-1 关于GCD中的创建和释放     在iOS6.0之前,在GCD中每当使用带creat单词的函数创建对象之后,都应该对其进行一次release操作.           在iOS6.0之后,GC ...