- 博客(31)
- 收藏
- 关注
转载 构造函数定义为private,protected(转)
原文地址:https://www.cnblogs.com/this-543273659/archive/2011/08/02/2125487.html将构造函数,析构函数声明为私有和保护的,那么对象如何创建?已经不能从外部调用构造函数了,但是对象必须被构造,应该如何解决,麻烦大家帮忙说明,关于构造,析构函数声明为私有和保护时的用法??? 提出这个问题,说明你已经对c++有所思考了...
2018-07-17 15:01:03
384
原创 c/c++知识点复习
3.当数组作为函数实参传递时,传递给函数的是数组首元素的地址。而将数组某一个元素的地址当作实参时,传递的是此元素的地址,这是可以理解为传递的是子数组(以此元素作为首元素的子数组)首元素的地址4.声明int a[4][5]数组: &a的类型为int(*)[4][5] a+i的类型为int(*)[5] *(a+i)的类型为int* ...
2018-07-05 21:05:56
461
原创 LeetCode - 152. Maximum Product Subarray - C++
第一种方法DP,并用一维vector进行存取以当前下表开始所能取得的最大连续子数组,在最后一个用例超时代码:class Solution {public: int maxProduct(vector<int>& nums) { //dp if(1==nums.size()) return nums[0]; vec...
2018-07-03 15:09:46
163
转载 LeetCode 149 Max Points on a Line
大佬的原文:https://www.cnblogs.com/grandyang/p/4579693.html这道题的OJ居然容忍brute force的方法通过,那么我感觉下面这种O(n3)的解法之所以能通过OJ,可能还有一个原因就是用了比较高效的判断三点共线的方法。一般来说判断三点共线有三种方法,斜率法,周长法,面积法(请参见这个帖子)。而其中通过判断叉积为零的面积法是坠好的。比如说有三个点A(...
2018-06-29 16:47:20
181
原创 LeetCode 120. Triangle
第一种递归解法最后用例超时,第二种DP解法改变所有输入,第三种DP解法使用k个空间不改变输入解法1:class Solution {public: int minimumTotal(vector<vector<int>>& triangle) { int ans = INT_MAX; int size = triangle.size...
2018-06-25 11:23:52
134
转载 判断平衡二叉树,只遍历一次的解法
LeetCode 110:Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as:a binary tree in which the depth of the two subtrees of every node ne...
2018-06-22 16:27:07
606
原创 递归题解
1. Leetcode 95 给定整数n,输出所有的BST二叉搜索树序列class Solution {public: vector<TreeNode*> generateTrees(int n) { vector<TreeNode*> ans; ans = getBST(1,n); return ans; } v...
2018-06-21 11:36:12
281
原创 动态规划题解
1. Leetcode 96:给定一个数字,输出从1至该数所能构成的BST(二叉搜索树)class Solution {public: int numTrees(int n) { if(n==0||n==1) return n; vector<int> ans; ans.push_back(1); an...
2018-06-21 10:57:54
227
原创 C++ 笔试题纠错
网易2018实习生笔试题: 1.printf以及cout都是从右向左编译,从左向右输出 2.初始化列表的的执行顺序是变量的声明顺序 3.欲实现出virtual函数,对象必须携带某些信息,主要用来在运行期决定哪一个virtual函数被调用,这份信息通常由所谓的一个vptr(virtual table pointer)指出,vptr指向一个由函数指...
2018-06-20 15:28:55
1045
原创 LeetCode 84,85 递增栈的典型用法
首先转载单调栈小结博客:LeetCode Monotone Stack Summary 单调栈小结LeetCode 84:static int x = [](){ std::ios::sync_with_stdio(false); cin.tie(NULL); return 0;}();class Solution {public: int largestRectangle...
2018-06-14 22:03:04
899
原创 leetcode 72 Edit distance
编辑距离的作用主要是用来比较两个字符串的相似度的基本的定义如下所示:编辑距离,又称Levenshtein距离(莱文斯坦距离也叫做Edit Distance),是指两个字串之间,由一个转成另一个所需的最少编辑操作次数,如果它们的距离越大,说明它们越是不同。许可的编辑操作包括将一个字符替换成另一个字符,插入一个字符,删除一个字符。这个概念是由俄罗斯科学家Vladimir Levenshtein在196...
2018-06-12 17:38:35
141
转载 cin.tie与sync_with_stdio加速输入输出
原文链接:http://www.hankcs.com/program/cpp/cin-tie-with-sync_with_stdio-acceleration-input-and-output.html我是怎么在不知道这一对函数的情况下活到今天的,以前碰到cin TLE的时候总是傻乎乎地改成scanf,甚至还相信过C++在IO方面效率低下的鬼话,殊不知这只是C++为了兼容C而采取的保守措施。ti...
2018-06-11 12:55:59
268
转载 Group Anagrams 群组错位词
最开始的做法因为超时无法通过最后一个样例,后受到启发map的第二个元素可以放置为vector<string>,通过。第一次代码,时间复杂度过大,不贴出来,也不加深自己的映像了AC代码:(该方法超过36%的提交)class Solution {public: vector<vector<string>> groupAnagrams(vector<str...
2018-06-08 21:37:52
214
转载 new 与 malloc 的区别 (转载)
转载:点击打开链接new与malloc的10点区别1. 申请的内存所在位置new操作符从自由存储区(free store)上为对象动态分配内存空间,而malloc函数从堆上动态分配内存。自由存储区是C++基于new操作符的一个抽象概念,凡是通过new操作符进行内存申请,该内存即为自由存储区。而堆是操作系统中的术语,是操作系统所维护的一块特殊内存,用于程序的内存动态分配,C语言使用malloc从堆上...
2018-06-06 20:35:24
125
原创 LeetCode 42 Trapping Rain Water
自己第一次尝试写的代码通过314/315个案例,最后一个案例超时,最后一个案例的高度都在1千以上,所以每次向上上移一个单位是不够的(思想写在代码的注释里)第一次代码:class Solution {public: int trap(vector<int>& height) { if(height.size()<=2) return...
2018-06-06 17:08:12
131
原创 回溯法题目
LeetCode 39 Combination Sum 给出一个不重复的数组和一个target,数组中元素可以重复使用,输出所有可能的组合Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in...
2018-06-06 14:39:56
312
转载 Count and Say
题目:The count-and-say sequence is the sequence of integers with the first five terms as following:1. 12. 113. 214. 12115. 1112211 is read off as "one 1" or 11.11 is read off as...
2018-06-06 11:36:31
153
翻译 Valid Sudoku
之前思考想要1次遍历即完成,但是想到27个set,最后采用3次遍历(通过map)完成判断,时间复杂度过高。记录他人的解法(采用set):最后贴上自己的代码(实在是又长又烂):...
2018-06-06 09:33:38
102
原创 Search for a Range 排序vector查找指定数字的起始位置终止位置
思路先确定是否存在目标数字,在目标数字的左端寻找最小位置,在目标数字的最右端寻找最大位置,通过二分查找,时间复杂度限定在O(logn)class Solution {public: vector<int> searchRange(vector<int>& nums, int target) { int len = nums.size(); ...
2018-06-06 08:14:25
408
翻译 Divide Two Integers 不用乘法,除法,取模符号进行除法运算
首先出现溢出的有两种可能性,除数为0;被除数为INT_MIN,除数为-1,先进行排除。其次通过异或判断商的符号,通过循环每次添加一倍的除数,循环次数过多,每次采取对除数乘2进行运算,(乘二操作通过左移一位完成)需要注意的是,每次做减法操作是在求取绝对值的除数和被除数上进行。代码:class Solution {public: int divide(int dividend, int divi...
2018-06-01 19:33:35
361
原创 c++ error
1. member access within null pointer of type 'struct listNode' 错误信息为:类型为'struct ListNode'的空指针内的成员访问 错误原因:发生于链表中,编译器不知道当前链表结点和结点的下一个结点是否是空指针 解决办法:判断结点是不是nullptr 或者判断下一节点是不是等于vector的...
2018-05-31 17:09:02
3297
原创 Swap Nodes in Pairs 交换链表的每两个节点
只允许使用一个申请空间,申请一个pre节点指向head节点,用指针交换,如果pre后存在两个节点就进行一次指针交换操作/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) ...
2018-05-31 11:17:11
556
原创 Generate Parentheses 产生括号 c++
采用递归的思想,剩余左括号数大于剩余右括号数,该情况一定错误。左右括号数均为0时,进行输出class Solution {public: vector<string> generateParenthesis(int n) { vector<string> ans; generateans(n,n,"",ans); return...
2018-05-31 10:26:49
184
原创 3sum
去重比较的技巧,与前一个数比较class Solution {public: vector<vector<int>> threeSum(vector<int>& nums) { int len = nums.size(); vector<vector<int>> ans; vecto...
2018-05-30 16:49:05
204
原创 机器人的运动范围
class Solution {public: int movingCount(int threshold, int rows, int cols) { if(threshold<1||rows<1||cols<1) return 0; bool* visited = new bool[rows*cols]; ...
2018-05-29 20:34:22
124
原创 矩阵中的路径
回溯法的典型应用,*(str+1)和*str+1具有不同的优先级,'\0'比0运行时间更短class Solution {public: bool hasPath(char* matrix, int rows, int cols, char* str) { if(matrix==nullptr||rows<1||cols<1||str==nullptr) ...
2018-05-29 19:16:39
89
原创 数据流中的中位数
若采用stl的sort函数对vector进行排序,时间复杂度为0(n^2),改为采用堆排序,时间复杂度为O(log(n)*n)。通过最大堆和最小堆实现,求取中位数,保证两个堆的数目之差不超过1,同时最大堆的数据小于最小堆的数据。当数据总数目是偶数时,插入最小堆(若数,小于最大堆最大数,则先插入最大堆,排序最大堆,将最大堆的最大数插入最小堆中去),反之亦然。代码:class Solution {pu...
2018-05-29 10:31:00
111
原创 二叉搜索树的第k个节点
/*struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { }};*/class Solution {public: TreeNode* KthN...
2018-05-29 09:42:26
212
原创 leetcode 5 最长回文子序列
class Solution {private: int start=0; int maxlen=0;public: string longestPalindrome(string s) { int len = s.size(); if(len<2) return s; for(int i=0;i<l...
2018-05-25 20:02:45
163
原创 UIAlertController实现退出登录
- (IBAction)logOut:(id)sender { UIAlertController* alertcontroller = [UIAlertController alertControllerWithTitle:@"提示!" message:@"是否退出登录" preferredStyle:UIAlertControllerStyleActionSheet]; UIAle...
2018-04-04 19:17:59
243
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人