返回介绍

solution / 2100-2199 / 2177.Find Three Consecutive Integers That Sum to a Given Number / README_EN

发布于 2024-06-17 01:03:09 字数 2499 浏览 0 评论 0 收藏 0

2177. Find Three Consecutive Integers That Sum to a Given Number

中文文档

Description

Given an integer num, return _three consecutive integers (as a sorted array)__ that sum to _num. If num cannot be expressed as the sum of three consecutive integers, return_ an empty array._

 

Example 1:

Input: num = 33
Output: [10,11,12]
Explanation: 33 can be expressed as 10 + 11 + 12 = 33.
10, 11, 12 are 3 consecutive integers, so we return [10, 11, 12].

Example 2:

Input: num = 4
Output: []
Explanation: There is no way to express 4 as the sum of 3 consecutive integers.

 

Constraints:

  • 0 <= num <= 1015

Solutions

Solution 1

class Solution:
  def sumOfThree(self, num: int) -> List[int]:
    x, mod = divmod(num, 3)
    return [] if mod else [x - 1, x, x + 1]
class Solution {
  public long[] sumOfThree(long num) {
    if (num % 3 != 0) {
      return new long[] {};
    }
    long x = num / 3;
    return new long[] {x - 1, x, x + 1};
  }
}
class Solution {
public:
  vector<long long> sumOfThree(long long num) {
    if (num % 3) {
      return {};
    }
    long long x = num / 3;
    return {x - 1, x, x + 1};
  }
};
func sumOfThree(num int64) []int64 {
  if num%3 != 0 {
    return []int64{}
  }
  x := num / 3
  return []int64{x - 1, x, x + 1}
}
function sumOfThree(num: number): number[] {
  if (num % 3) {
    return [];
  }
  const x = Math.floor(num / 3);
  return [x - 1, x, x + 1];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文