- 博客(386)
- 资源 (3)
- 收藏
- 关注
原创 Matching Nuts and Bolts 螺母螺帽的匹配
问题:给你一堆螺母和螺帽,每个螺母都有一个相对应的螺帽,但是他们之间的对应关系已经打乱。你可以比较螺母和螺帽的大小关系,但是你无法比较螺母和螺母的大小关系,你也无法比较螺帽和螺帽的大小关系。设计一个算法,找出螺母和螺帽的对应关系。思路:一个简单的方法就是,对每一个螺母,用线性搜索的方法找出对应的螺帽,复杂度是O(n^2)。下面给出一个类似于快速排序的算法,平均复杂度是O(n Log(n)
2015-01-15 00:03:06
4917
原创 [LeetCode] Reverse Words in a String
Given an input string, reverse the string word by word.For example, Given s = "the sky is blue", return "blue is sky the".Clarification:What constitutes a word?A sequence of non-space ch
2015-01-10 22:28:57
717
原创 [LeetCode] Substring with Concatenation of All Words
You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without an
2015-01-10 21:56:07
559
原创 [LeetCode] Merge K Sorted Linked Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.算法一:最简单的做法就是先1、2合并,12结果和3合并,123结果和4合并,…,123..k-1结果和k合并。两两合并的时候可以用递归的方法。我们计算一下复杂度。1、2合并,遍历2n个节点
2014-12-29 17:15:53
689
原创 [LeetCode] Roman to Integer
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.代码:inline int map(const char c) { switch (c) { case 'I': return 1; case 'V':
2014-12-26 18:10:33
538
原创 [LeetCode] Integer to Roman
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.string intToRoman(int num) { const int radix[] = {1000, 900, 500, 400, 100, 90,
2014-12-26 18:08:26
567
原创 [LeetCode] Mutiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative.用逐位相乘的方法,复杂度是O(m+n),m和n分别是 str1 和 str2 的长度
2014-12-26 15:19:09
608
原创 [LeetCode] 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
2014-12-26 14:40:50
556
原创 [LeetCode] Search Insert Position
相关问题1:Find a number in the array having least difference with the given number (n)Given a sorted array and a target value, return the index if the target is found. If not, return the index where ...
2014-12-26 14:02:04
582
原创 [LeetCode] 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 r
2014-12-26 10:04:53
550
原创 [LeetCode] Jump Game I
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. Determine if you
2014-12-26 09:20:53
450
原创 [LeetCode] Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution.下面的算法用广度搜索的思想,在每一个空的ce
2014-12-25 22:50:20
515
原创 [LeetCode] 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-12-24 21:09:03
893
翻译 [LeetCode] Wildcard Matching
Implement wildcard pattern matching with support for '?' and '*'.'?' Matches any single character.'*' Matches any sequence of characters (including the empty sequence).The matching should cover
2014-12-23 17:45:12
628
原创 [LeetCode] N-Queen N皇后
相关问题1:八皇后问题,解决思路和代码相关问题2:The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.Given an integer n, return all distinct solutio
2014-12-23 15:02:02
2710
原创 [LeetCode] Permutation II
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 [2,1,1].思路...
2014-12-23 10:09:08
680
原创 [LeetCode] Anagram
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case.思路:用一个map保存字符串,如果字符串第一次遇到,那么把这个字符串放入map,如果第2、3、4、...次遇到,那么把第1、2、3、4、...次的字符串放入返回的向量中
2014-12-22 17:32:13
721
原创 [LeetCode] 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. For example, given 1->2->3->3->4->4->5, return 1->2->5. Given 1->1->1->2
2014-12-22 16:14:56
698
原创 [LeetCode] Remove Duplicates From Sorted Array II
Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3].
2014-12-21 21:48:08
556
原创 [LeetCode] Remove Duplicates From Sorted Array I
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with co
2014-12-20 21:57:20
570
原创 [LeetCode] Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with the character '.'.A partially fille
2014-12-20 21:10:19
730
原创 [LeetCode] Palindrome Partitioning II
Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab", Return 1 sinc
2014-12-18 21:38:51
604
原创 Remove all nodes which don’t lie in any path with sum>= k
Given a binary tree, a complete path is defined as a path from root to a leaf. The sum of all nodes on that path is defined as the sum of that path. Given a number K, you have to remove (prune the tre
2014-12-17 14:08:59
808
翻译 打印距离叶子节点为k的节点 Print nodes that are at distance k from a leaf node
Print all nodes that are at distance k from a leaf nodeGiven a Binary Tree and a positive integer k, print all nodes that are distance k from a leaf node.Here the meaning of distance is different
2014-12-11 05:51:54
924
原创 [LeetCode] Spiral Matrix 旋转打印矩阵
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.For example, given the following matrix:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]You sh
2014-12-10 07:45:34
2085
原创 [LeetCode] k-th permutation 第k个排列
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order, We get the following sequence (ie, for n = 3):"123""132""213""231"
2014-12-10 06:38:02
1318
转载 [LeetCode] Rotate Linked 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->NULL and k = 2, return 4->5->1->2->3->NULL.Analysis:The idea is to get the whole
2014-12-10 06:15:40
569
原创 [LeetCode] Partition List 分割单链表
给你一个单链表和一个整数x,调整单链表的元素的位置,使得前半部分的值小于x,后半部分的值大于x。下面的算法的思路和单链表快速排序的思路是一样的。ListNode *partition(ListNode *head, int x) { ListNode* slow = head; ListNode* fast = head; while
2014-12-10 05:57:27
781
原创 [LeetCode] Minimum Path Sum in Matrix
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at
2014-12-09 11:07:42
553
原创 [LeetCode] Add Binary 加法
Given two binary strings, return their sum (also a binary string). For example, a = "11", b = "1" Return "100".逐位相加,记录相加的和sum,和进位carry。注意用到了一个有用的函数 to_string() 把整数变成string。string addBinary(strin
2014-12-09 10:41:56
524
转载 [LeetCode] Simplify Path
转自:http://fisherlei.blogspot.com/2013/01/leetcode-simplify-path.htmlGiven an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", => "/home"path = "/a/./b/../..
2014-12-09 07:09:30
522
原创 [LeetCode] Search a 2D Matrix
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted from left to right.The first integer of each
2014-12-09 06:09:44
482
原创 [LeetCode] Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For example, if n = 4 and k = 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]
2014-12-09 03:18:01
598
翻译 [LeetCode] Nonrecursive postorder traversal 非递归后续遍历
简单的做法是,用一个栈来保存遍历时的节点,用另外一个栈保存经过节点的次数。下面的算法不需要节点次数的栈。翻译自:http://leetcode.com/2010/10/binary-tree-post-order-traversal.htmlWe use a prev variable to keep track of the previously-traversed no
2014-12-09 01:24:42
955
原创 [LeetCode] Nonrecursive preorder traversal
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].下面给出非递归前序遍历的算法,和非递归中序遍历非常像。
2014-12-08 12:39:27
645
原创 [LeetCode] 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 it
2014-12-08 11:47:17
657
原创 [LeetCode] Remove Duplicates from Sorted Linked 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.代码很简单,用两个指针,slow和fast,slow
2014-12-08 08:21:42
919
原创 [LeetCode] 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 (size that is greater or equal to m + n) to hold additional elements from B.
2014-12-08 05:52:11
933
原创 [LeetCode] Number of decoding ways
A message containing letters from A-Z is being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine the total numb
2014-12-08 04:10:38
593
原创 [LeetCode] 翻转子链表 reverse linked list
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 follow
2014-12-08 00:50:20
620
Monte carlo模拟的Mathematica代码
2010-04-10
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人
RSS订阅