![](https://img-blog.csdnimg.cn/20201014180756738.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
数据结构和算法
涛涛酱
北邮研究僧在读
展开
-
【排序算法实现】快速排序、归并排序和堆排序
快速排序 int partition_(vector<int>& nums, int left, int right) { int key = nums[left]; int st = left, ed = right; while(st < ed) { while(nums[ed] > key) ed--; if (st < ed) swap(nums[st++], nums[ed]); whil原创 2020-07-12 21:47:25 · 285 阅读 · 0 评论 -
【LeetCode 86】Partition List
题目描述 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 in each of the two partitions. Example: Input: head = 1->4-&g原创 2020-05-17 13:35:55 · 62 阅读 · 0 评论 -
【LeetCode 92】Reverse Linked List II
题目描述 Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Example: Input: 1->2->3->4->5->NULL, m = 2, n = 4 Output: 1->4->3->2->5->NULL 思路 设了一个头结点,m==1时,方便。 代码 /** * Definition for原创 2020-05-16 23:00:59 · 66 阅读 · 0 评论 -
【LeetCode 209】 Minimum Size Subarray Sum
题目描述 Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn’t one, return 0 instead. Example: Input: s = 7, nums = [2,3,1,2,4,3] Output: 2 Explanation: the subarra原创 2020-05-13 11:35:04 · 76 阅读 · 0 评论 -
【LeetCode 239】 Sliding Window Maximum
题目描述 Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position. Return the max sliding原创 2020-05-12 20:26:55 · 84 阅读 · 0 评论 -
【LeetCode 698】Partition to K Equal Sum Subsets
题目描述 Given an array of integers nums and a positive integer k, find whether it’s possible to divide this array into k non-empty subsets whose sums are all equal. Example 1: Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4 Output: True Explanation: It's possible原创 2020-05-11 23:27:38 · 96 阅读 · 0 评论 -
【LeetCode 474】 Ones and Zeroes
题目描述 Given an array, strs, with strings consisting of only 0s and 1s. Also two integers m and n. Now your task is to find the maximum number of strings that you can form with given m 0s and n 1s. Each 0 and 1 can be used at most once. Example 1: Input: str原创 2020-05-10 22:40:42 · 86 阅读 · 0 评论 -
【LeetCode 518】 Coin Change 2
题目描述 You are given coins of different denominations and a total amount of money. Write a function to compute the number of combinations that make up that amount. You may assume that you have infinite number of each kind of coin. Example 1: Input: amount =原创 2020-05-10 21:13:08 · 106 阅读 · 0 评论 -
【面试题 & LeetCode 367】 Valid Perfect Square
题目描述 判断一个数是否是平方数。 思路 平方数可以拆成一个等差数列的和。 代码 class Solution { public: bool isPerfectSquare(int num) { int cnt = 1; while (num > 0) { num -= cnt; cnt += 2; ...原创 2020-05-06 22:05:41 · 113 阅读 · 0 评论 -
【面试题】实现一个数据结构,要求 set(index, val)、get(index)、setAll(val)三种操作时间复杂度都是O(1)
想了好久。。。 代码 class Node{ private: map<int, pair<int, int> > mp; // index, cnt, val int tot; int cnt = 0; public: void set(int index, int value) { mp[index] = {cnt, va...原创 2020-04-23 11:55:02 · 749 阅读 · 0 评论 -
【LeetCode】2Sum,3Sum,4Sum,kSum,3Sum Closest系列
题目描述 给一个数组,分别求出数组中两个数之和为target的集合。三个数之和,四个数之和,三个数之和最接近target的情况。 思路 2sum : 哈希或者双指针。 3sum: 一层循环确定一个数字,然后用2sum方法。 4sum: 两层循环+2sum。 closet 3sum: 在3sum的基础上,记录每次的diff,以及最接近的和。 ...原创 2020-04-09 11:13:08 · 143 阅读 · 1 评论 -
【排序算法】快排
递归实现: #include <iostream> #include <stdio.h> #include <vector> #include <algorithm> using namespace std; int partition_(vector<int>& v, int left, int right) { in...原创 2020-04-07 23:35:02 · 128 阅读 · 0 评论 -
快排
https://blog.csdn.net/MoreWindows/article/details/6684558 代码 //快速排序 void quick_sort(int s[], int l, int r) { if (l < r) { //Swap(s[l], s[(l + r) / 2]); //将中间的这个数和第一个数交换 参见注1 int i...原创 2020-03-28 23:32:16 · 123 阅读 · 0 评论 -
归并排序
一个比较好懂的实现。 https://blog.csdn.net/MoreWindows/article/details/6678165原创 2020-03-28 23:18:18 · 111 阅读 · 0 评论