随笔分类 -  Integer

摘要:Given an array of integers, every element appearsthreetimes except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?Ref :http://fisherlei.blogspot.com/2013/11/leetcode-single-number-ii-solution.html[Thoughts] 阅读全文
posted @ 2014-02-20 14:45 Razer.Lu 阅读(213) 评论(0) 推荐(0)
摘要:Given an array of integers, every element appearstwiceexcept for one. Find that single one.Could you implement it without using extra memory?Ref:http://www.cnblogs.com/feiling/p/3349654.html[解题思路]以前看书时遇到过,一个数组中有一个元素出现一次,其他每个元素都出现两次要求空间复杂度为O(1)由于两个相同的元素异或的结果为0,而0^a==a,将数组中所有元素都进行异或,得到结果就是只出现一次的元素publ 阅读全文
posted @ 2014-02-18 08:41 Razer.Lu 阅读(193) 评论(0) 推荐(0)
摘要:Determine whether an integer is a palindrome. Do this without extra space.Some hints:Could negative integers be palindromes? (ie, -1)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 solved 阅读全文
posted @ 2014-02-07 03:58 Razer.Lu 阅读(230) 评论(0) 推荐(0)
摘要: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 you if you have already thought through this!If the integer's last digit is 0, what should the output be? ie, cas 阅读全文
posted @ 2014-02-03 03:09 Razer.Lu 阅读(177) 评论(0) 推荐(0)
摘要:public class Solution { public int romanToInt(String s) { int result = 0; if(s == null || s.length() == 0){ return result; } for(int i = 0; i 0 && cToI(s.charAt(i)) > cToI(s.charAt(i-1))){ // IV(4) result = 1 + 5 - 1*2 = 4 ... 阅读全文
posted @ 2014-01-28 13:58 Razer.Lu 阅读(153) 评论(0) 推荐(0)
摘要:public class Solution { public int divide(int dividend, int divisor) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. if(dividend == 0) return 0; if(divisor == 1) return... 阅读全文
posted @ 2014-01-26 16:48 Razer.Lu 阅读(142) 评论(0) 推荐(0)
摘要:参考:http://www.cnblogs.com/reynold-lei/p/3385290.html http://www.cnblogs.com/feiling/p/3302242.html罗马数字共有7个,即I(1)、V(5)、X(10)、L(50)、C(100)、D(500)和M(1000)。在较大的罗马数字的右边记上较小的罗马数字,表示大数字加小数字。在较大的罗马数字的左边记上较小的罗马数字,表示大数字减小数字。左减的数字有限制,仅限于I、X、C。比如45不可以写成VL,只能是XLV但是,左减时不可跨越一个位数。比如,99不可以用IC()表示,而是用XCIX()表示。(等同于阿拉. 阅读全文
posted @ 2014-01-25 05:51 Razer.Lu 阅读(229) 评论(0) 推荐(0)
摘要:public class Solution { public int atoi(String str) { if(str.length() == 0 || str == null){ return 0; } str = str.trim(); boolean positiveflag = true; if(str.charAt(0) == '+'){ str = str.substring(1); }else if(str.cha... 阅读全文
posted @ 2014-01-23 16:04 Razer.Lu 阅读(177) 评论(0) 推荐(0)