Leetcode-1028 Convert to Base -2(负二进制转换)

 1 class Solution
 2 {
 3     public:
 4         string baseNeg2(int N)
 5         {
 6             string ss;
 7             int  n = N;
 8             stack<int> s;
 9             int a;
10             if(n == 0)
11                 return "0";
12             else
13             {
14                 while(n!=0)
15                 {
16                     a = abs(n%(-2));
17                     s.push(a);
18                     n = (n-a) / (-2);
19                 }
20             }
21             while(!s.empty())
22             {
23                 ss+=s.top()+‘0‘;
24                 s.pop();
25             }
26             return ss;
27         }
28 };

原文地址:https://www.cnblogs.com/Asurudo/p/10630804.html

时间: 2024-10-17 01:52:33

Leetcode-1028 Convert to Base -2(负二进制转换)的相关文章

《N诺机试指南》(六)负二进制转化习题

先看题目: 意思:将一个十进制数进行负二进制转化,将一个十进制数进行二进制转化大家都很清楚,取余再除2向下取整,接着反序输出 负二进制转化类似:1.对-2取余,再取绝对值 2.存入结果数组里 3.将数减去余数再除-2 4.反顺序打印出来 代码: //负二进制转换 /* 道理与 十进制转二进制一样 注意:有多组输入 */ #include <stdio.h> #include <math.h> int main(){ int n; int result[105]; while(sca

LeetCode OJ - Convert Sorted Array/List to Binary Search Tree

虚函数使用的时机 为什么虚函数不总是适用? 1. 虚函数有事会带来很大的消耗: 2. 虚函数不总是提供所需的行为: 3. 当我们不考虑继承当前类时,不必使用虚函数. 必须使用虚函数的情况: 1. 当你想删除一个表面上指向基类对象,实际却是指向派生类对象的指针,就需要虚析构函数. LeetCode OJ - Convert Sorted Array/List to Binary Search Tree,布布扣,bubuko.com LeetCode OJ - Convert Sorted Arra

转负二进制(个人模版)

转负二进制: 1 //POJ 3191 2 #include<stdio.h> 3 #include<string.h> 4 #include<algorithm> 5 using namespace std; 6 int ans[1000]; 7 int main() 8 { 9 int a; 10 while(~scanf("%d",&a)) 11 { 12 if(a==0) 13 { 14 printf("0\n")

leetcode -day19 Convert Sorted List to Binary Search Tree

1.  Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 分析:将一个升序排列的链表转换为平衡二叉搜索树,采用递归的方式,先找到链表的中点,作为二叉树的根,然后递归求解左右子树. 如下: class Solution { public: Tr

leetcode No109. Convert Sorted List to Binary Search Tree

Question: Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 把有序链表转化成平衡的BST Algorithm: 把链表转化成数组,再根据leetcode No108. Convert Sorted Array to Binary Search Tree的方法 找到数组中间的元素,作为根节点,则根节点左边是左子树,根节点

convert from base 10 to base 2

COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION Hence, we convert from base 10 to base 2 by repeated divisions by 2. The remainders and the final quotient, 1, give us, in order of increas-ing significance, the binary di

[Leetcode][BST][Convert Sorted Array to Binary Search Tree]

把一个排好序的vector转换为一颗二分查找树. 很简单的题目,递归即可,保证边界不要出错. 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 1

[LeetCode 题解]:Convert Sorted List to Binary Search Tree

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 题意:给定一个有序的链表,将其转换成平衡二叉搜索树 思路: 二分法 要构建一个平衡二叉树,二分法无疑是合适的,至于如何分是的代码简洁,就需要用到递归了. class Solution { public: // find middle element of the list Lis

c#常用工具类:文件和二进制转换

//================二进制相关转换类============== #region 将文件转换为二进制数组 /// <summary> /// 将文件转换为二进制数组 /// </summary> /// <param name="FilePath">文件完整路径</param> /// <returns>二进制数组</returns> public static byte[] FileToBinar