- 博客(49)
- 收藏
- 关注
原创 Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the lengthof last word in the string.If the last word does not exist, return 0.Note: A word is defi
2014-09-29 11:09:52 1620
原创 LeetCode——Simplify Path
Given an absolute path for a file (Unix-style), simplify it.For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c"Corner Cases:Did you consider the case w
2014-09-29 10:52:55 338
原创 Anagrams
Given an array of strings, return all groups of strings that are anagrams.Note: All inputs will be in lower-case.
2014-09-29 10:46:18 420
原创 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
2014-09-26 16:57:50 310
原创 Integer to Roman
初阶。Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.进阶。Roman to IntegerGiven a roman numeral, convert it to an integer.Inpu
2014-09-26 14:22:32 332
原创 Valid Number
Validate if a given string is numeric.Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => trueNote: It is intended for the problem statement to be ambiguous. Yo
2014-09-26 14:08:30 309
原创 Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.
2014-09-26 11:21:31 397
转载 通过金矿模型介绍动态规划
对于动态规划,每个刚接触的人都需要一段时间来理解,特别是第一次接触的时候总是想不通为什么这种方法可行,这篇文章就是为了帮助大家理解动态规划,并通过讲解基本的01背包问题来引导读者如何去思考动态规划。本文力求通俗易懂,无异性,不让读者感到迷惑,引导读者去思考,所以如果你在阅读中发现有不通顺的地方,让你产生错误理解的地方,让你难得读懂的地方,请跟贴指出,谢谢! ----第一节
2014-09-26 11:03:45 708
原创 Regular Expression Matching
Implement regular expression matching with support for '.' and '*'.'.' Matches any single character. '*' Matches zero or more of the preceding element.The matching should cover the entire input st
2014-09-26 09:11:01 457
原创 Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengthof S is 1000, and there exists one unique longest palindromic substring.思路。1)动态规划。
2014-09-26 08:47:35 406
原创 Add Binary
Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return ”100”.思路:
2014-09-24 16:51:20 512
原创 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 andask yourself what are the possible input cas
2014-09-24 15:20:16 453
原创 Implement strStr()
Implement strStr().Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.思路:暴力破解。也有比较高级的算法,如KMP
2014-09-24 14:46:54 1074
原创 Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoringcases.For example,”A man, a plan, a canal: Panama” is a palindrome.”race a car” is not a p
2014-09-24 14:44:44 392
转载 KMP算法详解
相信很多人(包括自己)初识KMP算法的时候始终是丈二和尚摸不着头脑,要么完全不知所云,要么看不懂书上的解释,要么自己觉得好像心里了解KMP算法的意思,却说不出个究竟,所谓知其然不知其所以然是也。 经过七八个小时地仔细研究,终于感觉自己能说出其所以然了,又觉得数据结构书上写得过于简洁,不易于初学者接受,于是决定把自己的理解拿出来与大家分享,希望能抛砖引玉,这便是Bill写这篇文章想要
2014-09-24 14:05:30 436
原创 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 t
2014-09-23 10:08:16 363
原创 Search a 2D Matrix
Write an efficient algorithm that searches for a value in an mn matrix. This matrix has the followingproperties:• Integers in each row are sorted from left to right.• The first integer of each
2014-09-23 10:07:26 326
原创 Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the indexwhere it would be if it were inserted in order.You may assume no duplicates in the array.
2014-09-23 10:06:54 304
原创 各种排序
#include using namespace std;class Solution{public: //冒泡排序 //比较相邻的两个元素,如果前面大于后面,则交换,每一轮过后,最大的那个数就放到了最后一个 //优化1:如果没有发生变化,那么证明已经是排好序了,则不需要再继续下一轮 //优化2:如果有100个数的数组,仅前面10个无序,后面90个都已排好序且都大于前面10个数字。
2014-09-18 12:57:30 423
转载 白话经典算法系列之六 快速排序 快速搞定
快速排序由于排序效率在同为O(N*logN)的几种排序方法中效率较高,因此经常被采用,再加上快速排序思想----分治法也确实实用,因此很多软件公司的笔试面试,包括像腾讯,微软等知名IT公司都喜欢考这个,还有大大小的程序方面的考试如软考,考研中也常常出现快速排序的身影。总的说来,要直接默写出快速排序还是有一定难度的,因为本人就自己的理解对快速排序作了下白话解释,希望对大家理解有帮助,达到快速
2014-09-17 13:06:14 457
转载 白话经典算法系列之五 归并排序的实现
归并排序是建立在归并操作上的一种有效的排序算法。该算法是采用分治法(Divide and Conquer)的一个非常典型的应用。首先考虑下如何将将二个有序数列合并。这个非常简单,只要从比较二个数列的第一个数,谁小就先取谁,取了后就在对应数列中删除这个数。然后再进行比较,如果有数列为空,那直接将另一个数列的数据依次取出即可。
2014-09-15 17:01:21 347
原创 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-09-15 15:58:12 326
原创 Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same color areadjacent, with the colors in the order red, white and blue.Here, we will use the integers 0
2014-09-15 15:56:44 349
原创 Sort List
Sort a linked list in O(nlogn) time using constant space complexity.
2014-09-15 15:55:52 280
原创 Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing togetherthe nodes of the first two lists.
2014-09-15 15:54:50 808
原创 Merge Sorted Array
Given two sorted integer arrays A and B, merge B into A as one sorted array.Note: You may assume that A has enough space to hold additional elements from B. The number ofelements initialized in A
2014-09-15 15:52:29 406
原创 Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point toany node in the list or null.Return a deep copy of the list.
2014-09-11 20:02:08 359
原创 LRU Cache
Design and implement a data structure for Least Recently Used (LRU) cache. It should support thefollowing operations: get and set.get(key) - Get the value (will always be positive) of the key if t
2014-09-11 20:01:43 267
原创 Reverse Nodes in k-Group
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.
2014-09-11 20:01:34 1149
原创 Reorder List
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,
2014-09-11 20:01:16 474
原创 Linked List Cycle
初阶。Given a linked list, determine if it has a cycle in it.Follow up: Can you solve it without using extra space?进阶。Given a linked list, return the node where the cycle begins. If the
2014-09-11 20:00:53 422
原创 Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head.For example, Given 1->2->3->4, you should return the list as 2->1->4->3.Your algorithm should use only constant space. You ma
2014-09-11 19:58:53 473
原创 Remove Nth Node From End of List
Given a linked list, remove the nthnode 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 linked li
2014-09-11 19:58:01 1325
原创 Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative.For example: Given 1->2->3->4->5->nullptr and k = 2, return 4->5->1->2->3->nullptr.思路。1
2014-09-11 16:56:05 1475
原创 Remove Duplicates from Sorted List
初阶。Given a sorted linked list, delete all duplicates such that each element appear only once.For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3.进阶。Given a sor
2014-09-11 13:08:22 462
原创 Partition List
Given a linked list and a value x, partition it such that all nodes less than x come before nodes greaterthan or equal to x.You should preserve the original relative order of the nodes in each of
2014-09-11 13:06:18 510
原创 Reverse Linked List II
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->nullptr, m = 2 and n = 4,return 1->4->3->2->5->nullptr.Note: Given m, n satisfy the f
2014-09-09 16:51:53 599
转载 链表的逆序,排序等
#include#includetypedef struct node{ int data; node* pNext;}Node;//链表的操作,以有头节点为例,无头节点类似Node* head = NULL;//创建链表,头结点data=0,pNext=NULL;bool createNodeList(){ head = (Node*) malloc(sizeof(
2014-09-08 20:32:51 464
原创 链表的基本操作
#include using namespace std;struct Node{ int data; struct Node *Next;};//创建只有头结点的空链表struct Node * createList(){ struct Node * head = NULL; head = (struct Node*)malloc(sizeof(struct Node)
2014-09-08 20:30:06 591
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人