摘要: Implementatoito convert a string to an integer.Hint:Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.Notes:It is intended for this problem to be specified vaguely (ie, no given input specs). You are respo 阅读全文
posted @ 2012-11-14 21:38 chkkch 阅读(2166) 评论(1) 推荐(0) 编辑
摘要: Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e.,0 1 2 4 5 6 7might become4 5 6 7 0 1 2).You are given a target value to search. If found in the array return its index, otherwise return -1.You may assume no duplicate exists in the array.二分搜索来找到转折点,也就是最小数的位置。对二分搜索要稍作修改, 阅读全文
posted @ 2012-11-14 21:23 chkkch 阅读(5831) 评论(0) 推荐(0) 编辑
摘要: 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 ofO(logn).If the target is not found in the array, return[-1, -1].For example,Given[5, 7, 7, 8, 8, 10]and target value 8,return[3, 4].二分找最左端,再二 阅读全文
posted @ 2012-11-14 16:30 chkkch 阅读(3099) 评论(0) 推荐(0) 编辑
摘要: Write an efficient algorithm that searches for a value in anmxnmatrix. This matrix has the following properties:Integers in each row are sorted from left to right.The first integer of each row is greater than the last integer of the previous row.For example,Consider the following matrix:[ [1, 3, ... 阅读全文
posted @ 2012-11-14 16:21 chkkch 阅读(1779) 评论(1) 推荐(0) 编辑
摘要: Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value.用树的遍历来检查。 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 *... 阅读全文
posted @ 2012-11-14 16:12 chkkch 阅读(426) 评论(0) 推荐(0) 编辑
摘要: Reverse digits of an integer.Example1:x = 123, return 321Example2:x = -123, return -321每次取出个位,然后放到新的数里。 1 class Solution { 2 public: 3 int reverse(int x) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 int sign = x > 0 ? 1 : -1; 7 ... 阅读全文
posted @ 2012-11-14 16:07 chkkch 阅读(1769) 评论(1) 推荐(0) 编辑
摘要: Given a string containing only digits, restore it by returning all possible valid IP address combinations.For example:Given"25525511135",return["255.255.11.135", "255.255.111.35"]. (Order does not matter)DFS搜索所有的可能解,同时判断解的合法性。 1 class Solution { 2 private: 3 vector<s 阅读全文
posted @ 2012-11-14 16:00 chkkch 阅读(3108) 评论(1) 推荐(0) 编辑
摘要: Given a linked list, remove thenthnode from the end of list and return its head.For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5.Note:Givennwill always be valid.Try to do this in one pass.依然 阅读全文
posted @ 2012-11-14 15:41 chkkch 阅读(8334) 评论(0) 推荐(0) 编辑
摘要: Given an array and a value, remove all instances of that value in place and return the new length.The order of elements can be changed. It doesn't matter what you leave beyond the new length.依然是双指针思想 1 class Solution { 2 public: 3 int removeElement(int A[], int n, int elem) { 4 // Start ... 阅读全文
posted @ 2012-11-14 15:32 chkkch 阅读(4423) 评论(0) 推荐(0) 编辑
摘要: Follow up for "Remove Duplicates":What if duplicates are allowed at mosttwice?For example,Given sorted array A =[1,1,1,2,2,3],Your function should return length =5, and A is now[1,1,2,2,3].接上题,增加一个count变量来记录key出现的次数。 1 class Solution { 2 public: 3 int removeDuplicates(int A[], int n) { 4 . 阅读全文
posted @ 2012-11-14 15:28 chkkch 阅读(1333) 评论(0) 推荐(1) 编辑
摘要: Given a sorted array, remove the duplicates in place such that each element appear onlyonceand return the new length.Do not allocate extra space for another array, you must do this in place with constant memory.For example,Given input array A =[1,1,2],Your function should return length =2, and A is 阅读全文
posted @ 2012-11-14 15:20 chkkch 阅读(5249) 评论(0) 推荐(1) 编辑
摘要: Implement pow(x,n).用二分法,O(logn)。注意n < 0的处理 1 class Solution { 2 public: 3 double power(double x, int n) 4 { 5 if (n == 0) 6 return 1; 7 8 double v = power(x, n / 2); 9 10 if (n % 2 == 0)11 return v * v;12 else13... 阅读全文
posted @ 2012-11-14 15:12 chkkch 阅读(2229) 评论(0) 推荐(0) 编辑