摘要: Ugly Number:Write a program to check whether a given number is an ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugl... 阅读全文
posted @ 2016-01-13 18:52 Lewisr 阅读(114) 评论(0) 推荐(0)
摘要: Power of Two: Given an integer, write a function to determine if it is a power of two. 题意:给定一个整数,判断其是否是2幂次方。 思路:如果一个整数是2的幂次方的话,用二进制可以表示为100…的形式,如8可以表示为100。在二进制的表示形式下只有一个1。判断n的2进制中1的个数。 代码: public bool... 阅读全文
posted @ 2016-01-12 19:40 Lewisr 阅读(112) 评论(0) 推荐(0)
摘要: Happy Number: Write an algorithm to determine if a number is "happy". A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of ... 阅读全文
posted @ 2016-01-12 19:33 Lewisr 阅读(191) 评论(0) 推荐(0)
摘要: Add Digits: Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. For example: Given num = 38, the proc... 阅读全文
posted @ 2016-01-11 19:40 Lewisr 阅读(128) 评论(0) 推荐(0)
摘要: Reverse Integer:Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return –321 题意:反转整数,不改变正负号。 思路: 逐步对给定的整数进行取余和求整,初始化最初的结果为result=0,然后result = result * 10 + 余数。最后注意判断是否溢出... 阅读全文
posted @ 2016-01-11 19:28 Lewisr 阅读(107) 评论(0) 推荐(0)
摘要: Longest Common Prefix:Write a function to find the longest common prefix string amongst an array of strings. 题意:查找一个字符串数组中的字符串的最长公共子序列。 思路:首先查找字符串数组中,字符串长度最小的字符串的索引,然后在逐位判断。 代码: public String longestC... 阅读全文
posted @ 2016-01-10 10:00 Lewisr 阅读(144) 评论(0) 推荐(0)
摘要: Length of Last Word:Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.If the last word does not exist, return 0. Note... 阅读全文
posted @ 2016-01-10 09:56 Lewisr 阅读(128) 评论(0) 推荐(0)
摘要: Compare Version Numbers: Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 b) return 1; if(a<b) return -1; } return 0; ... 阅读全文
posted @ 2016-01-08 22:38 Lewisr 阅读(177) 评论(0) 推荐(0)
摘要: Valid Parentheses:Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.The brackets m... 阅读全文
posted @ 2016-01-08 22:29 Lewisr 阅读(149) 评论(0) 推荐(0)
摘要: Valid Palindrome:Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example, "A man, a plan, a canal: Panama" is a palindrome. "race a car... 阅读全文
posted @ 2016-01-07 22:55 Lewisr 阅读(171) 评论(0) 推荐(0)