Can you solve this real interview question? Convert to Base -2 - Given an integer n, return a binary string representing its representation in base -2. Note that the returned string should not have leading zeros unless the string is "0". Example 1:
classSolution(object):defbaseNeg2(self, N):""":type N: int :rtype: str"""bs= bin(N)[2:][::-1] res= [int(i)foriinlist(bs)]defcarrier(res,i):ifi == len(res) - 1: res+= [1]iflen(res) % 2 ==0: res+= [1]else: res[i+ 1] += 1foriinrange(1,len(res)):if...
比如 -3 除以2,等于 -1,但是右移一位却不等于 -1,-3 的八位的表示为 11111101,右移一位是 11111110,是 -2。 Github 同步地址: https://github.com/grandyang/leetcode/issues/1017 类似题目: Encode Number 参考资料: https://leetcode.com/problems/convert-to-base-2/ https://leetcode.com/problems...
public class Solution { public String convertToBase7(int num) { int base = 1, result = 0; while (num != 0) { result += base * (num % 7); num /= 7; base *= 10; } return String.valueOf(result); } } 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11....
基本思路同 LeetCode: 108. Convert Sorted Array to Binary Search Tree 题解。由于给定数据结构是链表,因此只能先生成左子树,以使迭代指针指向链表中根节点的元素。 AC 代码 /** * Definition for singly-linked list. * struct ListNode { ...
LeetCode 2027 - 转换字符串的最少操作次数 (Python3|Go)[贪心] Minimum Moves to Convert String 满赋诸机 前小镇做题家,现大厂打工人。题意 给定一个只含有 'X' 和 'O' 的字符串,每次可以将任意连续长度为 3 的子串变为 "OOO" 。 求最少多少次可以将字符串变为只含有 'O' ? 数据限制 3 <= s....
原题链接:https://leetcode.com/problems/convert-a-number-to-hexadecimal/ Given an integer, write an algorithm to convert it to hexadecimal. For negative integer,two’s complementmethod is used. Note: All letters in hexadecimal (a-f) must be in lowercase. ...
LeetCode 109. Convert Sorted List to Binary Search Tree Description Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. For this problem, a height-balancedbinary treeis defined as a binary tree in which the depth of the two subtrees...
The given number is guaranteed to fit within the range of a 32-bit signed integer. Youmust not useanymethod provided by the librarywhich converts/formats the number to hex directly. Example 1: Input: 26 Output: "1a" Example 2:
405. 数字转换为十六进制数 - 给定一个整数,编写一个算法将这个数转换为十六进制数。对于负整数,我们通常使用 补码运算 [https://baike.baidu.com/item/%E8%A1%A5%E7%A0%81/6854613?fr=aladdin] 方法。 答案字符串中的所有字母都应该是小写字符,并且除了 0 本身之外,答