【Leetcode_easy】754. Reach a Number
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;
完
【Leetcode_easy】754. Reach a Number的更多相关文章
- 【LeetCode】754. Reach a Number 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学 日期 题目地址:https://leetcod ...
- 【Leetcode_easy】1137. N-th Tribonacci Number
problem 1137. N-th Tribonacci Number solution: class Solution { public: int tribonacci(int n) { ) ; ...
- LeetCode 754. Reach a Number
754. Reach a Number(到达终点数字) 链接:https://leetcode-cn.com/problems/reach-a-number/ 题目: 在一根无限长的数轴上,你站在0的 ...
- 【贪心】codeforces D. Minimum number of steps
http://codeforces.com/contest/805/problem/D [思路] 要使最后的字符串不出现ab字样,贪心的从后面开始更换ab为bba,并且字符串以"abbbb. ...
- 【LeetCode】9、Palindrome Number(回文数)
题目等级:Easy 题目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it rea ...
- 【leetcode】668. Kth Smallest Number in Multiplication Table
题目如下: 解题思路:几乎和[leetcode]719. Find K-th Smallest Pair Distance 的方法一样.只不过一个是减法一个是乘法,还有一点区别是[leetcode]7 ...
- 【Leetcode_easy】933. Number of Recent Calls
problem 933. Number of Recent Calls 参考 1. Leetcode_easy_933. Number of Recent Calls; 完
- 【Leetcode_easy】1128. Number of Equivalent Domino Pairs
problem 1128. Number of Equivalent Domino Pairs solution1: 不明白为什么每个元素都要加上count: class Solution { pub ...
- 【Leetcode_easy】806. Number of Lines To Write String
problem 806. Number of Lines To Write String solution: class Solution { public: vector<int> nu ...
随机推荐
- 并发编程大师系列之:线程的定义和中断 interrupt
1.启动线程的三种方式: 1.1继承Thread类 public static class UseThread extends Thread { public void run() { System. ...
- Django中出现:TemplateDoesNotExist at
setting文件中加入:
- C#经纬度加减运算(度°分′秒″格式)
经度是分和秒是按60进位,如果要做运算第一步就是转换成浮点数,之后就是计算和还原. using System.Text.RegularExpressions; public static double ...
- pip 安装包 不行 自己下载whl 包自己安装
https://www.lfd.uci.edu/~gohlke/pythonlibs/#twisted
- 自定义MVC--增删改查
使用自定义mvc框架完成CRUD操作 1.通用分页的jar.自定义mvc框架.自定义标签 导入jar.导入之前写好的pageTag.自定义mvc.xml 1.1导入需要用到的jar包 1.2导入之前已 ...
- iptables 相关命令
1. 清除已有iptables规则 iptables -F iptables -X iptables -Z 2. 开放指定的端口(添加规则) iptables -A INPUT -s 127.0.0. ...
- PHP变量及其操作
一.概念 变量是内存中用于存储数据的一个空间,这个空间有一个名字,这个名字就是变量名,变量名用于对这个内存中的数据进行引用的 二.声明 语法: $变量名=值 变量名只能包含字母.数字.下划线,只 ...
- NOIP刷题
搜索 [NOIP2013]华容道 最短路+带剪枝的搜索,是一个思维难度比较大的题目. CF1064D Labyrinth 考虑贪心,用双向队列bfs [NOIP2017]宝藏 剪枝搜索出奇迹 题解:h ...
- 爬虫(十八):scrapy分布式部署
scrapy部署神器-scrapyd -->GitHub地址 -->官方文档 一:安装scrapyd 安装:pip3 install scrapyd 这里我在另外一台ubuntu lin ...
- 为http请求追加cookie值
1.html中引入JQuery Cookie插件. 2.JS var expiresTime = new Date(); expiresTime.setTime(expiresTime.getTime ...