![](https://img-blog.csdnimg.cn/20201014180756922.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode刷题记录
FrancisGeek的工作室
算法&设计模式
展开
-
排列序号
排列序号给出一个不含重复数字的排列,求这些数字的所有排列按字典序排序后该排列的编号。其中,编号从1开始。思路:这个问题是一个组合数学问题,如果知道 康托展开,那么好理解一点。但是绝大部分人应该都不知道康托展开。对于这道题,有高中数学基础也就够了,为了便于描述,这里给康托展开的描述,我来解释原因。 3 5 7 4 1 2 9 6 8 展开为 98884 X=2*8!+3*7!+4*6!+2*原创 2017-05-18 21:11:28 · 1953 阅读 · 0 评论 -
Arranging Coins
DescriptionYou have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins.Given n, find the total number of full staircase rows that can be forme原创 2017-02-24 21:04:02 · 141 阅读 · 0 评论 -
Repeated Substring Pattern
Description:Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. You may assume the given string consists of lowe原创 2017-03-02 21:02:36 · 149 阅读 · 0 评论 -
Number of Segments in a String
Description:Count the number of segments in a string, where a segment is defined to be a contiguous sequence of non-space characters.Please note that the string does not contain any non-printable chara原创 2017-03-02 20:49:14 · 196 阅读 · 0 评论 -
Balanced Binary Tree
Description: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 ne原创 2017-01-25 21:22:47 · 169 阅读 · 0 评论 -
Maximum Depth of Binary Tree
Description:Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.问题描述求二叉树的深度,二叉树的深度定义为从根结点到叶子结原创 2017-01-25 21:11:19 · 137 阅读 · 0 评论 -
Binary Tree Level Order Traversal
Description:Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level)问题描述:按层序遍历二叉树,将每层的元素按List的形式存储。Ex:3 / \ 9 20 / \ 15 7原创 2017-01-25 21:04:08 · 128 阅读 · 0 评论 -
Symmetric Tree
Description:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center)Ex: 1 / \ 2 2 / \ / \3 4 4 3 ----symmetric 1 / \ 2 2 \ \ 3原创 2017-01-25 20:52:14 · 191 阅读 · 0 评论 -
Same Tree
Description:Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes have the same value.问题描述:原创 2017-01-25 20:39:43 · 161 阅读 · 0 评论 -
Implement strStr()
Description:Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.问题描述在源字符串中找目标字符串,如果有,返回第一次出现的首字母索引,否则返回-1..解法一:思路:来个暴搜解法。。。Code:p原创 2017-02-28 21:00:00 · 176 阅读 · 0 评论 -
Find the Duplicate Number
Description:Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate原创 2017-02-27 21:15:27 · 160 阅读 · 0 评论 -
Search in Rotated Sorted Array
Description:Suppose an array sorted in ascending order 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 f原创 2017-02-27 20:46:49 · 160 阅读 · 0 评论 -
Two Sum II - Input array is sorted
问题描述简化了的Two Sum问题,数组全部已经递增排列解法一:思路:由于已经递增排列,那么就不需要用之前的HashMap了,直接上一首一尾两个指针,根据情况移动指针就可以了Code:public class Solution { public int[] twoSum(int[] numbers, int target) { int lo = 0, hi = numbers原创 2017-02-27 20:37:41 · 167 阅读 · 0 评论 -
Search a 2D Matrix
Description: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原创 2017-02-27 20:32:44 · 154 阅读 · 0 评论 -
Find Peak Element
Description:A peak element is an element that is greater than its neighbors.Given an input array where num[i] ≠ num[i+1], find a peak element and return its index.The array may contain multiple peaks,原创 2017-02-24 21:34:32 · 235 阅读 · 0 评论 -
Validate Binary Search Tree
Description:Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: 1.The left subtree of a node contains only nodes with keys less than the node原创 2017-02-25 21:15:13 · 256 阅读 · 0 评论 -
Search Insert Position
Description:Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in t原创 2017-02-25 21:32:57 · 198 阅读 · 0 评论 -
Sqrt(int x) &&Sqrt(double x)
DescriptionImplement int sqrt(int x).问题描述实现求整数X的平方根函数解法一思路:核心思路是二分查找,初始左边界(left)为1,初始右边界(right)为x… 比较条件为mid与x/mid做比较。。。 如果 mid < sqrt(x) 即mid < x/mid , left = mid + 1, right不动,right为最终答案。 如果mid > sq原创 2017-02-24 20:55:18 · 786 阅读 · 0 评论 -
closest-number-in-sorted-array
问题描述:在一个排好序的数组 A 中找到 i 使得 A[i] 最接近 target解法一:思路:Naive二分查找,如果找到target就返回。出循环后,找到最近的那个边界。 ###Code: public class Solution { /** * @param A an integer array sorted in ascending order * @par原创 2017-05-09 20:51:30 · 243 阅读 · 0 评论 -
Divide Two Integers
Description:Divide two integers without using multiplication, division and mod operator.If it is overflow, return MAX_INT.问题描述实现整数除法思路:这是一道考察基本功的好题目,包括int型数值范围,处理数值计算的思路,以及细心。代码明天给出原创 2017-05-08 19:56:58 · 150 阅读 · 0 评论 -
排列&组合问题总结
全排列(不带重复元素)Ex:给出一个列表[1,2,3],其全排列为: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ]Code:解法一:class Solution { /** * @param nums: A list of integers. * @return:原创 2017-05-16 20:24:01 · 262 阅读 · 0 评论 -
Squirrel Simulation
Description:There’s a tree, a squirrel, and several nuts. Positions are represented by the cells in a 2D grid. Your goal is to find the minimal distance for the squirrel to collect all the nuts and put原创 2017-05-07 15:42:03 · 430 阅读 · 0 评论 -
下一个排列
问题描述:给定一个若干整数的排列,给出按正数大小进行字典序从小到大排序后的下一个排列。如果没有下一个排列,则输出字典序最小的序列。Ex:左边是原始排列,右边是对应的下一个排列。 1,2,3 → 1,3,23,2,1 → 1,2,31,1,5 → 1,5,1思路分析:刚开始看这个题目没有看懂,在网上搜集一番资料后,懂得了题目想要做的事情。 给出数字序列的全排列,按照字典序排好,找到对应的下一个排列原创 2017-05-14 15:15:37 · 245 阅读 · 0 评论 -
书籍复印
问题描述:给出一个数组A包含n个元素,表示n本书以及各自的页数。现在有个k个人复印书籍,每个人只能复印连续一段编号的书,比如A[1],A[2]由第一个人复印,但是不能A[1],A[3]由第一个人复印,求最少需要的时间复印所有书。Ex:A = [3,2,4],k = 2返回5,第一个人复印前两本书心路历程这是一道hard级别的题目,以前我认为hard题目太难,就没有管,非常喜欢做easy级别的难度题。原创 2017-05-11 19:59:02 · 759 阅读 · 0 评论 -
Two Sum
Description:Given an array of integers, return indices of the two numbers such that they add up to a specific target.问题描述:给定一个整数数组,返回两个数字的索引,使得它们相加得到一个特定的目标值。Ex:Given nums = [2, 7, 11, 15], target = 9,原创 2016-12-01 20:35:17 · 195 阅读 · 0 评论 -
Remove Duplicates from Sorted Array
Description: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 p原创 2016-12-06 23:03:19 · 158 阅读 · 0 评论 -
链表反转总结
Description:Reverse a linked list from position m to n. Do it in-place and in one-pass.Ex:Given 1->2->3->4->5, m = 2 and n = 4 return 1->4->3->2->5问题描述:在给定的区间反转链表1 ≤ m ≤ n ≤ length of list.解法一:思路:这里需要原创 2016-12-05 19:08:59 · 166 阅读 · 0 评论 -
反转链表总结
原地反转链表---递归和非递归解法原创 2016-12-01 22:13:48 · 221 阅读 · 0 评论 -
链表的基本处理技巧
算法题中链表的操作主要是对链表结构的操作,实现难度不大,但是由于操作比较细腻,所以比较考察基本功。虽然算法成面不多,但是还是有一些基本处理技巧的。注意事项: 时刻注意链表是否为空(和数组越界一样严重) 结构的操作,注意分析有没有影响前后结点,该怎样处理,这正是基本功所在。技巧一: 设置dummy Node… dummy Node 在几乎90%的链表题中会用到,当链表的 head 有可能变化(原创 2017-04-08 17:20:42 · 769 阅读 · 0 评论 -
Search a 2D Matrix
Description: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原创 2017-03-08 21:30:45 · 140 阅读 · 0 评论 -
Search a 2D Matrix II
Description: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 in ascending from left to right. Integer原创 2017-03-08 21:24:37 · 151 阅读 · 0 评论 -
Search for a Range
Description:Given an array of integers sorted in ascending order, 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 t原创 2017-02-25 22:10:59 · 240 阅读 · 0 评论 -
Remove Duplicates from Sorted List
Description:Given a sorted linked list, delete all duplicates such that each element appear only once.问题描述:给一条已经排好序的链表,删除其中的重复值元素,使每个元素仅出现一次。输出给定链表的头结点,返回处理好链表的头结点。解法一:思路:先进行边界条件检查,处理空链表和单结点链表的情况。后面对链表原创 2016-12-02 22:46:51 · 135 阅读 · 0 评论 -
Merge Sorted Array
Description:Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: You may assume that nums1 has enough space (size that is greater or equal to m + n) to h原创 2016-12-07 22:44:59 · 125 阅读 · 0 评论 -
Coin Change
Description:You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amoun原创 2017-01-08 21:42:49 · 168 阅读 · 0 评论 -
Remove Duplicates from Sorted List II
Description:Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.问题描述:给一条已经排好序的链表,删除所有重复结点,只保留在链表中出现过一次的结点。Ex:Given 1->2->3->3-原创 2016-12-05 19:30:22 · 142 阅读 · 0 评论 -
Partition List
Description:Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes i原创 2016-12-05 19:20:03 · 147 阅读 · 0 评论 -
Linked List Cycle II
Description:Given a linked list, return the node where the cycle begins. If there is no cycle, return null.问题描述:给定一条链表,判断是否有环,函数返回环开始的第一个结点(也就是环的入口处)解法一:思路:这个问题算是之前判断链表是否有环问题的一个拓展。解决这个问题的思路分为两步,第一步是个追击原创 2016-12-05 18:57:15 · 155 阅读 · 0 评论 -
Insertion Sort List
Description:Sort a linked list using insertion sort.问题描述:用插入排序给一条链表元素排序解法一:思路:将链表元素一个一个的切下来,利用两个指针,一个排好序链表的表头指针(sortedHead),一个排好序链表的表尾指针。 插入结点时分为三种情况: + 待插入元素比新表表头元素小 + 待插入元素比新表表尾大 + 待插入元素在新表表头和新表表原创 2016-12-04 21:46:55 · 142 阅读 · 0 评论 -
Reorder List
Description:Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…问题描述:给一条单链表,有n+1个结点,将第n个结点插入到第0个结点与第1个结点之间,将第n-1个结点插入到第1个结点与第2个结点之间,依次类推。Ex:Given {1,2,3,4}, reorder it原创 2016-12-04 13:03:36 · 218 阅读 · 0 评论