leetcode
文章平均质量分 81
cow__sky
互联网8年老兵,热衷于技术,喜欢钻研源码,欢迎交流
展开
-
字符串转换为整型
题目原型为: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 possible in原创 2013-12-29 12:54:50 · 725 阅读 · 0 评论 -
字符串数组的最长公共前缀
题目原型:Write a function to find the longest common prefix string amongst an array of strings.分别字符串数组中的每个字符串进行扫描对比,设置一个临时变量保存每次的比较的结果。//取得两个字符串的最长公共前缀 public String getLongestStr(String one , Stri原创 2013-12-31 10:08:00 · 919 阅读 · 0 评论 -
删除单链表的倒数第n个元素
题目原型: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 t原创 2013-12-31 10:36:56 · 837 阅读 · 0 评论 -
判断单链表是否存在环,并找出环的入口
问题:1、如何判断一个链表是不是这类链表?2、如果链表为存在环,如何找到环的入口点?先来认识下这个带环链表:对于第一个问题,我们就会要找在表中是否存在环,方法如下:设置两个指针(fast, slow),初始值都指向头,slow每次前进一步,fast每次前进二步,如果链表存在环,则fast必定先进入环,而slow后进入环,两个指针必定相遇。(肯定是在环内相原创 2013-12-26 16:01:25 · 807 阅读 · 0 评论 -
罗马数字转换为整数
题目原型Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.首先要认识一些罗马符号:I表示1,V表示5,X表示10,L表示50,C表示100,D表示500,M表示1000. 然后,从前往后扫描字符串,设置4个变量:临原创 2013-12-30 11:18:29 · 1418 阅读 · 0 评论 -
判断一个字符串是否是数值
Validate if a given string is numeric.Some examples:"0" => true" 0.1 " => true"abc" => false 不能为全字母"1 a" => false 不能含有字母(e另外算)"2e10" => true 可以含有e科学表达式表示的数"e9";//false 科学表达式前面不能没有数值,原创 2013-12-29 17:36:29 · 967 阅读 · 0 评论 -
生成n对有效的小括号序列
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:"((()))", "(()())", "(())()", "()(())", "()()()"原创 2013-12-31 15:39:43 · 1515 阅读 · 0 评论 -
二叉树先(中)(后)序遍历
Given a binary tree, return the preorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,2,3].Note: Recursive solution is trivial, could you do it iteratively?原创 2014-01-02 20:49:15 · 916 阅读 · 0 评论 -
单链表的重新排列
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For example,Given {1,2,3,4}, reorder it to {1,4,2,3}.原创 2014-01-02 20:59:38 · 1219 阅读 · 0 评论 -
求一个数的平方根
Implement int sqrt(int x).Compute and return the square root of x.原创 2014-01-03 10:15:07 · 973 阅读 · 0 评论 -
合并两个有序单链表
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.原创 2014-01-07 08:39:12 · 855 阅读 · 0 评论 -
LRU Cache
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.set(key,原创 2014-01-07 08:30:33 · 1173 阅读 · 1 评论 -
翻转部分链表
Reverse a linked list from position m to n. Do it in-place and in one-pass.For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4,return 1->4->3->2->5->NULL.Note:Given m, n satisfy the following condition:1 ≤ m ≤ n ≤ length of list.原创 2014-01-12 10:40:24 · 1123 阅读 · 0 评论 -
单链表的排序
归并排序的一种比较快的排序,尤其在链表中,在所有排序中是时间复杂度为nlog(n)级别的有三种,分别为快速排序,堆排序和归并排序,但是快速排序在单链表中没有优势(适合于双向链表),同样堆排序在建堆和调整堆得过程对于单链表也是比较麻烦,这里我们选取了归并排序。归并排序的概念和原理我就不介绍了,网上的相关资料比较多。在这里,我只想记录一个思路,归并的排序分为三步走:1 分割,2 递归,3 合并。原创 2013-12-27 11:22:17 · 912 阅读 · 0 评论 -
完成整数除法运算
Divide two integers without using multiplication, division and mod operator.原创 2014-01-13 12:26:48 · 1368 阅读 · 3 评论 -
分组反转链表
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.You may not alter the values in the nodes, only nodes i原创 2014-01-13 16:22:34 · 1128 阅读 · 0 评论 -
下一个排列(Next Permutation)
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).The replacement m原创 2014-01-15 12:18:34 · 985 阅读 · 0 评论 -
最长有效小括弧(Longest Valid Parentheses)
Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.For "(()", the longest valid parentheses substring is "()", which has length = 2.Another example is ")()())", where the原创 2014-01-16 11:44:30 · 1277 阅读 · 0 评论 -
Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.原创 2014-02-22 16:29:43 · 944 阅读 · 0 评论 -
生成格雷码(Gray Code)
The gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0.原创 2014-02-22 21:35:14 · 1195 阅读 · 0 评论 -
Search in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).You are given a target value to search. If found in the array return its index, otherwise return -1.You may assume no duplicate原创 2014-02-23 10:04:25 · 706 阅读 · 0 评论 -
Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once.原创 2014-02-22 16:22:41 · 636 阅读 · 0 评论 -
Pow(x, n)
Implement pow(x, n).原创 2014-02-24 08:45:50 · 1095 阅读 · 0 评论 -
First Missing Positive(在数组中找到第一个丢失的正整数)
Given an unsorted integer array, find the first missing positive integer.For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2.Your algorithm should run in O(n) time and uses constant space.原创 2014-02-23 16:25:48 · 1521 阅读 · 0 评论 -
Insert Interval(区间插入)
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).You may assume that the intervals were initially sorted according to their start times.Example 1:Given intervals [1,3],[6,9], insert and merge [2,5原创 2014-02-24 15:41:22 · 1562 阅读 · 0 评论 -
大整数相乘
大整数相乘。原创 2014-02-24 20:40:10 · 712 阅读 · 0 评论 -
STL系列之十 全排列(百度迅雷笔试题)
全排列在笔试面试中很热门,因为它难度适中,既可以考察递归实现,又能进一步考察非递归的实现,便于区分出考生的水平。所以在百度和迅雷的校园招聘以及程序员和软件设计师的考试中都考到了,因此本文对全排列作下总结帮助大家更好的学习和理解。对本文有任何补充之处,欢迎大家指出。首先来看看题目是如何要求的(百度迅雷校招笔试题)。用C++写一个函数, 如 Foo(const char *str),转载 2014-02-25 16:05:29 · 500 阅读 · 0 评论 -
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, then one 1" or 1211.Given an integer n, generate the nt原创 2014-02-25 19:32:11 · 772 阅读 · 0 评论 -
最大子数组
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray [4,−1,2,1] has the largest sum = 6.原创 2014-02-26 09:18:27 · 788 阅读 · 0 评论 -
Search for a Range
Given a sorted array of integers, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the order of O(log n).If the target is not found in the array, return [-1, -1].For example,Given [5, 7, 7,原创 2014-02-26 09:29:38 · 824 阅读 · 0 评论 -
Rotate Image
You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?原创 2014-02-26 09:24:08 · 766 阅读 · 0 评论 -
字符串按词典分割
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.For example, givens = "leetcode",dict = ["leet", "code"].Return true because "leetcode" can be segmented原创 2014-01-05 11:14:06 · 1702 阅读 · 0 评论 -
完成strStr() 函数
题目原型:Implement strStr().Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.原创 2014-02-27 19:57:29 · 760 阅读 · 0 评论 -
全排列问题
The set [1,2,3,…,n] contains a total of n! unique permutations.原创 2014-02-25 16:24:23 · 772 阅读 · 0 评论 -
全排列的java实现(含重复数字)
题目原型:Given a collection of numbers that might contain duplicates, return all possible unique permutations.For example,[1,1,2] have the following unique permutations:[1,1,2], [1,2,1], and原创 2014-02-28 10:42:36 · 5440 阅读 · 0 评论 -
java集合之迭代器ListIterator
java集合之迭代器ListIterator首先,我们来看一段程序: public static void main(String[] args) { ArrayList list = new ArrayList(); list.add(1); list.add(2); list.add(3); list.add(4); for(ListIterator iter = list.listIterator();i原创 2014-02-28 19:57:24 · 900 阅读 · 0 评论 -
全排列的java实现(无重复元素)
题目原型:Given a collection of numbers, return all possible permutations.For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].基本原创 2014-02-28 10:38:01 · 2916 阅读 · 0 评论 -
爬楼梯问题
You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?原创 2014-03-04 08:29:48 · 844 阅读 · 0 评论 -
判断平衡二叉树
Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.原创 2014-03-04 08:16:16 · 846 阅读 · 0 评论 -
Jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Your goal is to reach the last index in the minimum number of jumps.F原创 2014-03-03 11:15:06 · 790 阅读 · 0 评论