![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
算法题
qufayudao
这个作者很懒,什么都没留下…
展开
-
lintcode题目-3.统计数字
统计数字描述:计算数字k在0到n中的出现的次数,k可能是0~9的一个值样例:例如n=12,k=1,在 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],我们发现1出现了5次 (1, 10, 11, 12)解题思路1:先将大问题分解成小问题,题目中需要统计0-n中出现的次数,可以判断一个数中出现了几次k,在累计即可得到共出现的次数。 问题就变成了,给定一个数原创 2018-01-02 22:44:28 · 391 阅读 · 0 评论 -
LeetCode 138. Copy List with Random Pointer
描述 A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy of the list.思路 由于有了一个随机的指针,因此这个单向链表有点像...原创 2018-03-17 22:25:12 · 99 阅读 · 0 评论 -
139. Word Break
描述 Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words. You may...原创 2018-03-18 14:38:30 · 166 阅读 · 0 评论 -
LeetCode 142. Linked List Cycle II
描述 Given a linked list, return the node where the cycle begins. If there is no cycle, return null.Note: Do not modify the linked list.Follow up: Can you solve it without using extra space?思...原创 2018-03-18 15:50:50 · 234 阅读 · 0 评论 -
LeetCode 91. Decode 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 ...原创 2018-03-05 17:36:54 · 199 阅读 · 0 评论 -
LeetCode 103. Binary Tree Zigzag Level Order Traversal
描述 Given a binary tree, return the zigzag level order traversal of its nodes’ values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given bina...原创 2018-03-12 21:35:16 · 98 阅读 · 0 评论 -
LeetCode 144. Binary Tree Preorder Traversal
描述Given a binary tree, return the preorder traversal of its nodes’ values. For example: Given binary tree [1,null,2,3], 1 \ 2 / 3return [1,2,3].Note: Recursive solution i...原创 2018-03-22 22:13:13 · 157 阅读 · 0 评论 -
LeetCode 143. 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,3,4}, reorder it t...原创 2018-03-22 15:11:25 · 129 阅读 · 0 评论 -
LeetCode 147. Insertion Sort List
描述 Sort a linked list using insertion sort.思路 首先要明白直接插入排序的概念:遍历一个数组,当找到一个数a,该数比前一个数更小,则将a之前的所有大于a的数全部向前移动1,将a插入最后一个大于该数的位置。用数组排序的代码如下代码(c#) public void InsertSort(int[] arr) { ...原创 2018-03-22 23:56:56 · 106 阅读 · 0 评论 -
LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal
描述 Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. For example, given preorder = [3,9,20,15,7] inorder ...原创 2018-03-13 16:26:00 · 102 阅读 · 0 评论 -
LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal
描述Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. For example, given inorder = [9,3,15,20,7] postorde...原创 2018-03-13 16:30:36 · 120 阅读 · 0 评论 -
LeetCode 109. Convert Sorted List to Binary Search Tree
描述 Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. For this problem, a height-balanced binary tree is defined as a binary tree in which...原创 2018-03-13 17:09:22 · 111 阅读 · 0 评论 -
LeetCode 113. Path Sum II
描述 Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / \...原创 2018-03-13 17:46:36 · 121 阅读 · 0 评论 -
LeetCode 114. Flatten Binary Tree to Linked List
描述 Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like: 1...原创 2018-03-13 18:45:33 · 93 阅读 · 0 评论 -
LeetCode 92. 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->NULL, m = 2 and n = 4,return 1->4->3->2->5->NULL....原创 2018-03-06 16:42:01 · 95 阅读 · 0 评论 -
LeetCode 148. Sort List
描述 Sort a linked list in O(n log n) time using constant space complexity.思路 排序算法有很多,对链表进行排序可以选择比较容易操作的算法,这里选择快速排序法,因为快速排序法比较容易用链表实现。快速排序法的思想:对未排序链表进行遍历,每次找出最小的元素位置,将最小的元素与未排序的第一个元素进行交换,继续遍历,知道所有的元...原创 2018-03-23 16:12:22 · 113 阅读 · 0 评论 -
LeetCode 150. Evaluate Reverse Polish Notation
描述Evaluate the value of an arithmetic expression in Reverse Polish Notation.Valid operators are +, -, *, /. Each operand may be an integer or another expression.Some examples: ["2", "1", "+...原创 2018-03-23 18:17:16 · 144 阅读 · 0 评论 -
LeetCode 137. Single Number II
描述Given an array of integers, every element appears three times except for one, which appears exactly once. Find that single one.Note: Your algorithm should have a linear runtime complexity. Co...原创 2018-03-17 21:53:24 · 145 阅读 · 0 评论 -
LeetCode 134. Gas Station
描述There are N gas stations along a circular route, where the amount of gas at station i is gas[i].You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i t...原创 2018-03-17 21:02:46 · 121 阅读 · 0 评论 -
lintcode-5丑数
描述:设计一个算法,找出只含素因子2,3,5 的第 n 小的数。符合条件的数如:1, 2, 3, 4, 5, 6, 8, 9, 10, 12…样例:如果n = 9, 返回 10思路 1.一个数只含因子2、3、5,说明这个数不断的整除2、3、5最后能得到1,一个数是丑数可以用以下的伪代码表示while(num%5==0){ num=num/5;}while(num%3==0){原创 2018-01-02 23:37:59 · 163 阅读 · 0 评论 -
lintcode-29交叉字符串
描述给出三个字符串:s1、s2、s3,判断s3是否由s1和s2交叉构成。样例比如 s1 = “aabcc” s2 = “dbbca” 当 s3 = “aadbbcbcac”,返回 true. 当 s3 = “aadbbbaccc”, 返回 false.思路如果s3是由s1、s2交叉构成,那么一定满足一下条件 1、s3的长度等于s1的长度加s2的长度。 2、s3原创 2018-01-03 22:10:36 · 342 阅读 · 0 评论 -
lintcode-31数组划分
描述给出一个整数数组 nums 和一个整数 k。划分数组(即移动数组 nums 中的元素),使得:1、所有小于k的元素移到左边 2、所有大于等于k的元素移到右边 返回数组划分的位置,即数组中第一个位置 i,满足 nums[i] 大于等于 k。样例给出数组 nums = [3,2,2,1] 和 k = 2,返回 1.思路比k小的在左边,比k大的在右边,并不需要严原创 2018-01-04 21:05:33 · 270 阅读 · 0 评论 -
93. Restore IP Addresses
描述Given a string containing only digits, restore it by returning all possible valid IP address combinations.For example: Given “25525511135”,return [“255.255.11.135”, “255.255.111.35”]. (Orde...原创 2018-03-07 15:00:22 · 93 阅读 · 0 评论 -
94. Binary Tree Inorder Traversal
描述Given a binary tree, return the inorder traversal of its nodes’ values.For example: Given binary tree [1,null,2,3], 1 \ 2 / 3return [1,3,2].Note: Recursive solution ...原创 2018-03-07 15:15:24 · 89 阅读 · 0 评论 -
LeetCode 127. Word Ladder
描述 Given two words (beginWord and endWord), and a dictionary’s word list, find the length of shortest transformation sequence from beginWord to endWord, such that:Only one letter can be changed a...原创 2018-03-15 15:38:32 · 136 阅读 · 0 评论 -
LeetCode 129. Sum Root to Leaf Numbers
描述 Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the...原创 2018-03-15 15:59:32 · 98 阅读 · 0 评论 -
LeetCode 130. Surrounded Regions
描述 Given a 2D board containing ‘X’ and ‘O’ (the letter O), capture all regions surrounded by ‘X’. A region is captured by flipping all ‘O’s into ‘X’s in that surrounded region. For example,X ...原创 2018-03-15 20:38:17 · 164 阅读 · 0 评论 -
LeetCode 95. Unique Binary Search Trees II
描述 Given an integer n, generate all structurally unique BST’s (binary search trees) that store values 1…n. For example, Given n = 3, your program should return all 5 unique BST’s shown below....原创 2018-03-09 15:20:08 · 81 阅读 · 0 评论 -
96. Unique Binary Search Trees
描述 Given n, how many structurally unique BST’s (binary search trees) that store values 1…n? For example, Given n = 3, there are a total of 5 unique BST’s. 1 3 3 2 1 ...原创 2018-03-09 15:53:22 · 83 阅读 · 0 评论 -
LeetCode 90. Subsets II
LeetCode 90. Subsets II 解题的一种思路描述 Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set). Note: The solution set must not contain duplicat...原创 2018-03-05 16:49:54 · 152 阅读 · 0 评论 -
LeetCode 78. Subsets 解题思路
描述Given a set of distinct integers, nums, return all possible subsets (the power set).Note: The solution set must not contain duplicate subsets.For example, If nums = [1,2,3], a solution is: ...原创 2018-03-05 16:47:28 · 217 阅读 · 1 评论 -
LeetCode 98. Validate Binary Search Tree
描述 Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node’s key. ...原创 2018-03-10 17:44:48 · 157 阅读 · 0 评论 -
LeetCode 102. Binary Tree Level Order Traversal
描述 Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level). For example: Given binary tree [3,9,20,null,null,15,7], 3 / \ ...原创 2018-03-10 18:17:02 · 91 阅读 · 0 评论 -
LeetCode 131. Palindrome Partitioning
描述 Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. For example, given s = “aab”, Return[ ["aa","b"...原创 2018-03-17 00:50:19 · 376 阅读 · 0 评论 -
LeetCode 133. Clone Graph
描述Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.OJ’s undirected graph serialization: Nodes are labeled uniquely.We use # as a separator for ea...原创 2018-03-17 01:49:54 · 148 阅读 · 0 评论 -
LeetCode 120. Triangle
描述 Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle[ [2], [3,4], ...原创 2018-03-14 16:23:28 · 99 阅读 · 0 评论