算法和数据结构
似水~年华
这个作者很懒,什么都没留下…
展开
-
LeetCode 回文子串
5. 最长回文子串题目描述:解法一(暴力求解):class Solution {public: string longestPalindrome(string s) { int n=s.size(); string res; for(int i=0;i<n;i++){ for(int j=i;...原创 2019-09-06 17:15:30 · 237 阅读 · 0 评论 -
LeetCode 455. 分发饼干
题目描述:解法:class Solution {public: int findContentChildren(vector<int>& g, vector<int>& s) { sort(g.begin(),g.end()); sort(s.begin(),s.end()); int ...原创 2019-09-01 20:11:23 · 188 阅读 · 0 评论 -
LeetCode 454. 四数相加 II
题目描述:解法一(暴力法,显然超时):class Solution {public: int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) { int res=0; ...原创 2019-09-01 20:01:15 · 94 阅读 · 0 评论 -
LeetCode 453. 最小移动次数使数组元素相等
题目描述:解法一(暴力法):class Solution {public: int minMoves(vector<int>& nums) { int res=0; int mmax=0,mmin=0; while(1){ for(int i=0;i<nums.size();i...原创 2019-09-01 19:40:52 · 120 阅读 · 0 评论 -
LeetCode 452. 用最少数量的箭引爆气球
题目描述:解法一:class Solution {public: static bool comp(vector<int>& a, vector<int>& b){ return a[0]<b[0]; } int findMinArrowShots(vector<vector<int&...原创 2019-08-31 19:56:19 · 123 阅读 · 0 评论 -
LeetCode 451. 根据字符出现频率排序
题目描述:解法:class Solution {public: string frequencySort(string s) { unordered_map<char, int> ump; for (const auto& str : s) ump[str]++; multimap&...原创 2019-08-31 19:17:20 · 152 阅读 · 0 评论 -
LeetCode 464. 我能赢吗
题目描述:解法一(递归搜索)(超时):class Solution {public: bool canWin(const int& maxChoosableInteger, int desired, unordered_map<int, bool> record) { for (int i = 1; i <= maxChoosabl...原创 2019-08-31 19:02:03 · 262 阅读 · 0 评论 -
LeetCode 2. 两数相加
题目描述:解法:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ...原创 2019-08-31 16:12:18 · 76 阅读 · 0 评论 -
LeetCode 53. 最大子序和
题目描述:解法一(DP):class Solution {public: int maxSubArray(vector<int>& nums) { if(nums.size()==1) return nums[0]; int res=nums[0],temp=nums[0]; for(int i=1;i&...原创 2019-08-31 15:45:28 · 131 阅读 · 0 评论 -
LeetCode 147. 对链表进行插入排序
题目描述:解法一(头节点另处理):/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {...原创 2019-08-31 15:18:05 · 89 阅读 · 0 评论 -
LeetCode 456. 132模式
题目描述:解法一:class Solution {public: bool find132pattern(vector<int>& nums) { int n=nums.size(); if(n<3) return false; int i=0,j=0,k=0; while(i<...原创 2019-09-01 20:35:52 · 202 阅读 · 0 评论 -
LeetCode 739. 每日温度
题目描述:解法一(暴力):class Solution {public: vector<int> dailyTemperatures(vector<int>& T) { int n = T.size(); vector<int> res(n, 0); for (int i = 0; ...原创 2019-09-02 09:49:07 · 130 阅读 · 0 评论 -
LeetCode 下一个更大元素
496. 下一个更大元素 I题目描述:解法(单调栈+哈希表):class Solution {public: vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) { int n=nums2.size(); ...原创 2019-09-02 11:00:18 · 139 阅读 · 0 评论 -
LeetCode 买卖股票的最佳时机
121. 买卖股票的最佳时机题目描述:解法一:class Solution {public: int maxProfit(vector<int>& prices) { int n=prices.size(); if(n<2) return 0; int minp=prices[0]; ...原创 2019-09-05 19:32:12 · 141 阅读 · 0 评论 -
LeetCode 峰值
162. 寻找峰值题目描述:解法一(迭代):class Solution {public: int findPeakElement(vector<int>& nums) { int left=0,right=nums.size()-1; while(left<right){ int mid...原创 2019-09-04 12:59:51 · 210 阅读 · 0 评论 -
LeetCode 1. 两数之和
题目描述:解法:class Solution {public: vector<int> twoSum(vector<int>& nums, int target) { unordered_map<int,int> record; vector<int> res; for(...原创 2019-09-03 16:39:19 · 118 阅读 · 0 评论 -
LeetCode 4. 寻找两个有序数组的中位数
题目描述:解法:class Solution {public: double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { if(nums1.size()>nums2.size()) return findMedianSorted...原创 2019-09-03 16:32:12 · 104 阅读 · 0 评论 -
LeetCode 11. 盛最多水的容器
题目描述:解法:class Solution {public: int maxArea(vector<int>& height) { int n=height.size(); int right=n-1,left=0; int res=INT_MIN; while(left<right...原创 2019-09-03 10:06:38 · 146 阅读 · 0 评论 -
LeetCode 312. 戳气球
题目描述:解法一:class Solution {public: int maxCoins(vector<int>& nums) { int n=nums.size(); vector<vector<int>> dp(n+2,vector<int>(n+2,0)); //dp[i]...原创 2019-09-03 09:09:48 · 117 阅读 · 0 评论 -
LeetCode 3. 无重复字符的最长子串
题目描述:解法:class Solution {public: int lengthOfLongestSubstring(string s) { int n = s.size(); if(n<2) return n; vector<int> dp(128, -1); ...原创 2019-09-02 18:22:36 · 103 阅读 · 0 评论 -
LeetCode 983. 最低票价
题目描述:解法:class Solution {public: int mincostTickets(vector<int>& days, vector<int>& costs) { int n=days.size(); vector<int> dp(n+1,0); /...原创 2019-09-02 17:03:32 · 234 阅读 · 0 评论 -
LeetCode 978. 最长湍流子数组
题目描述:解法一(递归):class Solution {public: int coinChange(vector<int>& coins, int amount) { if(amount==0) return 0; int res=INT_MAX; for(auto coin:coins){ ...原创 2019-09-02 15:45:34 · 123 阅读 · 0 评论 -
LeetCode 143. 重排链表
题目描述:解法:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ...原创 2019-08-31 14:38:11 · 102 阅读 · 0 评论 -
LeetCode 92. 反转链表 II
题目描述:解法:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ...原创 2019-08-31 12:53:45 · 104 阅读 · 0 评论 -
LeetCode 206. 反转链表
题目描述:解法一(迭代):/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {p...原创 2019-08-31 12:03:43 · 86 阅读 · 0 评论 -
剑指offer-数学
题目一:输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的。输出描述: 对应每个测试案例,输出两个数,小的先输出。解题思路:数列满足递增,设两个头尾两个指针i和j,若ai + aj == sum,就是答案(相差越远乘积越小)若ai + aj > sum,aj肯定不是答案之一(前面已得出 i 前...原创 2019-08-14 22:36:05 · 118 阅读 · 0 评论 -
剑指offer-约瑟夫环
题目:每年六一儿童节,牛客都会准备一些小礼物去看望孤儿院的小朋友,今年亦是如此。HF作为牛客的资深元老,自然也准备了一些小游戏。其中,有个游戏是这样的:首先,让小朋友们围成一个大圈。然后,他随机指定一个数m,让编号为0的小朋友开始报数。每次喊到m-1的那个小朋友要出列唱首歌,然后可以在礼品箱中任意的挑选礼物,并且不再回到圈中,从他的下一个小朋友开始,继续0...m-1报数....这样下去......原创 2019-08-14 15:12:50 · 160 阅读 · 0 评论 -
剑指offer-进制转化
题目一:写一个函数,求两个整数之和,要求在函数体内不得使用+、-、*、/四则运算符号。思路:首先看十进制是如何做的: 5+7=12,三步走第一步:相加各位的值,不算进位,得到2。第二步:计算进位值,得到10. 如果这一步的进位值为0,那么第一步得到的值就是最终结果。第三步:重复上述两步,只是相加的值变成上述两步的得到的结果2和10,得到12。同样我们可以用三步走的方式计...原创 2019-08-14 11:28:01 · 118 阅读 · 0 评论 -
剑指offer-数组
题目一:给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1]。不能使用除法。class Solution {public: vector<int> multiply(const vector<int>& A) {...原创 2019-08-13 21:55:43 · 125 阅读 · 0 评论 -
剑指offer-字符串
题目一(正则表达式匹配):请实现一个函数用来匹配包括'.'和'*'的正则表达式。模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"ab*ac*a"匹配,但是与"aa.a"和"ab*a"均不匹配/* 解这题需要把题意仔细研究清楚,反正我试了好多次才明白的...原创 2019-08-13 20:57:30 · 107 阅读 · 0 评论 -
剑指offer-链表
题目一:在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5。/*struct ListNode { int val; struct ListNode *next; ListNode(int x)...原创 2019-08-13 16:32:46 · 90 阅读 · 0 评论 -
中缀表达式到后缀表达式的转换
# include <iostream>#define MAXsize 30#define Stksize 20int icp(char op){ switch (op) { case '#': return 0; break; case '(': return 6; break; case '*': return 4; break; case '/': ret...原创 2019-08-06 15:53:52 · 97 阅读 · 0 评论 -
大数取余算法的证明及实现
大数取余算法当一个数很大时,大到long long int类型都存放不了的时候,我们该怎么办呢?首先我们举个简单的例子来了解该算法的思想:假设有一个数443443,我们要求443%3443%3,不妨拿起笔和纸和我一起演算一下,我们看到计算过程是:第一位数字4:4%3=14%3=1第二位数字4:(1×10+4)%3=2(1×10+4)%3=2第三位数字3:(2×10+3...转载 2019-08-05 15:44:39 · 425 阅读 · 0 评论 -
栈的混洗---出栈序列数推导
对于每一个数来说,必须进栈一次、出栈一次。我们把进栈设为状态‘1’,出栈设为状态‘0’。n个数的所有状态对应n个1和n个0组成的2n位二进制数。由于等待入栈的操作数按照1‥n的顺序排列、入栈的操作数b大于等于出栈的操作数a(a≤b),因此输出序列的总数目=由左而右扫描由n个1和n个0组成的2n位二进制数,1的累计数不小于0的累计数的方案种数。在2n位二进制数中填入n个1的方案数为c(2n,n...转载 2019-07-08 20:40:42 · 1186 阅读 · 0 评论 -
剑指offer-回溯
题目一:题目描述:请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则之后不能再次进入这个格子。 例如 a b c e s f c s a d e e 这样的3 X 4 矩阵中包含一条字符串"bcced"的路径,但是矩阵中不包含"abcb...原创 2019-08-12 16:06:33 · 90 阅读 · 0 评论 -
剑指offer-数组
题目:给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值。例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,他们的最大值分别为{4,4,6,6,6,5}; 针对数组{2,3,4,2,6,2,5,1}的滑动窗口有以下6个: {[2,3,4],2,6,2,5,1}, {2,[3,4,2],6,2,5,1}, {2,3,[4,2,6],...原创 2019-08-13 11:38:46 · 89 阅读 · 0 评论 -
LeetCode 6. Z 字形变换
题目描述:解法:class Solution {public: string convert(string s, int numRows) { int n = s.size(); if (numRows >= n||numRows<2) return s; //特殊情况特殊处理 string res; ...原创 2019-08-30 12:59:59 · 88 阅读 · 0 评论 -
LeetCode 205. 同构字符串
题目描述:解法(交叉映射):class Solution {public: bool isIsomorphic(string s, string t) { unordered_map<char,char> record1; unordered_map<char,char> record2; for(in...原创 2019-08-30 22:50:51 · 97 阅读 · 0 评论 -
LeetCode 204. 计数质数
题目描述:解法一(厄拉多塞筛法):class Solution {public: int countPrimes(int n) { vector<bool> dp(n,0); int res=0; for(int i=2;i<n;i++){ if(dp[i]==0){ ...原创 2019-08-30 22:29:07 · 107 阅读 · 0 评论 -
LeetCode 203. 移除链表元素
题目描述:解法一(自然想法):/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {...原创 2019-08-30 20:24:19 · 96 阅读 · 0 评论