【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 ...
随机推荐
- 泛型 System.Collections.Generic及泛型继承、运算符、结构、接口、方法、委托、事件、可空类型等
一.定义泛型类 void Main() { //实例化泛型类时,才指定具体的类型 MyGenericClass<); Console.WriteLine(MyGeneri.InnerT1Obje ...
- JAVA系列:浅谈Java中的equals和==
在初学Java时,可能会经常碰到下面的代码: 1 String str1 = new String("hello"); 2 String str2 = new String(&qu ...
- django安装过程简介(Mac版)
Django 中的项目和应用是什么? 简单来说,可以认为
- 2019-2020-1 20199312《Linux内核原理与分析》第二周作业
c语言代码 // main.c int g(int x) { return x + 4; } int f(int x) { return g(x); } int main(void) { return ...
- B5G/6G新技术
组网技术:由自组织向自支撑发展:卫星通信(大尺度衰落)采用DTN组网. 多址技术:非正交多址:Polar-SCMA:交织多址:IDMA. 信道技术:多径分集.多普勒分集.OFDM的CP用ZP替代.设计 ...
- 2019年京东 PHP工程师面试题
1. Apache与Nginx大访问下性能描述正确的是? A.Apache所采用的epoll网络I/O模型非常高效B.Nginx使用了最新的kqueue和select网络I/O模型C.Apache进程 ...
- Oracle ORA-00600[2662] 解决
一.问题描述 1.数据库情况 1)数据库版本:11.2.0.4: 2)未开启归档: 3)没有备份:无RMAN备份.无DUMP备份: 4)数据库redo log 日志组,每组只有一个成员: 2.问题出现 ...
- 011_Python3 集合
集合(set)是一个无序的不重复元素序列. 可以使用大括号 { } 或者 set() 函数创建集合,注意:创建一个空集合必须用 set() 而不是 { },因为 { } 是用来创建一个空字典. 创 ...
- 系列属性(offset、scroll、client)
一.offset系列属性 <div id="dv"></div> <!-- 已在style标签里设置div宽高各100px --> <sc ...
- 炸掉的fft,改天再调
#include <iostream> #include <cstdio> #include <cmath> #include <algorithm> ...