上一页 1 ··· 6 7 8 9 10 11 12 13 14 ··· 16 下一页
摘要: Implement pow(x, n).Solution: recursion. 1 class Solution { 2 public: 3 double pow(double x, int n) { 4 if(x < 0) return n % 2 == 0 ? pow(-x, n) : -pow(-x, n); 5 if(x == 0 || x == 1) return x; 6 if(n == 0) return 1.0; 7 if(n < 0) return 1.0/pow(x, -n); 8 ... 阅读全文
posted @ 2014-04-05 01:00 beehard 阅读(106) 评论(0) 推荐(0) 编辑
摘要: Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.Solution: use or (clean) 1 class Solution { 2 public: 3 int romanToInt(string s) { 4 unordered_map map; 5 map['M'] = 1000; 6 map['D'] = 500; 7 map['C'] = 10... 阅读全文
posted @ 2014-04-05 00:58 beehard 阅读(109) 评论(0) 推荐(0) 编辑
摘要: Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.Solution: Buffer the roman numbers. 1 class Solution { 2 public: 3 string rome[4][10] = {{"", "I", "II", "III", "IV", "V", "VI&qu 阅读全文
posted @ 2014-04-05 00:55 beehard 阅读(125) 评论(0) 推荐(0) 编辑
摘要: Divide two integers without using multiplication, division and mod operator. 1 class Solution { 2 public: 3 int divide(int dividend, int divisor) { 4 assert(divisor != 0); 5 int res = 0; 6 bool flag = (dividend > 0 && divisor > 0) || (dividend < 0 && divisor < 0); 7 ... 阅读全文
posted @ 2014-04-05 00:54 beehard 阅读(121) 评论(0) 推荐(0) 编辑
摘要: Given an array of integers, every element appears three times except for one.Find that single one.Your algorithm should have a linear runtime complexity. Could you implement itwithout using extra memory?Solution: Count the number of each bit. 1 class Solution { 2 public: 3 // assume that integer... 阅读全文
posted @ 2014-04-05 00:51 beehard 阅读(126) 评论(0) 推荐(0) 编辑
摘要: Given an array of integers, every element appears twice except for one.Find that single one.Your algorithm should have a linear runtime complexity.Could you implement it without using extra memory?Solution: XOR. 1 class Solution { 2 public: 3 int singleNumber(int A[], int n) { 4 int sing... 阅读全文
posted @ 2014-04-05 00:49 beehard 阅读(112) 评论(0) 推荐(0) 编辑
摘要: Determine whether an integer is a palindrome. Do this without extra space.Some hints:Could negative integers be palindromes? (ie, -1) (No!)If you are thinking of converting the integer to string, note the restriction of using extra space.You could also try reversing an integer. However, if you have 阅读全文
posted @ 2014-04-05 00:48 beehard 阅读(143) 评论(0) 推荐(0) 编辑
摘要: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice?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].Solution:Two pointers ('last' and 'lastlast'). 1 class Solution { 2 public: 3 int 阅读全文
posted @ 2014-04-05 00:43 beehard 阅读(144) 评论(0) 推荐(0) 编辑
摘要: Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Your goal is to reach the last index in the minimum number of jumps.For example:Given array A = [2,3,1,1,4]The minimum 阅读全文
posted @ 2014-04-05 00:41 beehard 阅读(140) 评论(0) 推荐(0) 编辑
摘要: Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determine if you are able to reach the last index.For example:A = [2,3,1,1,4], return true.A = [3,2,1,0,4], return fals 阅读全文
posted @ 2014-04-05 00:39 beehard 阅读(134) 评论(0) 推荐(0) 编辑
上一页 1 ··· 6 7 8 9 10 11 12 13 14 ··· 16 下一页