俊介三

一天更新一点,一天积累一点

导航

随笔分类 -  Interview

摘要:实现这个函数pow(double x, int n)http://discuss.leetcode.com/questions/228/powx-n有几点要注意:1.n是整数,可以是正、负和02.x可以为03.使用divide and conquer.是的,一次for循环从0到|n|会超时,把它分成两半,时间复杂度立即减半代码入下:double pow(double x, int n) { // A trick to avoid -INT_MIN underflow if (n < 0) return 1.0 / pow(x, -(n+1)) / x; if (n == 0)... 阅读全文

posted @ 2013-03-11 20:41 俊介三在前进 阅读(166) 评论(0) 推荐(0)

摘要:Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.http://discuss.leetcode.com/questio 阅读全文

posted @ 2013-03-11 20:06 俊介三在前进 阅读(144) 评论(0) 推荐(0)

摘要:You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)Output: 7 -> 0 -> 8http://discuss.leet 阅读全文

posted @ 2013-03-11 13:09 俊介三在前进 阅读(163) 评论(0) 推荐(0)

摘要:问题:给一个排好序的数组arr[9]={1,3,5,7,19,23,22,33,100};再给某个数k,看它是否在此数组中,如果在,返回它的index,否则返回-1。使用遍历一遍查找的方法,最坏的时间复杂度是O(n).(假如第一个就找到了,时间复杂度是O(1),但这没有意义。一般情况,很可能找不到而变成就坏的情况。所以计算最坏时间复杂度非常有现实意义。)当然,这题使用二分查找,时间复杂度为O(logn)代码如下:View Code #include <stdio.h>int binarySearch(int* arr, int start, int end, int n){ int 阅读全文

posted @ 2013-03-10 22:42 俊介三在前进 阅读(126) 评论(0) 推荐(0)