[LeetCode] Reach a Number 达到一个数字

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 reach the destination.

Example 1:

Input: target = 3
Output: 2
Explanation:
On the first move we step from 0 to 1.
On the second step we step from 1 to 3.

Example 2:

Input: target = 2
Output: 3
Explanation:
On the first move we step from 0 to 1.
On the second move we step  from 1 to -1.
On the third move we step from -1 to 2.

Note:

  • target will be a non-zero integer in the range [-10^9, 10^9].

s

原文地址:https://www.cnblogs.com/grandyang/p/8456022.html

时间: 2024-08-30 12:35:00

[LeetCode] Reach a Number 达到一个数字的相关文章

LeetCode:Missing Number - 缺失的数字

1.题目名称 Missing Number (缺失的数字) 2.题目地址 https://leetcode.com/problems/missing-number 3.题目内容 英文:Given an array containing n distinct numbers taken from 0, 1, 2, ..., n  find the one that is missing from the array. 中文:给出一个包含了n个不同数字的数组,从0开始一直到n,找出缺失的数字.如果数

LeetCode 9 Palindrome Number 回文数字

题目:Determine whether an integer is a palindrome. Do this without extra space. 翻译:判断一个数字是否是回文数,不要额外空间. 解题思路:因为数字既然传过去了,就不会有越界的问题.每次只需要取最前面和最后面的那一位数字进行比较,相同则继续,不同则返回. 首先要获取数字的位数,假设数字是12344321,一共有8位. 其次是要每次取前后各一位来进行比较,用数字除以1后面7个0得到第一位,用数字对10取余数得到最后一位. 此

LeetCode (30) Palindrome Number (回文数字)

题目描述 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) -- 负数不为回文 If you are thinking of converting the integer to string, note the restriction of using extra space.

[LeetCode] 268. Missing Number 缺失的数字

Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array. Example 1 Input: [3,0,1] Output: 2 Example 2 Input: [9,6,4,2,3,5,7,0,1] Output: 8 Note:Your algorithm should run in linear runtime c

[LeetCode] Convert a Number to Hexadecimal 数字转为十六进制

Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s complement method is used. Note: All letters in hexadecimal (a-f) must be in lowercase. The hexadecimal string must not contain extra leading 0s. If the nu

[LeetCode]68. Palindrome Number回文数字

Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using ext

LeetCode 136 Single Number(只出现一次的数字)

翻译 给定一个整型数组,除了某个元素外其余元素均出现两次.找出这个只出现一次的元素. 备注: 你的算法应该是一个线性时间复杂度.你可以不用额外空间来实现它吗? 原文 Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you i

LeetCode 137 Single Number II(只出现一次的数字 II)(*)

翻译 给定一个整型数组,除了某个元素外其余的均出现了三次.找出这个元素. 备注: 你的算法应该是线性时间复杂度.你可以不用额外的空间来实现它吗? 原文 Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you im

LeetCode 136 Single Number(仅仅出现一次的数字)

翻译 给定一个整型数组,除了某个元素外其余元素均出现两次. 找出这个仅仅出现一次的元素. 备注: 你的算法应该是一个线性时间复杂度. 你能够不用额外空间来实现它吗? 原文 Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could yo