leetcode754】的更多相关文章

You are standing at position 0 on an infinite number line. There is a goal at position target. On each move, you can either go left or right. During the n-th move (starting from 1), you take n steps. Return the minimum number of steps required to rea…
class Solution { public: int reachNumber(int target) { // 理解这题的意思 这题就好做了 // 分析 首先考虑一种比较极端的情况 即一直向正方向移动n步 ,刚好达到target // 那么target的值就等于前n步的和 ,也就是1+2+.....+n = n*(n+1)/2 // 如果n(n+1)/2>target ,那么所需要的步数肯定要比n多,而且肯定有向左走的步子,也就是求和的时候肯定是有负数的,至于哪个或者哪些个为负,下面开始讨论…