数据结构与算法
文章平均质量分 50
一些基本数据结构以及算法题思路
惜暮
这个作者很懒,什么都没留下…
展开
-
[b.d]--算法题-求含有min函数的栈的实现
带有min函数的栈1. 空间复杂度是O(n)这种情况没什么好说的,利用一个同等大的辅助栈,如果入栈数据的数据比辅助栈辅助栈栈顶数据小,就更新辅助栈数据,如果大于或则等于辅助栈的栈顶,就直接吧辅助栈的栈顶复制一次再入辅助栈就行了。这种方法比较简单,没什么说的,也不给出实现了,百度一大堆。2. 空间复杂度的优化版本前面的一种解法,存在大量的重复元素被插入辅助栈,辅助栈是可以进行优化的。下面给出优化算法原创 2017-02-27 15:40:46 · 530 阅读 · 0 评论 -
[b.d]--算法题-求万级别的电话号码的排序
先给出问题的描述以及解决思路:/** * Created by louyuting on 2017/2/24. * * Question: 对某公司员工的所有电话号码进行排序, 数据量级别明显是万级别的. * * category: 大数据量进行排序 * * solution: (1)首先根据电话号码的号段进行分类, 分为130-139, 150-159, 180-189 也就是30个原创 2017-02-27 13:41:30 · 2747 阅读 · 0 评论 -
[m.t]--算法-求数组中最接近0的子序列
Question:求一个无序数组中最近接0的子序列。1. 暴力算法:求最接近于0的子数组序列,那么数组中的任何一个元素都有可能成为这个子数组的起点,那么我们可以使用双重for循环求出以每个元素为开头的最接近0的子数组。其实就是两轮for循环,具体的算法就不给出了。2. 优化算法:优原创 2017-02-17 12:20:52 · 1689 阅读 · 0 评论 -
[剑指Offer]-求序列中最小的k个数
问题描述: 输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4。解法一:利用快速排序,快速排序每轮排序 可以找出基准的位置,基准前面的序列都小于基准,右边的都大于基准。利用这个特性求解。但是这种解法会改变数组的结构。暂时先不给出这种实现的源码。解法二:适用于大数据下比如在5000千万个数据下,找出这5000千万个数据中最下的1原创 2017-02-15 00:48:25 · 756 阅读 · 1 评论 -
算法中的亦或--数组中只出现一次的数
算法中的亦或 ^1.亦或的性质交换律 a^b = b^a结合律 a^b^c=a^(b^c)=(a^b)^c0^a=a 0与任何数亦或都是这个数a^a=02.典型应用:2.1 交换两个数a=a^b; b=b^a;(b^a^b=b^b^a=a) a=a^b;((a^b)^a=b)2.2 落单的数对于一个整型数组,里面有包含一个单独的数字和其余都是出现两次的数字(或则多次),找出这个单原创 2016-12-28 00:23:43 · 1966 阅读 · 0 评论 -
[leetcode-动态规划]--96. Unique Binary Search Trees
Question 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.原创 2017-02-10 14:05:06 · 728 阅读 · 0 评论 -
[leetcode-二叉查找树的修正]--99. Recover Binary Search Tree
Question 99. Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note: A solution using O(n) sp原创 2017-02-09 15:15:53 · 548 阅读 · 0 评论 -
[leetcode-二叉树层次遍历并统计每层节点数]--103. Binary Tree Zigzag Level Order Traversal
Question 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 leve原创 2017-02-09 14:10:30 · 1879 阅读 · 0 评论 -
[leetcode-二叉树层次遍历并统计每层节点数]--107. Binary Tree Level Order Traversal II
Question 107. Binary Tree Level Order Traversal II Given a binary tree, return the bottom-up level order traversal of its nodes’ values. (ie, from left to right, level by level from leaf to root).原创 2017-02-09 13:35:09 · 1643 阅读 · 0 评论 -
[leetcode-二叉树层次遍历并统计每层节点数]--102. Binary Tree Level Order Traversal
Question 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原创 2017-02-09 13:22:07 · 2992 阅读 · 0 评论 -
[leetcode-二叉树后序遍历]--145. Binary Tree Postorder Traversal
Question 145. Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes’ values. For example: Given binary tree {1,#,2,3}, 1 \ 2原创 2017-02-09 11:05:10 · 682 阅读 · 0 评论 -
[leetcode-二叉树中序遍历]--94. Binary Tree Inorder Traversal
Question 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原创 2017-02-09 11:00:47 · 584 阅读 · 0 评论 -
[leetcode-二叉树先序遍历]--144.Binary Tree Preorder Traversal
Question 144. Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes’ values. For example: Given binary tree {1,#,2,3}, 1 \ 2原创 2017-02-09 10:53:29 · 993 阅读 · 0 评论 -
[leetcode-查找]--74. Search a 2D Matrix
Question 74. 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: (1) Integers in each row are sorted from left原创 2017-02-08 16:53:01 · 465 阅读 · 0 评论 -
[leetcode-查找]--35. Search Insert Position
Question 35. Search Insert Position 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原创 2017-02-08 16:01:29 · 433 阅读 · 0 评论 -
[leetcode-二分查找]--34. Search for a Range
Question 34. Search for a Range 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 i原创 2017-02-08 14:49:43 · 853 阅读 · 0 评论 -
[leetcode-排序]--75. Sort Colors
Question 75. Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Her原创 2017-02-08 12:21:47 · 590 阅读 · 0 评论 -
[leetcode-排序]--148. Sort List
Question: 148. Sort List Sort a linked list in O(n log n) time using constant space complexity. 中文:使用恒定的空间复杂度排序一个链表,要求时间复杂度是O(nlogn)原创 2017-02-05 22:54:20 · 1935 阅读 · 0 评论 -
[leetcode-排序]--147. Insertion Sort List
Question147. Insertion Sort List Sort a linked list using insertion sort. 中文:使用插入排序来让链表有序。解决思路:在新链表的head结点之前构建一个结点,然后将所有的结点依次插入在helper结点之后,最后返回helper.next 结点即是排序后的新链表的首结点。实现源码:/** * 核心思想是在head原创 2017-02-05 20:42:33 · 604 阅读 · 0 评论 -
[leetcode-排序]--23. Merge k Sorted Lists
Question 23. Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 中文:合并k个有序的链表,然后返回一个有序的链表的表头结点。原创 2017-02-05 16:57:40 · 517 阅读 · 0 评论 -
[leetcode-排序]--21. Merge Two Sorted Lists
Question 21. Merge Two Sorted Lists 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. 中文:合并两个有序的链表,然后返原创 2017-02-05 16:53:38 · 2730 阅读 · 1 评论 -
[leetcode-排序]--88. Merge Sorted Array
Question 88. Merge Sorted Array Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. 中文: 给定两个有序的整型数组num1和num2,将num2合并进num1,成为一个新的数组。 Note: You may原创 2017-02-05 16:03:33 · 656 阅读 · 0 评论 -
[leetcode]--413. Arithmetic Slices
Question 413. Arithmetic Slices A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same.For example原创 2017-02-05 14:51:09 · 440 阅读 · 0 评论 -
[leetcode]--338. Counting Bits
Question 338. Counting Bits Question: Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1’s in their binary representation and return them原创 2017-02-05 14:07:45 · 513 阅读 · 0 评论 -
[leetcode]--260. Single Number III
Question 260: Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. 中文:一个整形原创 2017-02-05 12:57:13 · 363 阅读 · 0 评论 -
[leetcode]--80. Remove Duplicates from Sorted Array II
Follow up for “Remove Duplicates”: What if duplicates are allowed at most twice? 中文含义:紧接着26. Remove Duplicates from Sorted Array,如果最多允许两个重复的呢? For example, Given sorted array nums = [1,原创 2017-02-05 12:29:23 · 403 阅读 · 0 评论 -
[leetcode]--26. Remove Duplicates from Sorted Array
Question 26: 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 th原创 2017-02-05 12:20:37 · 404 阅读 · 0 评论 -
[leetcode]--258. Add Digits
Question 258: Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. 中文解释: 对于一个非负的整数n, 把其每位数的数字相加求和,直到其和小于10. For example: Given num = 38,原创 2017-02-03 23:50:39 · 446 阅读 · 0 评论 -
[leetcode]--389. Find the Difference
Question 389: Given two strings s and t which consist of only lowercase letters. String t is generated by random shuffling string s and then add one more letter at a random position. Find t原创 2017-02-02 12:25:22 · 487 阅读 · 0 评论 -
[leetcode]--104. Maximum Depth of Binary Tree
Question 104: 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-02-02 12:25:02 · 285 阅读 · 0 评论 -
[leetcode]--371. Sum of Two Integers
Question 371: Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. 不用算术运算符 + 和 - 来计算出两个整数的和。Example:Given a = 1 and b = 2, return 3.解决思路:求整数和,但是题目说了,不原创 2017-02-02 09:05:34 · 462 阅读 · 0 评论 -
[leetcode]--136. Single Number
Question 136 Given an array of integers, every element appears twice except for one. Find that single one. 中文解释:有一个整型数组,数组里面除了有一个整数只出现一次,其余的整数都是出现两次,找出这个只出现一次的整数。解决思路:最开始看到这个题目我第一反应就是遍历,然后用各种标志原创 2017-02-02 08:23:04 · 430 阅读 · 0 评论 -
[leetcode]--292. Nim Game
Question 292 You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last sto原创 2017-02-02 08:08:13 · 399 阅读 · 0 评论 -
[leetcode]--刷leetcode算法目录和算法总结目录
这篇博文是记录所有刷leetcode算法的目录以及全部源码的github地址:所有代码的github地址:https://github.com/leetcode-hust/leetcode/tree/master/louyuting/src/leetcode目录:Fizz Buzz http://blog.csdn.net/u010853261/article/details/54814871原创 2017-02-02 07:54:24 · 3487 阅读 · 0 评论 -
[leetcode]--344. Reverse String
Question344: Write a function that takes a string as input and returns the string reversed. 中文解释: 其实就是字符串的原地转置。Example:Given s = "hello", return "olleh".解决思路:使用双指针法,即使用两个指针一前一后交换数据并向中间逼近,直至相原创 2017-02-02 07:50:34 · 421 阅读 · 0 评论 -
[leetcode]--412. Fizz Buzz
Question:Write a program that outputs the string representation of numbers from 1 to n.But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buz原创 2017-02-01 23:24:57 · 613 阅读 · 0 评论 -
【图结构专题】有向图
有向图一. 有向图的相关术语在有向图中,边是单向的:每条边连接的两个顶点都是一个有序对,它们的邻接性是单向的。我们开发过程中碰到的很多场景都是有向图:比如任务調度的依赖关系,社交网络的任务关系等等都是天然的有向图。以下概念都是针对有向图的:(1)==有向图==:一幅有向图是由一组顶点和一组有方向的边组成的,每条有方向的边都连接着有序的一对顶点。(2)==顶点的出度==:该顶...原创 2018-03-19 22:00:26 · 22005 阅读 · 1 评论 -
【树结构】Trie字典树
字典树概念使用场景:实现1. 数据模型2. 添加字符串3. 删除操作4. 获取单词频率5. 测试概念Trie树的名字有很多,比如字典树,前缀树等等。下面我们有and,as,at,cn,com这些关键词,那么如何构建trie树呢?从上面的图中,我们或多或少的可以发现一些好玩的特性。 第一:根节点不包含字符,除根节点外的每一个子节点都包含一个字符。 第二:从根节点到某一节点,路径上经...原创 2019-03-26 00:06:54 · 446 阅读 · 0 评论 -
【树结构】平衡二叉树
平衡二叉树平衡二叉树定义数据模型定义(go):旋转1. 左左情况(左子树的左边节点)2.右右情况(右子树的右边节点)3. 左右情况(左子树的右边节点)3. 右左情况(右子树的左边节点)新增节点删除节点平衡二叉树定义前面我们说过,二叉查找树不是严格的O(logN),导致了在真实场景中没有用武之地,谁也不愿意有O(N)的情况发生。当有很多数据灌到我的树中时,我肯定会希望最好是以“完全二叉树”的形式...原创 2019-03-25 22:47:23 · 458 阅读 · 0 评论 -
B树、B+树、LSM树以及其典型应用场景
前言动态查找树主要有:二叉查找树、平衡二叉树、红黑树、B树、B+树。前面三种是典型的二叉查找树,查找的时间复杂度是O(log2N)与树的深度有关系,那么降低树的深度也就可以提升查找效率。这时就提出了平衡多路查找树,也就是B树以及B+树。B树和B+树非常典型的场景就是用于关系型数据库的索引(MySQL)B树B树是一种平衡多路搜索树,B树与红黑树最大的不同在于,B树的结点可以有多个子女,从几个到几千个。转载 2017-10-12 17:50:52 · 24539 阅读 · 0 评论