LeetCode题库解析
主要是介绍LeetCode的题目解析,从思路到具体的代码
fesng
CSDN年度博客之星和博客专家,毕业长期在一线互联网专注于Android开发以及Android漏洞挖掘研究。
展开
-
[leetcode]-Linked List Random Node
题目描述:给定一个单向的链表,要求以相等的概率返回要求:时间复杂度o(n),空间复杂度o(1)原文描述:Given a singly linked list, return a random node’s value from the linked list. Each node must have the same probability of being chosen.Follow up:Wha原创 2016-08-18 00:48:28 · 1704 阅读 · 1 评论 -
【leetcode82】Linked List Cycle
题目描述:判断有序list是不是环要求:时间复杂度o(n)原文描述:Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?思路:设置两个指针,一个快指针,每次走两步,一个慢指针,每次走一步如果块指针==慢指针,那么包含环注意判断空值和原创 2016-08-01 00:18:44 · 1695 阅读 · 0 评论 -
【leetcode81】Product of Array Except Self
题目描述:给定一个长度为n的整数数组Array【】,输出一个等长的数组result【】,这个输出数组,对应位置i是除了Array【i】之外,其他的所有元素的乘积例如:given [1,2,3,4], return [24,12,8,6].要求:时间复杂度是o(n)原文描述:Given an array of n integers where n > 1, nums, return an array原创 2016-07-31 22:45:19 · 1465 阅读 · 0 评论 -
【leetcode80】Reverse Vowels of a String(元音字母倒叙)
题目描述:写一个函数,实现输入一个字符串,然后把其中的元音字母倒叙注意元音字母包含大小写,元音字母有五个a,e,i,o,u原文描述:Write a function that takes a string as input and reverse only the vowels of a string.Example 1:Given s = “hello”, return “holle”.Examp原创 2016-07-31 16:58:34 · 1508 阅读 · 0 评论 -
【leetcode79】Single Number III
题目描述:给定一个数组,里面只有两个数组,只是出现一次,其余的数字都是出现两次,找出这个两个数字,数组形式输出原文描述:Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two原创 2016-07-31 16:16:49 · 1439 阅读 · 0 评论 -
【leetcode78】Single Number II
题目描述:给定一个数组,里面除了一个数字,其他的都出现三次。求出这个数字原文描述:Given an array of integers, every element appears three times except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity.原创 2016-07-31 15:29:00 · 1351 阅读 · 0 评论 -
【leetcode77】Single Number
题目描述:给定一个数组,只有一个数字出现两次,判断那个数字思路:不断取出数据进行异或,最后一个数字,因为相同的数字会抵消代码:public class Solution { public int singleNumber(int[] nums) { int left = nums[0]; for(int i =1; i< nums.length; i++)原创 2016-07-31 00:15:39 · 1561 阅读 · 1 评论 -
【leetcode76】Intersection of Two Arrays II
题目描述:给定两个数组求他们的公共部分,输出形式是数组,相同的元素累计计数例如:nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].原文描述:Given two arrays, write a function to compute their intersection.Example:Given nums1 = [1, 2, 2, 1], nums2原创 2016-07-30 22:14:30 · 1157 阅读 · 2 评论 -
【leetcode75】Intersection of Two Arrays(数组的交集)
题目描述:给定两个数组求他们的公共部分,输出形式是数组,相同的元素只是输出一次例如:nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].原文描述:Given two arrays, write a function to compute their intersection.Example:Given nums1 = [1, 2, 2, 1], nums原创 2016-07-30 21:54:17 · 1381 阅读 · 0 评论 -
【leetcode74】Sum of Two Integers(不用+,-求两数之和)
题目描述:不用+,-求两个数的和原文描述:Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.Example:Given a = 1 and b = 2, return 3.方法一:用位运算模拟加法思路1:异或又被称其为“模2加法“设置变量recipe模拟进位原创 2016-07-30 16:41:55 · 1524 阅读 · 0 评论 -
【leetcode73】经典算法-Guess Number Higher or Lower
题目描述:从1~n中,随便的拿出一个数字,你来猜测。提示提供一个guess(int num)的api,针对猜测的数字,返回三个数值。0,-1,10;猜中返回num-1:比猜测的数值小1:比猜测的数值大例如:n = 10, I pick 6.Return 6.原文描述:We are playing the Guess Game. The game is as follows:I pick a原创 2016-07-30 15:39:40 · 1609 阅读 · 0 评论 -
72【leetcode】经典算法- Lowest Common Ancestor of a Binary Search Tree(lct of bst)
题目描述:一个二叉搜索树,给定两个节点a,b,求最小的公共祖先 _______6______ / \ ___2__ ___8__ / \ / \ 0 _4 7 9 / \ 3 5例如:2,8 —->6原创 2016-07-30 11:09:56 · 1348 阅读 · 1 评论 -
【leetcode】经典算法题-Counting Bits
题目描述:给定一个数字n,统计0~n之间的数字二进制的1的个数,并用数组输出例子:For num = 5 you should return [0,1,1,2,1,2].要求:算法复杂复o(n)空间复杂度o(n)原文描述:Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num c原创 2016-07-30 00:37:56 · 2089 阅读 · 0 评论 -
[leetcode]经典算法题- String to Integer (atoi)
题目描述:把字符串转化为整数值原文描述:Implement atoi to convert a string to an integer.Hint:Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the pos原创 2016-07-29 21:56:21 · 3746 阅读 · 2 评论 -
谈谈面试和博客写作
为什么写博客?写作对于我而言是一个很重要的交流和总结的方式,写作的初衷是写给自己看的,希望自己在过去的学习轨迹中,不断反思,获得启发。后面更多的是为了交流,一统成长!博客内容:主要是关于安卓和安卓安全以及基础知识包括:算法和数据结构,操作系统,计算机网络,计算机基础知识。 另外我还专门谢了面试的两个专栏来介绍面试的必备知识和经典面试题。欢迎入群:公众号IT面试题汇总讨论群如果扫描不进去,加我微信(原创 2016-05-10 10:40:02 · 1828 阅读 · 0 评论 -
LeetCode(69)-Reverse String
题目:Write a function that takes a string as input and returns the string reversed.Example:Given s = "hello", return "olleh".思路:题意:反转字符串不用考虑为空的情况,这种情况sb.toString()也是null,倒叙遍历,StringBuffer相加代码:public原创 2016-04-23 16:46:05 · 4040 阅读 · 0 评论 -
LeetCode(68)-Compare Version Numbers
题目:Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0.You may assume that the version strings are non-empty and con原创 2016-04-23 12:19:58 · 2561 阅读 · 0 评论 -
LeetCode(67)-Rotate Array
题目:Rotate an array of n elements to the right by k steps.For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].Note:Try to come up as many solutions as you can, th原创 2016-04-21 12:24:34 · 1271 阅读 · 0 评论 -
leetCode(66)-Excel Sheet Column Title
题目:Given a positive integer, return its corresponding column title as appear in an Excel sheet.For example: 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB 思路:题意:求对应的数字和字原创 2016-04-19 18:13:27 · 1949 阅读 · 1 评论 -
LeetCode(65)-Power of Four
题目:Given an integer (signed 32 bits), write a function to check whether it is a power of 4.Example:Given num = 16, return true. Given num = 5, return false.Follow up: Could you solve it without loops/原创 2016-04-19 12:50:29 · 1500 阅读 · 0 评论 -
LeetCode(64)- Min Stack
题目:Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get原创 2016-04-18 23:05:18 · 2585 阅读 · 0 评论 -
LeetCode(63)-First Bad Version
题目:You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on t原创 2016-04-17 22:58:55 · 3030 阅读 · 0 评论 -
LeetCode(62)-Two Sum
题目:Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution.Example:Given nums = [2, 7, 1原创 2016-04-15 16:58:49 · 4395 阅读 · 0 评论 -
leetCode(62)-Reverse Integer
题目: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原创 2016-04-15 13:39:54 · 3719 阅读 · 0 评论 -
LeetCode(61)-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" is not a pali原创 2016-04-15 11:19:06 · 3202 阅读 · 0 评论 -
LeetCode(60)-ZigZag Conversion
题目: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原创 2016-04-14 23:04:15 · 3641 阅读 · 0 评论 -
Leetcode(59)-Count Primes
题目:Description:Count the number of prime numbers less than a non-negative number, n.思路:题意:求小于给定非负数n的质数个数西元前250年,希腊数学家厄拉多塞(Eeatosthese)想到了一个非常美妙的质数筛法,减少了逐一检查每个数的的步骤,可以比较简单的从一大堆数字之中,筛选出质数来,这方法被称作厄拉多塞筛法原创 2016-04-14 18:51:48 · 3670 阅读 · 0 评论 -
leetcode(58)-Range Sum Query - Immutable
题目:Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.Example:Given nums = [-2, 0, 3, -5, 2, -1]sumRange(0, 2) -> 1sumRange(2, 5) -> -1sumRange(0, 5原创 2016-04-14 16:24:25 · 1027 阅读 · 0 评论 -
leetcode(57)- Implement strStr()
题目:Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.思路:题意:判断一个字符串是否包含另外一个写一个函数判断一个字符串从坐标k,是否相等于另一个,遍历一下,i < a.length() -b.le原创 2016-04-14 15:42:01 · 1109 阅读 · 0 评论 -
LeetCode(56)-Add Binary
题目:Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".思路:题意:对字符串的二进制数字,计算把二进制转化为整数,设置变量carry位进位,sum%2,是相应的数字,carry = sum/2 -代码:public class原创 2016-04-14 12:28:06 · 1311 阅读 · 0 评论 -
LeetCode(55)- Palindrome Linked List
题目:Given a singly linked list, determine if it is a palindrome.Follow up:思路:题意:判断一个链表是不是回文利用两个指针,一个快fast,一个慢slow,slow = head,fast = head,每次fast跳两格,slow一格,直到fast.next.next == null结束,slow位于中间,把slow.nex原创 2016-04-14 10:42:04 · 914 阅读 · 0 评论 -
LeetCode(54)-Longest Common Prefix
题目:Write a function to find the longest common prefix string amongst an array of strings.思路:题意:找出字符串的最大的公共前缀思路是写一个比较的函数,遍历 - 代码:public class Solution { public String longestCommonPrefix(String[原创 2016-04-13 23:10:28 · 3043 阅读 · 0 评论 -
LeetCode(53)-Binary Tree Paths
题目:Given a binary tree, return all root-to-leaf paths.For example, given the following binary tree: 1 / \2 3 \ 5All root-to-leaf paths are:["1->2->5", "1->3"]思路:题意:求二叉树根到叶子的所有路径二叉树的问题,原创 2016-04-13 22:42:33 · 3485 阅读 · 0 评论 -
LeetCode(52)-Remove Linked List Elements
题目:Remove all elements from a linked list of integers that have value val.ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5思路:题意:有序列表里面去掉给定的元素遍历发现相同的,就去掉原创 2016-04-13 21:13:30 · 3709 阅读 · 0 评论 -
LeetCode(51)- Count and Say
题目:The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off as "one 2, t原创 2016-04-12 16:57:49 · 2617 阅读 · 0 评论 -
LeetCode(50)-Word Pattern
题目:Given a pattern and a string str, find if str follows the same pattern.Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.Examples原创 2016-04-12 15:14:15 · 959 阅读 · 0 评论 -
LeetCode(49)-Valid Parentheses
题目:Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.The brackets must close in the correct order, "()" and "()[]{}" are all valid bu原创 2016-04-12 12:16:53 · 1185 阅读 · 0 评论 -
LeetCode(48)-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: A word is defined原创 2016-04-12 10:37:20 · 937 阅读 · 0 评论 -
LeetCode(47)-Reverse Bits
题目:Reverse bits of a given 32 bits unsigned integer.For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011原创 2016-04-12 10:08:51 · 1194 阅读 · 0 评论 -
LeetCode(46)-Remove Nth Node From End of List
题目:Given a linked list, remove the nth node from the end of list and return its head.For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linke原创 2016-04-11 22:55:57 · 3244 阅读 · 0 评论