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;

最新文章

  1. Android探索之AIDL实现进程间通信
  2. [LeetCode] Flatten 2D Vector 压平二维向量
  3. JAVA基础知识(转)
  4. MyBatis传入参数与parameterType
  5. Python filter,map,lambda,reduce,列表解析
  6. ini 文件操作记要(1): 使用 TIniFile
  7. BZOJ2862 : 分糖果
  8. vb.net Linq 筛选(像 select distinct) DateTable 日期数据中的年份
  9. Python之函数篇
  10. struts2的action从request获取参数值的几种方式
  11. poj 1904 King&#39;s Quest
  12. Linux下OpenSSL 安装
  13. 通过虚拟机VMware来练习安装ESXi
  14. Mac环境下.Net开发
  15. 高性能前端框架React详解
  16. 运行java web项目时报错:Several ports (8005, 8080, 8009) required
  17. Azure ARM (19) 将传统的ASM VM迁移到ARM VM (2)
  18. npm --save-dev --save 的区别
  19. HTML语言发展史
  20. 基于【CentOS-7+ Ambari 2.7.0 + HDP 3.0】搭建HAWQ数据仓库——安装配置NTP服务,保证集群时间保持同步

热门文章

  1. SCPI 语言简介
  2. 如何下载oracle jdk|oracle jdk下载慢,要登录等等问题
  3. Greenplum 查看连接与锁信息数据字典
  4. PostgreSQL 学习手册-模式Schema
  5. 004_Python3 注释
  6. fopen 的mode
  7. Linux下RabbitMQ的集群
  8. MySQL5.5安装步骤
  9. xyz
  10. HDU 4609 3-idiots ——(FFT)