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