随笔分类 -  LeetCode(Java)

摘要: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 阅读(111) 评论(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 阅读(127) 评论(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 阅读(106) 评论(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 阅读(143) 评论(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 阅读(126) 评论(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 阅读(176) 评论(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)
摘要:Add Binary:Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 题意:本题意思很明了,给定2个String,分别存储了用二进制表示的数,实现二进制的加法。 思路:从右向左逐位相加,注意进位即可。 代码: public S... 阅读全文
posted @ 2016-01-07 22:48 Lewisr 阅读(125) 评论(0) 推荐(0)
摘要:Contains Duplicate II: Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and ji... 阅读全文
posted @ 2016-01-07 22:36 Lewisr 阅读(870) 评论(0) 推荐(0)
摘要:Merge Sorted Array: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note: You may assume that nums1 has enough space (size that is greater or equal to m + n... 阅读全文
posted @ 2016-01-06 23:11 Lewisr 阅读(122) 评论(0) 推荐(0)
摘要:Majority Element: Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You ma... 阅读全文
posted @ 2016-01-06 23:00 Lewisr 阅读(156) 评论(0) 推荐(0)
摘要:Move Zeros: Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.For e... 阅读全文
posted @ 2016-01-06 22:50 Lewisr 阅读(123) 评论(0) 推荐(0)
摘要:Contains Duplicate:Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false ... 阅读全文
posted @ 2016-01-05 19:22 Lewisr 阅读(147) 评论(0) 推荐(0)
摘要:Remove Element: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.... 阅读全文
posted @ 2016-01-05 14:10 Lewisr 阅读(437) 评论(0) 推荐(0)
摘要:Plus One: Given a non-negative number represented as an array of digits, plus one to the number.The digits are stored such that the most significant d... 阅读全文
posted @ 2016-01-05 13:47 Lewisr 阅读(171) 评论(0) 推荐(0)