数字 (digits)
文章平均质量分 58
beiyetengqing
http://blog.csdn.net/beiyeqingteng 的镜像站
展开
-
找出丢失的数字 (XOR)
问题:有一组数字,从1到n中减少了一个数,顺序也被打乱了,放在一个n-1的数组里,请找出丢失的数字。分析:这类题是频率很高的面试题,这类题的主要思路还是用XOR位操作,利用XOR位操作的原理是如果两个相同的数进行XOR操作,结果是0,而0与数A进行位操作,结果仍旧是A。所以,我们可以用1^2^...^n的结果逐个异或当前输入数据,而最后的结果是丢失的那个数字。时间复杂度为O(n),空间原创 2012-08-29 08:21:43 · 1548 阅读 · 0 评论 -
快速找出2到n所有的素数
public static ArrayList primeNumbers(int n) { if (n < 2) return null; ArrayList list = new ArrayList(); boolean[] prime = new boolean[n + 1]; for (int i = 2; i < n + 1; i++) { prime[i] = tr原创 2013-03-11 12:29:40 · 2652 阅读 · 0 评论 -
n 个人围成一圈(编号1-n),数到3的出列,最后剩下的人的编号
问题:n 个人围成一圈(编号1-n),数到3的出列,然后又从头开始数,一直循环到最后一个人,请问最后剩下的人的编号?public int numberToExit(int total, int interval) { boolean[] arr = new boolean[total]; for(int i = 0; i < arr.length; i++) { arr[i]原创 2012-11-18 16:46:57 · 4153 阅读 · 0 评论 -
add two numbers without using +
Question:Add two numbers without using "+".Analysis:If "+" is not allowed, we have to use the bit manipulation. public static int add(int num1, int num2) { if (num1 == 0) return num2; int原创 2013-02-11 02:46:39 · 506 阅读 · 0 评论 -
Divide Two Integers
Divide two integers without using multiplication, division and mod operator.public class Solution { public int divide(int dividend1, int divisor1) { if (divisor1 == 1) return dividend1;原创 2013-02-03 09:59:16 · 2063 阅读 · 1 评论 -
3Sum Closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exa原创 2013-01-18 06:32:53 · 930 阅读 · 1 评论 -
Integer to Roman
Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.public class Solution { public String intToRoman(int number) { int[] value原创 2013-01-25 13:14:30 · 572 阅读 · 0 评论 -
Valid Number
Validate if a given string is numeric.Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => trueNote: It is intended for the problem statement to be amb原创 2013-01-02 12:00:43 · 2187 阅读 · 0 评论 -
Roman to Integer
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.Rule: 从前往后扫描,用一个临时变量记录分段数字。如果当前处理的字符对应的值和上一个字符一样,那么临时变量加上这个字符。比如III = 3如果当前比前一个转载 2013-01-02 13:40:43 · 12303 阅读 · 1 评论 -
pow(double a,int b)
实现pow(double a, int b) 最简单直接的方法就是用一个循环使得result *= a 做b次,但是,这样的方法不是最优的,比如我们求2^100, 我们只需要得到2^50就可以了,因为2^100 = 2^50 * 2^50。而要求2^50,只需要得到2^25就可以了,所以,按照这样一个思路,要求pow(double a,int b),我们可以按照如下方法做:public c原创 2012-08-29 23:36:35 · 3233 阅读 · 0 评论 -
3SUM
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note:Elements in a triplet原创 2012-12-07 01:12:45 · 665 阅读 · 0 评论 -
Largest Rectangle in Histogram
Question:Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.Above is a histogra原创 2012-12-05 03:20:01 · 5090 阅读 · 0 评论 -
不通过比较,找出两个数的最大值
问题:比如:给定两个值 5和10,不通过比较,直接找出最大值。分析:一旦涉及到不用比较找最大值,想都不用想,一般只能通过位运算来实现。 max = a - ((a-b)&((a-b)>>31))或者max = ((a+b)+|a-b|)/2如果找最小值,我们只需把两个值相加,减去max即可。转载请注明出处:http://blog.csdn.net/beiye原创 2012-11-19 02:21:28 · 934 阅读 · 0 评论 -
给一个钱数,用最少的纸币使之等于这个钱数
问题:给一个钱数,用最少的纸币使之等于这个钱数。 比如, 纸币的面额为 1, 2, 5, 10。则如果给你15块钱,你只能返回一张10块,一张5块。分析:使用DP,创建一个数组,里面保存用0块到给定块数的最小纸币数。然后利用递归函数F(n) = Min_{j \in 纸币面额}F(n - j ) + 1。代码:public static int findMinimumB原创 2012-12-02 07:29:55 · 1798 阅读 · 0 评论 -
shuffle a deck of cards
Question:You are given a deck of 52 cards, and you need to shuffle the cards and make sure the order is random.Solution:We can simply use the Durstenfeld and Knuth algorithm to shuffle the cards原创 2012-12-02 06:36:06 · 1983 阅读 · 0 评论 -
不占用任何额外空间的情况下交换两个数的值
问题:给你两个整数,比如 a = 10, b = 20,在不占用任何额外空间的条件下交换两个数的值。思路:首先,问题看起来是挺不可思议,但是也是挺有趣的。既然不能占用任何空间,只能在已有的变量上打主意。这里有两个方法,看完代码自己就能马上明白,所以就不多解释了。方法1:int a = 10;int b = 20;a = a^b;b = b^a;a = a^b;这个方原创 2012-08-29 06:55:54 · 912 阅读 · 0 评论 -
两个巨大正整数相加
问题:两个巨大整数相加,可能会造成溢出,或者它的大小已经超出基本数据类型的范围,所以,我们对巨大整数进行相加时,可以把它们转换成字符串,然后通过字符串的处理进行整数相加。这里有两种做法:第一种,把整数存在一个字符数组里进行处理。代码如下:public static String addThroughArray(String a, String b) { char[] adde原创 2012-08-24 12:18:54 · 942 阅读 · 0 评论 -
Sqrt(x)
Implement int sqrt(int x). int sqrt(int x) { if (x < 0) return -1; int start = 0; int end = x; if (x == 0 || x == 1) return x; while (start <= end) {原创 2012-12-30 05:09:18 · 1156 阅读 · 1 评论