模拟
wonderabd
这个作者很懒,什么都没留下…
展开
-
[LeetCode]17. 电话号码的字母组合
简单模拟题,把每一位的可能性列出来。 17. 电话号码的字母组合 class Solution { public: string ans=""; int tip; vector<string> ansx; //ansx.clear(); void solve(int now,string digits){ if (now>=...原创 2019-04-10 15:59:14 · 97 阅读 · 0 评论 -
[LeetCode]413.等差数列划分
题目描述:如果一个数列至少有三个元素,并且任意两个相邻元素之差相同,则称该数列为等差数列。 数组 A 包含 N 个数,且索引从0开始。数组 A 的一个子数组划分为数组 (P, Q),P 与 Q 是整数且满足 0<=P<Q<N 。 如果满足以下条件,则称子数组(P, Q)为等差数组: 元素 A[P], A[p + 1], …, A[Q - 1], A[Q] 是等差的。并且 P +...原创 2019-05-22 15:55:29 · 116 阅读 · 0 评论 -
[LeetCode]313. 超级丑数
时间复杂度 O(nk) 313. 超级丑数 class Solution { public: int nthSuperUglyNumber(int n, vector<int>& primes) { vector <int> ans(n+1,0); vector <int> p(primes.size(),1); ...原创 2019-05-11 12:44:45 · 109 阅读 · 0 评论 -
[leetcode]476.数字的补数
题目描述:给定一个正整数,输出它的补数。补数是对该数的二进制表示取反。 题目链接:476.数字的补数 class Solution { public: int findComplement(int num) { long long tip=1; long long ans=0; while (num>0){ ...原创 2019-05-15 16:48:29 · 88 阅读 · 0 评论 -
[LeetCode]73. 矩阵置零
题目描述:给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0。请使用原地算法。 题目链接:73. 矩阵置零 借鉴了评论区的思路: 用两个布尔变量就可以解决。 方法就是利用数组的首行和首列来记录行或列是否需要置0; 两个布尔值记录首行首列是否需要置0。 class Solution { public: void setZeroes(vector<...原创 2019-05-20 12:26:00 · 110 阅读 · 0 评论 -
[LeetCode]414. 第三大的数
题目描述:给定一个非空数组,返回此数组中第三大的数。如果不存在,则返回数组中最大的数。要求算法时间复杂度必须是O(n)。 题目链接:414. 第三大的数 class Solution { public: int thirdMax(vector<int>& nums) { long long big1=-1e18,big2=-1e18,big3=-1e...原创 2019-05-19 15:07:25 · 113 阅读 · 0 评论 -
[LeetCode]27. 移除元素
把数值等于val的元素移到数组的最后。 27. 移除元素 class Solution { public: int removeElement(vector<int>& nums, int val) { int n=nums.size(); int cnt=n-1; int temp; for (int i...原创 2019-05-09 15:51:43 · 66 阅读 · 0 评论 -
[LeetCode]283. 移动零
i 表示在原数组的下标,pos[i] 表示在新数组的下标,pos[i]<=i。 即最终数组 i 位置的数不会在处理原数组 1~i-1 位置上的数时被覆盖。 因此,for (int i=0;i<n;i++) 循环一遍处理即可。 283. 移动零 class Solution { public: void moveZeroes(vector<int>& nums...原创 2019-05-09 15:40:51 · 119 阅读 · 0 评论 -
[LeetCode]223. 矩形面积
223. 矩形面积 class Solution { public: int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { long long tot=0; tot+=(C-A)*(D-B); tot+=(G-E)*(H-F); lo...原创 2019-05-05 08:40:55 · 271 阅读 · 0 评论 -
[LeetCode]289. 生命游戏
289. 生命游戏 class Solution { public: void gameOfLife(vector<vector<int>>& board) { int movex[8]={0,0,-1,1,-1,1,-1,1}; int movey[8]={1,-1,0,0,-1,-1,1,1}; int m...原创 2019-05-05 08:22:08 · 136 阅读 · 0 评论 -
[剑指offer]孩子们的游戏(圆圈中最后剩下的数)
原题链接 就是非常经典的约瑟夫环问题 手写链表: class Solution { public: int LastRemaining_Solution(int n, int m) { if (n==0) return -1; //初始化数组 //nex表示队伍中下一个人的下标 //pre表示队伍中上一个人的下标 ...原创 2019-05-04 15:16:46 · 84 阅读 · 0 评论 -
[LeetCode]202. 快乐数
202. 快乐数 class Solution { public: bool isHappy(int n) { int now=n; bool appear[100005]; memset(appear,false,sizeof(appear)); while (now!=1){ if (now!=n)...原创 2019-04-19 13:40:02 · 79 阅读 · 0 评论 -
[LeetCode]221.最大正方形
枚举正方形的左上顶点,每次求出最大的可能正方形边长。 边长每加一,需要新判断一行和一列是否均是“1”。 221.最大正方形 class Solution { public: int maximalSquare(vector<vector<char>>& matrix) { int m=matrix.size(); if (m=...原创 2019-04-28 11:13:27 · 98 阅读 · 0 评论 -
[LeetCode]54. 螺旋矩阵+59. 螺旋矩阵 II
按右、下、左、上的方向顺序遍历矩阵,某方向无法继续有效访问时变换方向。 54. 螺旋矩阵 class Solution { public: //按右,下,左,上的顺序访问矩阵 const int movex[4]={0,1,0,-1}; const int movey[4]={1,0,-1,0}; vector<int> spiralOrder(vect...原创 2019-04-09 16:19:55 · 146 阅读 · 0 评论 -
[LeetCode]12. 整数转罗马数字
权值从大到小地构造罗马数字,把400,900,40,90,4,9也看成普通字符。 题目链接:12. 整数转罗马数字 class Solution { public: string intToRoman(int num) { string ans=""; while (num-1000>=0){ ans+="M"; ...原创 2019-04-07 11:01:55 · 92 阅读 · 0 评论 -
[LeetCode]14. 最长公共前缀
没什么难度的模拟题 题目链接:14. 最长公共前缀 class Solution { public: string longestCommonPrefix(vector<string>& strs) { int tip=strs.size(); int now=0; string ans=""; if (t...原创 2019-04-06 21:04:33 · 98 阅读 · 0 评论 -
[LeetCode]118. 杨辉三角
题目描述:给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。 题目链接:118. 杨辉三角 class Solution { public: vector<vector<int>> generate(int numRows) { vector<vector<int>> ans; ans...原创 2019-05-23 12:27:05 · 102 阅读 · 0 评论