摘要: Determine whether an integer is a palindrome. Do this without extra space.click to show spoilers.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. Howe 阅读全文
posted @ 2013-10-17 11:13 果汁果粒 阅读(138) 评论(0) 推荐(0) 编辑
摘要: 比较简单的题目,但是还是提交了好几遍才A掉一一!需要考虑的:(1)字符串前面的空格 trim()或者while()(2)正负符号(3)只取最前面的数字字符,一旦出现非数字字符后面即使有数字也不考虑了(4)空字符串(5)溢出:最大数最小数两种 遗忘了一种情况 ,出现了下面的错误。解决方法是:加上|| (sum>=1000000000)Submission Result:Wrong AnswerInput:" -11919730356x"Output:965171532Expected:-2147483648正确代码如下:public class Solution { p 阅读全文
posted @ 2013-10-15 21:54 果汁果粒 阅读(197) 评论(0) 推荐(0) 编辑
摘要: Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.仔细理解罗马数的规则,把IVIX等也作为基本元素列入对应表中。然后每次选择能表示的最大值,把对应的字符串连起来。直到剩下的数=0为止。public class Solution { public String intToRoman(int num) { // Note: The Solution object is instantiated only once an... 阅读全文
posted @ 2013-10-09 22:41 果汁果粒 阅读(153) 评论(0) 推荐(0) 编辑
摘要: Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.思路:首先搞清楚罗马数的表示方式,百度百科告诉我们:基本字符IVXLCDM相应的阿拉伯数字表示为1510501005001000相同的数字连写,所表示的数等于这些数字相加得到的数,如:Ⅲ = 3;小的数字在大的数字的右边,所表示的数等于这些数字相加得到的数, 如:Ⅷ = 8;Ⅻ = 12;小的数字,(限于Ⅰ、X 和C)在大的数字的左边,所表示的数等于大数减小数得到的数,如:Ⅳ= 4;Ⅸ 阅读全文
posted @ 2013-10-09 22:37 果汁果粒 阅读(177) 评论(0) 推荐(0) 编辑
摘要: Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.想 阅读全文
posted @ 2013-10-07 11:21 果汁果粒 阅读(132) 评论(0) 推荐(0) 编辑
摘要: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H NA P L S I I GY I RAnd then read line by line: "PAHNAPLSIIGYIR"Write the code that will take a string 阅读全文
posted @ 2013-10-06 11:07 果汁果粒 阅读(226) 评论(0) 推荐(0) 编辑
摘要: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321click to show spoilers.Have 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 sho 阅读全文
posted @ 2013-10-06 10:50 果汁果粒 阅读(150) 评论(0) 推荐(0) 编辑
摘要: Map map = new HashMap(); String[] ipList=IpList.split("_"); boolean ipIn=false; for(int i=0;i ipe){ long t = ips; ips = ipe; ipe = t; } if((ips <= ipt && ipt <= ipe)){ ... 阅读全文
posted @ 2013-09-30 15:55 果汁果粒 阅读(651) 评论(0) 推荐(0) 编辑
摘要: There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).1、如果把A和B合并成C,再取中位数。复杂度应该是O(m+n),不满足要求。2、要清楚的一个概念是:如果(m+n)为偶数,中位数应该是(m+n)个数排序后,位于中间的两个数的加和除以2.0。3、没有思路……用了这篇:http://www.cnblogs.com/lautsi 阅读全文
posted @ 2013-09-30 15:47 果汁果粒 阅读(195) 评论(0) 推荐(0) 编辑
摘要: Given a stringS, find the longest palindromic substring inS. You may assume that the maximum length ofSis 1000, and there exists one unique longest palindromic substring.自己只想到了O(n2)的算法,遍历每个字符,然后从中间向两旁扩,寻找最长子串。显示的结果一直是pending...http://www.felix021.com/blog/read.php?2040有O(n)的算法publicclassSolution {pu 阅读全文
posted @ 2013-09-26 22:24 果汁果粒 阅读(184) 评论(0) 推荐(0) 编辑