- 博客(13)
- 资源 (7)
- 收藏
- 关注
原创 leetcode 第15题,三个数的和
这题目和两个数的和求法一样,多了一层遍历,所以复杂度为O(n^2)重要的地方是防止重复vector的push,所以要检测是否重复#include #include #include using namespace std;vector> threeSum(vector& nums) { vector> ret; if(nums.empty()) return
2015-07-25 20:37:44 525
原创 leetcode 第14题 Longest Common Prefix
这题很简单,只需要做一个简单的遍历即可#include #include #include using namespace std;string longestCommonPrefix(vector& strs) { if(strs.empty()) return ""; string comstr=strs[0]; for(int i=1;i!=strs.
2015-07-25 19:11:18 458
原创 leetcode第11题
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Fin
2015-07-24 15:13:05 1119
原创 leetcode第9题,判断数字是否是回文数字的两种方法
class Solution {public: bool isPalindrome1(int x) { if(x<0) return false; vector vec; while(x) { vec.push_back(x%10); x=x/10;
2015-07-23 23:48:37 513
原创 leetcode第6题—zigZag
LeetCode第六题zigZag的简单解法这题目是一道简单的找规律的题目0 10第1行两个数之间的间隔为 10,10,101 911 第2行两个数之间的间隔为8,2,8,22 812 第3行两个数之间的间隔为6,4,6,43 713 第4行两个数之间的间隔为4,6,4,64 614 第5行两个数之间的间隔为2,8,2,85 15第6行两个数
2015-07-23 19:41:59 670
原创 leetcode第五题—最长回文字符串
最长回文字符串(Longest Palindromic Substring )1.中心结点法,时间复杂度为O(n^2)回文字符串都是对称的,有两种对称方式,一是关于字符对称,比如a,aba,cabac,这种回文字符串长度都是奇数;二是关于间隔对称,比如aa,abba,cbaabc,这种回文字符串长度都是偶数,所以要分别检测这两种情况。中心结点法,就是遍历整个字符串,分别
2015-07-23 17:52:53 2120
原创 带有O(1)时间返回最小值的stack
主要是在辅助的栈中push当前最小的值,若data栈pop出的数不是当前最小的,则辅助栈不动;若data栈pop出当前最小的值,则辅助栈也pop#include #include using namespace std;templateclass minStack{public: void push(object val) { data.push_back(val);
2015-07-22 14:06:03 578
原创 二叉树转为有序列表
#include struct BinaryBode{ int val; BinaryBode *left; BinaryBode *right; BinaryBode(int v=0,BinaryBode *lt=nullptr,BinaryBode *rt=nullptr) :val(v),left(lt),right(rt) { }};//创建时传递的一定是指针引用,
2015-07-22 13:40:45 791
原创 Longest Substring Without Repeating Characters —leetcode第三题
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. Fo
2015-07-21 14:29:51 291
原创 开始刷刷Leetcode—leetcode第二题
描述You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return
2015-07-20 21:30:12 332
原创 算法导论—广度优先算法
#include #include #include #include #include using namespace std;enum{WHITE,GRAY,BLACK};struct Vertex{ int color; int d; int pre; Vertex() :pre(-1),color(WHITE),d(INT_MAX) { }};cla
2015-07-19 23:45:13 398
原创 算法导论—最长公共子序列
#include #include #include #include #include using namespace std; vector> lcsLen(const string &s1,const string &s2){ int m=s1.size(); int n=s2.size(); vector> C(m+1,vector(n+1)); for(int
2015-07-19 00:25:47 394
原创 算法导论,最优钢条切分
#include #include #include #include #include using namespace std;int memoryCutAux(vector &p,int n,vector &r){ int q; if(r[n]>=0) return r[n]; if(n==0) q=0; else { q=INT_MIN; fo
2015-07-18 15:34:33 407
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人