problem

754. Reach a Number

solution1:

class Solution {
public:
int reachNumber(int target) {
target = abs(target);
long n = ceil(0.5*(sqrt(+8.0*target)-1.0));
long sum = n*(n+)/;
if(sum==target) return n;
long d = sum - target;
if((d&) == ) return n;
return n + ((n&) ? : );
}
};

solution2:

class Solution {
public:
int reachNumber(int target) {
target = abs(target);
int res = , sum = ;
while(sum<target || (sum-target)%==)
{
res++;
sum += res;
}
return res;
}
};

solution3:solution1的精简版;

class Solution {
public:
int reachNumber(int target) {
int n = ceil((sqrt(+8.0*abs(target))-)/), d = n*(n+)*0.5-abs(target);
return n + (d%)*(n%+);
}
};

参考

1. Leetcode_easy_754. Reach a Number;

2. Grandyang;

【Leetcode_easy】754. Reach a Number的更多相关文章

  1. 【LeetCode】754. Reach a Number 解题报告(Python & C++)

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

  2. 【Leetcode_easy】1137. N-th Tribonacci Number

    problem 1137. N-th Tribonacci Number solution: class Solution { public: int tribonacci(int n) { ) ; ...

  3. LeetCode 754. Reach a Number

    754. Reach a Number(到达终点数字) 链接:https://leetcode-cn.com/problems/reach-a-number/ 题目: 在一根无限长的数轴上,你站在0的 ...

  4. 【贪心】codeforces D. Minimum number of steps

    http://codeforces.com/contest/805/problem/D [思路] 要使最后的字符串不出现ab字样,贪心的从后面开始更换ab为bba,并且字符串以"abbbb. ...

  5. 【LeetCode】9、Palindrome Number(回文数)

    题目等级:Easy 题目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it rea ...

  6. 【leetcode】668. Kth Smallest Number in Multiplication Table

    题目如下: 解题思路:几乎和[leetcode]719. Find K-th Smallest Pair Distance 的方法一样.只不过一个是减法一个是乘法,还有一点区别是[leetcode]7 ...

  7. 【Leetcode_easy】933. Number of Recent Calls

    problem 933. Number of Recent Calls 参考 1. Leetcode_easy_933. Number of Recent Calls; 完

  8. 【Leetcode_easy】1128. Number of Equivalent Domino Pairs

    problem 1128. Number of Equivalent Domino Pairs solution1: 不明白为什么每个元素都要加上count: class Solution { pub ...

  9. 【Leetcode_easy】806. Number of Lines To Write String

    problem 806. Number of Lines To Write String solution: class Solution { public: vector<int> nu ...

随机推荐

  1. 字符编码,python解释器------总结

    目录 1. 编码: 1.字符编码 2. 编码的历史 3. 编码和解码 2. python解释器 解释代码的流程 1. 读取文本到解释器 2. 识别代码(检查语法问题) 3. 往终端打印 1. 编码: ...

  2. Java中实例方法和类方法的区别举例

    QAQQAQAQQQAQQAQQAQAQ import java.util.ArrayList; import java.util.Iterator; class myclass{ ; ; publi ...

  3. white-space

    white-space 语法: white-space:normal | pre | nowrap | pre-wrap | pre-line 默认值:normal 适用于:所有元素 继承性:有 动画 ...

  4. jquery验证时间

    http://blog.csdn.net/guguojin/article/details/7045908 验证时间的正则表达式集合 //日期格式yyyy  PatternsDict.date_y= ...

  5. learning scala pattern matching 02

    code package com.aura.scala.day01 object patternMatching02 { def main(args: Array[String]): Unit = { ...

  6. mutt/mail

    邮件管理命令 发送和接收邮件

  7. pandas记录

    pandas的map方法使用 import pandas as pd import numpy as np data = pd.DataFrame(np.random.randint(0, 10, ( ...

  8. docker部署vue前端

    1.下载安装nginx image docker pull nginx:latest 2.准备将编译后的代码上传到主机上 3.编写dockerfile, nginx conf,并创建镜像 Docker ...

  9. SSH dao层异常 org.hibernate.HibernateException: No Session found for current thread

    解决方法: 在 接口方法中添加 事务注解 即可. public interface IBase<PK extends Serializable, T> { @Transactional v ...

  10. Spring - 环境安装

    安装IDEA的非Community版本和Java的包之后就可以用Java来HelloWorld了. 然后去这个链接:https://github.com/spring-guides/gs-rest-s ...