
数据结构与算法
文章平均质量分 75
SegmentFault_
这个作者很懒,什么都没留下…
展开
-
旋转数组的最小元素
把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。输入一个递增排序的数组的一个旋转,输出旋转数组的最小元素。 例如有如下数组 旋转之后如下 此时需要找出当前旋转数组的最小值,由于数组是递增的,所以旋转之后前面的数组元素会大于后面数组的元素,也就是数组{4, 5}大于{1,2,3}。因此采用二分查找的方法找出最小元素。 标记数组的开头left与结尾righ原创 2016-08-04 23:16:22 · 513 阅读 · 0 评论 -
整数大数乘法
整数大数据进行运算的时候并不能简单使用一个变量去存储结果,因为运算结果可能超出该变量能存储的最大字节数。因此可以使用一个SeqList或者STL中的vector对数据进行存储。 在进行乘法运算时,根据乘法的运算规则,每次对一列求和即可,求和完成要求的进位。最后每一列求和完的结果和即为乘法结果。 1. 求出列数与被乘数个数相同各列的和。 (1) 被乘数从i位置开始,乘数从j位置开始原创 2016-08-28 09:35:52 · 721 阅读 · 0 评论 -
LeetCode Rotate Function
Given an array of integers A and let n to be its length. Assume Bk to be an array obtained by rotating the array A k positions clock-wise, we define a "rotation function" F on A as follow:原创 2016-09-24 11:41:06 · 461 阅读 · 0 评论 -
LeetCode Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Pascal三角形也叫杨辉三角形。杨辉三角形按如下性质进行排列: 1 1原创 2016-09-24 19:51:05 · 440 阅读 · 0 评论 -
LeetCode Invert Binary Tree
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 反转二叉树,首先采用递归的方式。先遍历作左子树,当指针指向NULL时,返回,赋值给tmp。接着遍历右子树,指向NULL,返回,并将该返回值赋原创 2016-09-25 16:09:41 · 444 阅读 · 0 评论 -
LeetCode Construct Binary Tree from Traversal
1.Construct Binary Tree from Preorder and Inorder Traversal 根据二叉树前序遍历和中序遍历构造一颗二叉树。 现在有前序遍历4213769,中序遍历为1234679。根据前序和中序遍历的概念,构造一棵二叉树如下步骤: (1) 设前序遍历的下标为prebegin = 0,preend = preorder.size() - 1。中序遍历下原创 2016-09-26 10:53:56 · 451 阅读 · 0 评论 -
LeetCode Odd Even Linked List
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes. You should try to do it in原创 2016-10-06 23:51:08 · 511 阅读 · 0 评论 -
LeetCode Sort List(链表快速排序)
Sort a linked list in O(n log n) time using constant space complexity. 看到O(n log n)这个复杂度就想到了快速排序法。快速排序法主要思想为取一个基准点,采用交换+分治法完成快速排序。 对于单链表的快速排序,不能从后面开始遍历,因为根据后继找不到其前驱。因此采用以下步骤: 1.取每次的头节点为基准节点,然后将这个原创 2016-10-08 17:17:30 · 2038 阅读 · 1 评论