s*****e 发帖数: 115 | 1 有机会再上其他题目.
题目大意如下,请注意输入输出的格式,这里有309 digits 的要求:
https://en.wikipedia.org/wiki/Power_of_two
309 digits,相当于 2^1024
The fuel control mechanisms have three operations:
1) Add one fuel pellet
2) Remove one fuel pellet
3) Divide the entire group of fuel pellets by 2 (due to the destructive
energy released when a quantum antimatter pellet is cut in half, the safety
controls will only allow this to happen if there is an even number of
pellets)
Write a function called answer(n) which takes a positive integer as a string
and returns the minimum number of operations needed to transform the number
of pellets to 1. The fuel intake control panel can only display a number up
to 309 digits long, so there won't ever be more pellets than you can
express in that many digits.
For example:
answer(4) returns 2: 4 -> 2 -> 1
answer(15) returns 5: 15 -> 16 -> 8 -> 4 -> 2 -> 1
Test cases
==========
Inputs:
(string) n = "4"
Output:
(int) 2
Inputs:
(string) n = "15"
Output:
(int) 5 |
s******9 发帖数: 4623 | 2 leetcode 397 原题
除了3以外碰到odd number如果+1能被4整除就+1,不然就-1
或者用recursive夜行
easy级别,电话面试吗?
good luck
safety
string
【在 s*****e 的大作中提到】 : 有机会再上其他题目. : 题目大意如下,请注意输入输出的格式,这里有309 digits 的要求: : https://en.wikipedia.org/wiki/Power_of_two : 309 digits,相当于 2^1024 : The fuel control mechanisms have three operations: : 1) Add one fuel pellet : 2) Remove one fuel pellet : 3) Divide the entire group of fuel pellets by 2 (due to the destructive : energy released when a quantum antimatter pellet is cut in half, the safety : controls will only allow this to happen if there is an even number of
|
p**********e 发帖数: 151 | 3 但是这题输入是string,而且最大309位,怎么计算能否被4整除?
递归的话感觉可能会栈溢出,mantain一个stakc或者queue做迭代吗?
【在 s******9 的大作中提到】 : leetcode 397 原题 : 除了3以外碰到odd number如果+1能被4整除就+1,不然就-1 : 或者用recursive夜行 : easy级别,电话面试吗? : good luck : : safety : string
|
s******9 发帖数: 4623 | 4 怎么学的数学?
什么stack,queue,想得太复杂,只要考虑后两位就好了
100的整数倍都能被4整除,每次用helper function update一下string就好了,
比方说
“1231251241523”
看最后两位,23+1=24能被4整除,所以选择+1,除以2以后就是12
1231251241500用个list,考虑carry就可以了,算出来的number + 12就好了
【在 p**********e 的大作中提到】 : 但是这题输入是string,而且最大309位,怎么计算能否被4整除? : 递归的话感觉可能会栈溢出,mantain一个stakc或者queue做迭代吗?
|
s*****e 发帖数: 115 | 5
这里还有个输入的问题:
public static int answer(String n)
怎么把这个这么大(309 digits )的 string 转成 n?
【在 s******9 的大作中提到】 : 怎么学的数学? : 什么stack,queue,想得太复杂,只要考虑后两位就好了 : 100的整数倍都能被4整除,每次用helper function update一下string就好了, : 比方说 : “1231251241523” : 看最后两位,23+1=24能被4整除,所以选择+1,除以2以后就是12 : 1231251241500用个list,考虑carry就可以了,算出来的number + 12就好了
|
h******k 发帖数: 810 | 6 用 int[] 存储和运算。java BigInteger 就是这么玩的。
【在 s*****e 的大作中提到】 : : 这里还有个输入的问题: : public static int answer(String n) : 怎么把这个这么大(309 digits )的 string 转成 n?
|
h******k 发帖数: 810 | 7 另外,存二进制,奇偶/加减一/除二/整除四的运算都很简单。
【在 h******k 的大作中提到】 : 用 int[] 存储和运算。java BigInteger 就是这么玩的。
|
s*****e 发帖数: 115 | 8 有机会再上其他题目.
Google:
题目大意如下,请注意输入输出的格式,这里有309 digits 的要求
https://en.wikipedia.org/wiki/Power_of_two
309 digits,相当于 2^1024
The fuel control mechanisms have three operations:
1) Add one fuel pellet
2) Remove one fuel pellet
3) Divide the entire group of fuel pellets by 2 (due to the destructive
energy released when a quantum antimatter pellet is cut in half, the safety
controls will only allow this to happen if there is an even number of
pellets)
Write a function called answer(n) which takes a positive integer as a string
and returns the minimum number of operations needed to transform the number
of pellets to 1. The fuel intake control panel can only display a number up
to 309 digits long, so there won't ever be more pellets than you can
express in that many digits.
For example:
answer(4) returns 2: 4 -> 2 -> 1
answer(15) returns 5: 15 -> 16 -> 8 -> 4 -> 2 -> 1
Test cases
==========
Inputs:
(string) n = "4"
Output:
(int) 2
Inputs:
(string) n = "15"
Output:
(int) 5
EDIT:这个最后使用java 里面的BigInteger,不够时间也不确定python怎么做类似的事
情,一开始用BFS给出了答案,后来遇到big integer这个问题,最后用bit operation来做
):
Linkedin:
热身:pow(a,b)
第二题:156 Binary Tree Upside Down
(大意了,没做这道要收费的题目,这个我感觉是惨败,我真心觉得三哥已经非常耐心地引
导我,但是最后时间到了我没想出来,三哥主动给了我pseudo code:
= reverse(root->left)
right.left = root.right;
right.right = root;
root.right = null
return newroot, root
不过我当时也没理解题目,后来我发现有一个地方我没注意
当时三哥给的题目是这个例子:
*
* for example, turn these:
*
* 1 1
* / \ / \
* 2 3 2 3
* /
* 4
* / \
* 5 6
*
* into these:
*
* 1 1
* / /
* 2---3 2---3
* /
* 4
* /
* 5---6
*
* where 5 is the new root node for the left tree, and 2 for the right tree.
* oriented correctly:
*
* 5 2
* / \ / \
* 6 4 3 1
* \
* 2
* / \
* 3 1
*
*/
这样的话我当时的screen没办法同一个页面看到input和output,所以最后题目都不是很
看懂.下次应该要注意,其实那个intermediate step好像不是很有帮助,删掉的话可以一
下子看到input,output,可能容易理解题目 |
s******9 发帖数: 4623 | 9 leetcode 397 原题
除了3以外碰到odd number如果+1能被4整除就+1,不然就-1
或者用recursive夜行
easy级别,电话面试吗?
good luck
safety
string
【在 s*****e 的大作中提到】 : 有机会再上其他题目. : Google: : 题目大意如下,请注意输入输出的格式,这里有309 digits 的要求 : https://en.wikipedia.org/wiki/Power_of_two : 309 digits,相当于 2^1024 : The fuel control mechanisms have three operations: : 1) Add one fuel pellet : 2) Remove one fuel pellet : 3) Divide the entire group of fuel pellets by 2 (due to the destructive : energy released when a quantum antimatter pellet is cut in half, the safety
|
p**********e 发帖数: 151 | 10 但是这题输入是string,而且最大309位,怎么计算能否被4整除?
递归的话感觉可能会栈溢出,mantain一个stakc或者queue做迭代吗?
【在 s******9 的大作中提到】 : leetcode 397 原题 : 除了3以外碰到odd number如果+1能被4整除就+1,不然就-1 : 或者用recursive夜行 : easy级别,电话面试吗? : good luck : : safety : string
|
|
|
s******9 发帖数: 4623 | 11 怎么学的数学?
什么stack,queue,想得太复杂,只要考虑后两位就好了
100的整数倍都能被4整除,每次用helper function update一下string就好了,
比方说
“1231251241523”
看最后两位,23+1=24能被4整除,所以选择+1,除以2以后就是12
1231251241500用个list,考虑carry就可以了,算出来的number + 12就好了
【在 p**********e 的大作中提到】 : 但是这题输入是string,而且最大309位,怎么计算能否被4整除? : 递归的话感觉可能会栈溢出,mantain一个stakc或者queue做迭代吗?
|
s*****e 发帖数: 115 | 12
这里还有个输入的问题:
public static int answer(String n)
怎么把这个这么大(309 digits )的 string 转成 n?
【在 s******9 的大作中提到】 : 怎么学的数学? : 什么stack,queue,想得太复杂,只要考虑后两位就好了 : 100的整数倍都能被4整除,每次用helper function update一下string就好了, : 比方说 : “1231251241523” : 看最后两位,23+1=24能被4整除,所以选择+1,除以2以后就是12 : 1231251241500用个list,考虑carry就可以了,算出来的number + 12就好了
|
c*******y 发帖数: 98 | |
s**********g 发帖数: 14942 | 14 最后两位啊。。
【在 p**********e 的大作中提到】 : 但是这题输入是string,而且最大309位,怎么计算能否被4整除? : 递归的话感觉可能会栈溢出,mantain一个stakc或者queue做迭代吗?
|
v********n 发帖数: 40 | 15 妞妞牛
【在 s******9 的大作中提到】 : leetcode 397 原题 : 除了3以外碰到odd number如果+1能被4整除就+1,不然就-1 : 或者用recursive夜行 : easy级别,电话面试吗? : good luck : : safety : string
|