
遇到过的算法题
文章平均质量分 84
huayunlong1
这个作者很懒,什么都没留下…
展开
-
[转]给定能随机生成整数1到5的函数,写出能随机生成整数1到7的函数
问题:给定能随机生成整数1到5的函数,写出能随机生成整数1到7的函数。解答:假设我们要等概率生成一个3位的10进制数(000 - 999),我们可以在 随机生成整数0到9的函数 基础上,随机生成3个数字组成三位数就得到了结果。这里类似,我们首先必须认识到:任何一个数都可以用5进制的数来表示,如12 = 5进制(22) = 2*5 + 2。因此假设我们要随机生成[0,转载 2015-11-14 14:33:55 · 548 阅读 · 0 评论 -
不用加法做加法。。
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.需要用位运算,比如当11101和10111两个数相加时, 0 1 1 1 0 1 0 1 0 1 1 1 1 1 0 1 0 0运算时可以发现,如果不考虑进位的问题,1和1,0和0原创 2016-08-20 14:08:32 · 350 阅读 · 0 评论 -
Power of Three
Given an integer, write a function to determine if it is a power of three. Could you do it without using any loop / recursion?bool isPowerOfThree(int n) { if(n==0) {原创 2016-09-05 09:50:51 · 184 阅读 · 0 评论 -
判断一个整数是否是回文
题目:Determine whether an integer is a palindrome. Do this without extra space正确代码bool isPalindrome(int x) { if(x int len=log10(x); while(len>0) { i原创 2016-10-15 15:37:24 · 706 阅读 · 0 评论 -
寻找两个相交链表的第一个公共节点的问题
在leetcode做题时遇到的问题,首先是最后通过的代码class Solution {public: ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { if(headA==NULL||headB==NULL) return NULL;原创 2016-11-06 13:26:52 · 282 阅读 · 0 评论 -
两个字符串表示的二进制数求和
这是自家的代码string addBinary(string a, string b) { int lena=a.size(); int lenb=b.size(); if(lena==0) return b; if(lenb==0) return a;原创 2016-11-09 10:16:22 · 1587 阅读 · 0 评论