![](https://img-blog.csdnimg.cn/20201014180756925.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
数据结构与算法
Arlingtonroad
这个作者很懒,什么都没留下…
展开
-
【剑指offer】求给定数组的具有最大和的连续子数组
题目:给定一个整数数组nums,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。示例:输入: [-2,1,-3,4,-1,2,1,-5,4],输出: 6解释:连续子数组[4,-1,2,1] 的和最大,为6。class Solution {public: int FindGreatestSumOfSubArray(vector<int> &nums) { int n = nums.size(); ...原创 2020-07-13 22:05:07 · 193 阅读 · 0 评论 -
【LeetCode】二叉树翻转:Invert Binary Tree
class Solution {public: TreeNode* invertTree(TreeNode* root) { if(root==nullptr) return nullptr; TreeNode* temp = nullptr; temp = root->left; root->left = root->right; root->right = temp; .原创 2020-07-11 01:03:54 · 104 阅读 · 0 评论 -
加权轮询算法
#include<iostream>#include<vector>#include<string>using namespace std;struct Server { int weight; string name; int curWeight;};Server* smoothWrr(vector<Server*>& servers) { int n = servers.size(); int maxIndex = -.原创 2020-07-04 00:09:04 · 312 阅读 · 0 评论 -
【算法题】找出给定数组里的第k大数
基本思想: 利用快速排序,只要找到第k个数,其左边的值都小于该元素,右边的值都大于该元素即可(左半区和右半区可以无序)。代码如下:int partition(vector<int>& nums, int left, int right) { int pivotValue = nums[left]; while(left < right) { while(left < right && nums[right] >= pi...原创 2020-07-03 22:16:36 · 609 阅读 · 0 评论 -
排序算法总结(内排序)
排序算法作为算法和数据结构的重要部分,系统地学习一下是很有必要的。排序是计算机内经常进行的一种操作,其目的是将一组“无序”的记录序列调整为“有序”的记录序列。 排序分为内部排序和外部排序:若整个排序过程不需要访问外存便能完成,则称此类排序问题为内部排序; 反之,若参加排序的记录数量很大,整个序列的排序过程不可能在内存中完成,则称此类排序问题为外部排序。 常说的的八大排序算法均属于内部排序。如果按照策略来分类,大致可分为:交换排序、插入排序、选择排序、归...原创 2020-07-03 02:10:56 · 601 阅读 · 1 评论 -
利用一个产生随机数的函数生成另一个产生随机数的函数
最近做了一些Tencent及几家公司的面试题,发现有一种关于产生随机数的类型的题目。看到多有大牛们做出来,而且效率很高,也有不知道怎么做的,最近根据几个产生随机数的题目整理一下,发现所有的类似题目可以用一种万能钥匙解决。故分享,欢迎发表不同看法,欢迎吐槽。题目一:给定能随机生成整数1到5的函数,写出能随机生成整数1到7的函数。 利用随机函数rand()函数生成一个等概率随机生成整数1到5的函数Rand5(),然后根据Rand5()生成Rand7(),代码如下:#i...原创 2020-06-30 00:08:59 · 1711 阅读 · 0 评论 -
【LeetCode】给定数组,找到两个数和x轴组成的容器能盛的最大水量:Container With Most Water
Givennnon-negative integersa1,a2, ...,an, where each represents a point at coordinate (i,ai).nvertical lines are drawn such that the two endpoints of lineiis at (i,ai) and (i, 0). Find two lines, which together with x-axis forms a container, su...原创 2020-06-15 22:58:20 · 223 阅读 · 0 评论 -
【LeetCode】找字符串中最大的不包含重复字符的子串:Longest Substring Without Repeating Characters
Given a string, find the length of thelongest substringwithout repeating characters.Example 1:Input: "abcabcbb"Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2:Input: "bbbbb"Output: 1Explanation: The answer is "b..原创 2020-06-15 22:09:48 · 187 阅读 · 0 评论 -
【LeetCode】买卖多次能获取的最大利润:Best Time to Buy and Sell Stock II
Say you have an arraypricesfor which theithelement is the price of a given stock on dayi.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times)...原创 2020-06-15 00:27:59 · 177 阅读 · 0 评论 -
【LeetCode】买卖一次股票获得最大利润:Best Time to Buy and Sell Stock
Say you have an array for which theithelement is the price of a given stock on dayi.If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.Note t...原创 2020-06-14 23:51:43 · 223 阅读 · 0 评论 -
【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 reach the last index in the minimum number of jumps.E原创 2020-06-14 23:12:45 · 1738 阅读 · 0 评论 -
【LeetCode】给定数组,判定能够跳到最后一个元素:Jump Game
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 are able to reach the last index.Example 1:Inp.原创 2020-06-14 22:36:29 · 1220 阅读 · 0 评论 -
【LeetCode】求一个非负整数的平方根:Sqrt(x)
Implementint sqrt(int x).Compute and return the square root ofx, wherexis guaranteed to be a non-negative integer.Since the return typeis an integer, the decimal digits are truncated and only the integer part of the resultis returned.Example 1:...原创 2020-06-14 21:51:34 · 425 阅读 · 0 评论 -
【LeetCode】实现pow(x, n):Pow(x, n)
Implementpow(x,n), which calculatesxraised to the powern(xn).Example 1:Input: 2.00000, 10Output: 1024.00000Example 2:Input: 2.10000, 3Output: 9.26100Example 3:Input: 2.00000, -2Output: 0.25000Explanation: 2-2 = 1/22 = 1/4 = 0.25...原创 2020-06-14 21:16:01 · 177 阅读 · 0 评论 -
【LeetCode】Letter Combinations of a Phone Number
Given a string containing digits from2-9inclusive, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.Ex..转载 2020-06-14 03:02:11 · 141 阅读 · 0 评论 -
【LeetCode】求1到n的整数中,任取k个元素的所有组合:Combinations
Given two integersnandk, return all possible combinations ofknumbers out of 1 ...n.Example:Input:n = 4, k = 2Output:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]代码如下:class Solution {public: vector<vector<int>>...原创 2020-06-14 02:49:31 · 872 阅读 · 0 评论 -
【LeetCode】求数组(可能包含重复数字)的所有全排列:Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations.Example:Input: [1,1,2]Output:[ [1,1,2], [1,2,1], [2,1,1]]解法一:class Solution {public: vector<vector<int>> permuteUnique(原创 2020-06-14 02:27:43 · 317 阅读 · 0 评论 -
【LeetCode】求数组的全排列:Permutations
Given a collection ofdistinctintegers, return all possible permutations.Example:Input: [1,2,3]Output:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]]解法1解题思路:使用递归来解,用一个数组保存当前排列中的数字是否出现过,代码如下class Solution {public: v..原创 2020-06-14 01:58:24 · 307 阅读 · 0 评论 -
【LeetCode】求一个数组下一个排列:Next Permutation
Implementnext permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).The replacement m.原创 2020-06-14 01:27:08 · 175 阅读 · 0 评论 -
【LeetCode】求数组元素(可能有重复元素)的所有子集: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 duplicate subsets.Example:Input: [1,2,2]Output:[ [2], [1], [1,2,2], [2,2], [1,2], [..原创 2020-06-13 22:15:55 · 227 阅读 · 0 评论 -
【LeetCode】求数组元素的所有子集:Subsets
1. 递归解法1.1 递归解法1思路比较清晰,每个元素都有两种可能性:选或者不选,直接上代码class Solution {public: vector<vector<int>> subsets(vector<int>& nums) { vector<vector<int> > result; vector<int> subset; sort原创 2020-06-13 14:11:24 · 542 阅读 · 0 评论 -
【LeetCode】二分查找详细总结(清楚易懂)
第一类: 需查找和目标值完全相等的数 这是最简单的一类,也是我们最开始学二分查找法需要解决的问题,比如我们有数组 [2, 4, 5, 6, 9],target = 6,那么我们可以写出二分查找法的代码如下:int find(vector<int>& nums, int target) { int left = 0, right = nums.size(); while (left < right) { int mid = le...转载 2020-06-13 03:12:23 · 364 阅读 · 0 评论 -
【LeetCode】求给定值在排好序的二维数组中是否存在:Search a 2D Matrix
Write an efficient algorithm that searches for a value in anmxnmatrix. This matrix has the following properties:Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previous row.E...原创 2020-06-13 02:59:46 · 151 阅读 · 0 评论 -
【LeetCode】在有序数组中找到给定元素或给定元素的插入位置: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 may assume no duplicates in the array.Example 1:Input: [1,3,5,6], 5Output: 2Example 2:原创 2020-06-13 02:00:57 · 226 阅读 · 0 评论 -
【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 array, return .For example, Given and target value 8,原创 2020-06-13 01:34:44 · 163 阅读 · 0 评论 -
【LeetCode】对数组中的不同颜色进行排序,使得相同颜色相邻: Sort Colors
Given an array withnobjects colored red, white or blue, sort themin-placeso that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the integers 0, 1, and 2 to represent the color red, white, an...原创 2020-06-13 00:55:26 · 416 阅读 · 0 评论 -
【LeetCode】找出数组中第一个缺失的正整数:First Missing Positive
1. 解法1解题思路:如果未限制使用空间,这道题用hash map最简单.class Solution {public: int firstMissingPositive(vector<int>& nums) { unordered_map<int, bool> unMap; int n = nums.size(); int max = INT_MIN; for(int i原创 2020-06-12 02:52:27 · 704 阅读 · 0 评论 -
【LeetCode】单链表排序:Insertion Sort List 和 Sort List
1.Insertion Sort List解题思路:遍历单链表,找到前半段已经排好序的链表中比当前结点大的元素的前驱结点,如果不存在,返回的是前半段链表中的尾结点;class Solution {public: ListNode* insertionSortList(ListNode* head) { if(!head || !head->next) return head; ListNode *dummy = new List.原创 2020-06-12 02:14:27 · 109 阅读 · 0 评论 -
【LeetCode】合并k个有序链表:Merge k Sorted Lists
1. 解法一解题思路:将第一次链表依次和后边链表合并,将合并结果保存在第一个链表里边,代码如下:class Solution {public: ListNode* mergeKLists(vector<ListNode*>& lists) { int n = lists.size(); if(n == 0) return nullptr; if(n == 1) return lists[0];原创 2020-06-12 00:00:55 · 129 阅读 · 0 评论 -
【LeetCode】合并两个排序数组:Merge Sorted Array
解题思路:从后往前遍历,cur指向当前合并的索引,代码如下:class Solution {public: void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { int cur = m + n - 1; int i = m - 1, j = n - 1; while(i >= 0 &&原创 2020-06-11 23:10:03 · 130 阅读 · 0 评论 -
【LeetCode】求二叉树所有路径组成的整数和:Sum Root to Leaf Numbers
Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number.An example is the root-to-leaf path1->2->3which represents the number123.Find the total sum of all root-to-leaf numbers.Note:A leaf is a no...原创 2020-06-11 01:48:15 · 195 阅读 · 0 评论 -
【LeetCode】给完全二叉树每个节点建立指向其右兄弟结点的指针:Populating Next Right Pointers in Each Node
class Solution {public: Node* connect(Node* root) { if(!root) return nullptr; Node *p = root->next; if(p) { p = p->left; //让p指向root结点右子树的右兄弟结点 } if(root->right) root->r.原创 2020-06-11 01:30:33 · 141 阅读 · 0 评论 -
【LeetCode】求二叉树的最大路径和(结点的起始位置可以是二叉树的任意两个结点): Binary Tree Maximum Path Sum
Given anon-emptybinary tree, find the maximum path sum.For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must containat least one nodeand does not...原创 2020-06-11 01:14:01 · 119 阅读 · 0 评论 -
【LeetCode】求二叉树中所有路径和等于给定值的路径: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.Note:A leaf is a node with no children.Example:Given the below binary tree andsum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ ..原创 2020-06-11 00:30:15 · 217 阅读 · 0 评论 -
【LeetCode】求二叉树是否存在某条路径,路径上的结点值相加等于给定值:Path Sum
1. 递归解法class Solution {public: bool hasPathSum(TreeNode* root, int sum) { if(!root) return false; if(!root->left && !root->right && root->val == sum) return true; return hasPathS原创 2020-06-11 00:10:52 · 353 阅读 · 0 评论 -
【LeetCode】求树的最大深度:Maximum Depth of Binary Tree
1. 递归解法class Solution {public: int maxDepth(TreeNode* root) { if(!root) return 0; int leftLen = 1 + maxDepth(root->left); int rightLen = 1 + maxDepth(root->right); return max(leftLen, rightLen); }};2. 迭代原创 2020-06-10 23:33:02 · 174 阅读 · 0 评论 -
【LeetCode】求树的最小深度:Minimum Depth of Binary Tree
1. 递归解法class Solution {public: int minDepth(TreeNode* root) { if(!root) return 0; if(!root->left) return 1 + minDepth(root->right); if(!root->right) return 1 + minDepth(root->left); return 1 + mi原创 2020-06-10 23:24:53 · 169 阅读 · 0 评论 -
【LeetCode】将有序链表转化为二叉查找树:Convert Sorted List to Binary Search Tree
class Solution {public: TreeNode* sortedListToBST(ListNode* head) { if(!head) return nullptr; if(!head->next) return new TreeNode(head->val); ListNode *slow = head, *fast = head, *last = slow; while(fast.原创 2020-06-10 01:54:49 · 115 阅读 · 0 评论 -
【LeetCode】将有序数组转化为二叉查找树:Convert Sorted Array to Binary Search Tree
class Solution {public: TreeNode* sortedArrayToBST(vector<int>& nums) { return sortedArrayToBST(nums, 0, nums.size() - 1); } TreeNode* sortedArrayToBST(vector<int>& nums, int begin, int end) { if(begin.原创 2020-06-10 01:32:09 · 75 阅读 · 0 评论 -
【LeetCode】验证给定的树是否是二叉查找树:Validate Binary Search Tree
该题的解法有很多种:使用递归来解,递归遍历每个节点,看结点的左子树的所有结点是不是比该结点小,右子树的所有节点是不是比该结点大; 使用中序遍历得到树的遍历结果,检查遍历结果是不是从小到大的顺序排列; 保存遍历过程中的前驱结点,检查当前结点值是不是比前驱结点大;1. 递归解法class Solution {public: bool isValidBST(TreeNode* root) { if(!root || (!root->left && !原创 2020-06-10 01:20:30 · 87 阅读 · 0 评论