
number process
文章平均质量分 56
benbenab
这个作者很懒,什么都没留下…
展开
-
Sqrt(x)
Implement int sqrt(int x).Compute and return the square root of x.二分法:class Solution {public: int sqrt(int x) { // Start typing your C/C++ solution below //原创 2012-11-04 10:55:12 · 366 阅读 · 0 评论 -
[leetcode ] gray code
class Solution {public: vector grayCode(int n) { vector results; do{ if (n < 0) break; else if (n == 0) { resul原创 2013-12-24 15:31:00 · 671 阅读 · 0 评论 -
Maximum Subarrary
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray [4,−1,2,1] ha原创 2013-10-16 13:18:45 · 637 阅读 · 0 评论 -
[leetcode]
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array.原创 2013-10-08 13:01:56 · 479 阅读 · 0 评论 -
reverse interger
Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321Have you thought about this?Here are some good questions to ask before coding. Bonus points for原创 2013-09-20 13:40:27 · 765 阅读 · 0 评论 -
Divide Two Integers
class Solution {public: int divide(int dividend, int divisor) { // Start typing your C/C++ solution below // DO NOT write int main() function if (divisor == 0)原创 2013-02-02 13:07:45 · 959 阅读 · 0 评论 -
Search for a Range !!!
Given a sorted array of integers, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the order of O(log n).If the target is not found in t原创 2013-02-02 08:44:24 · 361 阅读 · 0 评论 -
求最大prime
不知道这个的复杂度怎么分析。 不过worst case 应该是O(n),average 可能是O(sqrt(n))int maxPrime(int n){ assert(n>=0); if(n <= 3) return n; int k = 1; while(n > 1) { ++k; if(k > n / k) ret转载 2013-01-28 00:50:23 · 409 阅读 · 0 评论 -
leetcode power (x,n)
1. 考虑double float 的相等,不能仅仅用==表示,是有精度限制的。2. n 大于0,小于0的情况3. if (temp_diveded & 1) result *= base; base *= base; temp_diveded >>= 1;想法很赞!!!!#define AC原创 2013-01-23 08:08:07 · 777 阅读 · 0 评论 -
[leetcode] integer 与 Roman的转换
1. integer到roman利用了数组的位置的信息class Solution {public: string intToRoman(int num) { // Start typing your C/C++ solution below // DO NOT write int main() function char原创 2013-01-05 23:54:07 · 302 阅读 · 0 评论 -
expression 5+4*(7-15) or have parenthesis in any order // 波兰表示法
Answers write code in java /c for expression 5+4*(7-15) or have parenthesis in any order .idea: Use two stack, one for number, one for operator. Every time push a number or an operation into stack.原创 2012-12-16 22:53:57 · 505 阅读 · 0 评论 -
[leetcode] Decode Ways (!!)
A message containing letters from A-Z is being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine the total nu原创 2013-01-02 02:33:11 · 558 阅读 · 0 评论 -
求一个无序数组的子数组, 子数组的和最接近0
方法讨论:http://blog.kingsamchen.com/archives/649typedef struct{ int value; int index;}info;bool less_b (const info& info1, const info& info2){ return info1.value < info2.value;}int * temp = new原创 2012-12-10 22:25:28 · 873 阅读 · 0 评论 -
【leetcode】 4 sum
4SumGiven an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.No原创 2012-12-22 09:38:10 · 1129 阅读 · 1 评论 -
数组中出现次数超过一半的数字
数组中有一个数字出现了超过数组长度的一般,请找出这个数字。思路:如果这个数子长度超过一半,那肯定排序后他会出现在middle的位置上!int Partition(int* num, unsigned int length, int start, int end){ int pivot = num[end]; int i = start-1; int index; for转载 2012-11-23 05:04:09 · 467 阅读 · 0 评论 -
数值的整数次方 和开根号
1. 数值的整数次方 a. 不考虑大数情况 #define PRECISION 0.00001double pow(double base, int exp){ if (equal(base,0)) return 0; if (exp == 0) return 1.0; bool negative = (exp>0)?false:true;原创 2012-11-22 05:33:12 · 1704 阅读 · 0 评论 -
LeetCode Single Number I & II 都符合两个问题额外要求的 通用解法 与 思考过程
Single NumberGiven an array of integers, every element appears twice except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it转载 2014-10-06 12:34:50 · 618 阅读 · 0 评论