剑指offer
和为S的连续正数序列
相似的企业真题
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 64M,其他语言128M
热度指数:451962
本题知识点:
穷举
算法知识视频讲解
校招时部分企业笔试将禁止编程题跳出页面,为提前适应,练习时请使用在线自测,而非本地IDE。
题目描述
小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100。但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数)。没多久,他就得到另一组连续正数和为100的序列:18,19,20,21,22。现在把问题交给你,你能不能也很快的找出所有和为S的连续正数序列? Good Luck!
返回值描述:
输出所有和为S的连续正数序列。序列内按照从小至大的顺序,序列间按照开始数字从小到大的顺序
示例1
输入
复制
9
9
返回值
复制
[[2,3,4],[4,5]]
[[2,3,4],[4,5]]
上一题
下一题
登录
/
注册
我的提交
编辑器加载中...
import java.util.ArrayList; public class Solution { public ArrayList
> FindContinuousSequence(int sum) { } }
class Solution { public: vector
> FindContinuousSequence(int sum) { } };
# -*- coding:utf-8 -*- class Solution: def FindContinuousSequence(self, tsum): # write code here
using System.Collections.Generic; class Solution { public List
> FindContinuousSequence(int sum) { // write code here } }
function FindContinuousSequence(sum) { // write code here } module.exports = { FindContinuousSequence : FindContinuousSequence };
function FindContinuousSequence(sum) { // write code here }
package main /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param sum int整型 * @return int整型二维数组 */ func FindContinuousSequence( sum int ) [][]int { // write code here }
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # @param sum int整型 # @return int整型二维数组 # class Solution def FindContinuousSequence(sum) # write code here end end
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param sum int整型 * @return int整型二维数组 */ def FindContinuousSequence(sum: Int): Array[Array[Int]] = { // write code here } }
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param sum int整型 * @return int整型二维数组 */ fun FindContinuousSequence(sum: Int): Array
{ // write code here } }
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param sum int整型 * @return int整型二维数组 */ public int[][] FindContinuousSequence (int sum) { // write code here } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param sum int整型 * @return int整型二维数组 */ export function FindContinuousSequence(sum: number): number[][] { // write code here }
public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param sum int整型 * @return int整型二维数组 */ func FindContinuousSequence ( _ sum: Int) -> [[Int]] { // write code here } }
struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param sum int整型 * @return int整型二维数组 */ pub fn FindContinuousSequence(&self, sum: i32) -> Vec
> { // write code here } }
9
[[2,3,4],[4,5]]