- 博客(37)
- 收藏
- 关注
原创 【leetcode576】Out of Boundary Paths
我们用一个m*n的矩阵来记录每个格子走出去的路线数量。那么我们按照t从1到N来迭代这个m*n的矩阵。当t=1时,即只走一步,那么只有最外围一圈可以走出去,且走出去的方式与这个方格有几条边通向外界相关;当t=2时,每个格子的值可以由t=1时其上下左右四个格子的值相加得到,依次迭代下去,即当t=k时矩阵的值可以有t=k-1时的值计算得到,可以理解为先往上下左右四个方向走一步,再从这个格子走出去
2017-05-11 13:44:05
967
原创 【32】Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.For "(()", the longest valid parentheses substring is "()",
2017-01-04 18:06:01
297
原创 【c++ primer 笔记】第九章 顺序容器
1.vector, deque, list, forward_list, array, string的区别vector 可变大小数组,支持快速随机访问,在尾部之外的位置插入或删除元素可能很慢deque 双端队列,支持快速随机访问,在中间位置添加或删除元素的代价可能很高,但是在两端添加或删除元素很快list
2016-08-26 19:07:28
615
原创 【c++ primer 笔记】第八章 IO库
1.不能拷贝或对IO对象赋值进行IO操作的函数通常以引用方式传递和返回流。读写一个IO对象会改变其状态,因此传递和返回的引用不能个是const的。条件状态badbit 系统级错误,如不可恢复的读写错误,一旦badbit被置位,流就无法使用了failbit 可恢复错误,出现这种错误流还可以继续使用eofbit 流到达了文件结束,此时failbit同样会被置位goodbit
2016-08-26 19:03:11
579
原创 【c++ primer 笔记】第七章 类
1.定义在类内不的函数是隐式地inline函数this指针是一个常量指针,不允许改变this中保存的地址常成员函数string isbn() const {return this->bookNo;}此const给this指针加了一个底层const,使得this指向的内容无法改变默认情况下,我们不能把this绑定到一个常量对象上,也就是说我们不能在一个常量对象上调用
2016-08-24 20:49:09
531
原创 【31】Next Permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest possible o
2016-08-22 21:16:04
268
原创 【c++ primer 笔记】第六章 函数
1.函数的返回类型不能是数组类型或函数类型,但可以是指向数组或函数的指针返回数组的指针:1)使用类型别名typedef int arrT[10];using arrT = int[10]; //与上面的声明等价arrT* func(int i);2)直接声明int (*func(int i))[10];3)使用尾置返回类型auto func(int i
2016-08-22 19:43:38
367
原创 【30】Substring with Concatenation of All Words
You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and
2016-08-21 19:34:22
288
原创 【29】Divide Two Integers
Divide two integers without using multiplication, division and mod operator.If it is overflow, return MAX_INT.不能用乘除就只能用减法了,当然如果一个一个减的话显然太慢了,所以借住移位操作,每次减去一个被除数经过移位后能减的最大的数。int divide(int divi
2016-08-16 15:09:20
274
原创 【28】Implement strStr()
Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.直接暴力做就好了int strStr(string haystack, string needle) { int len1=
2016-08-16 15:02:24
220
原创 【27】Remove Element
Given an array and a value, remove all instances of that value in place and return the new length.Do not allocate extra space for another array, you must do this in place with constant memory.The
2016-08-16 14:36:08
315
原创 【26】Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this in place with c
2016-08-16 14:14:06
256
原创 【25】Reverse Nodes in k-Group
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
2016-08-16 13:51:03
381
原创 【24】Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head.For example,Given 1->2->3->4, you should return the list as 2->1->4->3.Your algorithm should use only constant space. You m
2016-08-15 17:39:09
523
原创 【23】Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.这里用到了优先队列,每次让权值最小的节点出队并让它的后一个节点(不为NULL)入队,直到队列为空。而如果当前只剩一条链不为空时,直接把这条链链接到最后。struct cmp{ bool o
2016-08-15 16:54:35
370
原创 【22】Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:[ "((()))", "(()())", "(())()", "()(())",
2016-08-15 16:30:22
282
原创 【21】Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.简单的链表题,加一个头节点方便编码ListNode* mergeTwoLists(ListNode* l1,
2016-08-15 15:50:45
244
原创 【20】Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.The brackets must close in the correct order, "()" and "()[]{}" are all vali
2016-08-15 15:30:45
249
原创 【19】Remove Nth Node From End of List
Given a linked list, remove the nth node from the end of list and return its head.For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the l
2016-08-14 21:42:57
252
原创 【18】4Sum
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.Note: The solution
2016-08-14 21:26:48
256
原创 【17】Letter Combinations of a Phone Number
基本的dfsvoid dfs(string &digits,string *s,vector &res,string oneRes,int cur,int n){ if(cur==n){res.push_back(oneRes);return ;} string ts=s[digits[cur]-'0']; for(int i=0;i<ts.length();i++){
2016-08-13 17:56:23
200
原创 【16】3Sum Closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exact
2016-08-12 20:45:40
211
原创 【15】3Sum
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note: The solution set must not contain d
2016-08-12 17:40:21
290
原创 【14】Longest Common Prefix
string longestCommonPrefix(vector &strs) { string common=""; int pos=0; if(strs.size()==0)return ""; if(strs.size()==1)return strs[0]; int n=strs.size(); int *len=new int[n];
2016-08-12 17:38:05
262
原创 【13】Roman to Integer
int romanToInt(string s) { int num[256]; num['I']=1;num['X']=10;num['C']=100;num['M']=1000; num['V']=5;num['L']=50;num['D']=500; int len=s.length(); int res=0; for(int i=0;i<le
2016-08-12 17:20:34
284
原创 【12】Integer to Roman
按9 5 4 1一点一点减string intToRoman(int num) { string res=""; int c=num/1000; res+=string(c,'M'); num-=(c*1000); if(num>=900){res+="CM";num-=900;} else if(num>=500){res+="D";num-=
2016-08-12 17:05:01
378
原创 【11】Container With Most Water
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
2016-08-11 20:27:47
248
原创 【10】Regular Expression Matching
Implement regular expression matching with support for '.' and '*'.'.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cover the entire input st
2016-08-11 19:46:37
304
原创 【9】Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space.首先负数不是回文数。对于正数,求一下它倒过来的数(即个位放在最高位,十位放在次高位。。。),如果结果与原数相同,则是回文数 bool isPalindrome(int x) { if(x<0)return false;
2016-08-10 21:58:06
293
原创 【8】String to Integer (atoi)
此题主要就是注意一些trick前导空格、非数字字符、溢出等int myAtoi(string str) { int res=0; int max_inf=0x7fffffff; int min_inf=0x80000000; int p=1; int i=0; while(isspace(s
2016-08-10 21:11:37
364
原创 【7】Reverse Integer
Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321唯一难点在于处理overflow,计算之前提前判断一下是否会溢出即可 int reverse(int x) { int res=0; const int
2016-08-10 20:51:23
264
原创 【6】ZigZag Conversion
找到同一行相邻两个字符间距的规律即可,以numRows=5为例1 9 ... (0,8)2 8 10 (6,2)3 7 11 (4,4)4 6 12 (2,6)5 13 (8,0)string convert(stri
2016-08-10 20:21:48
217
原创 【5】Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.此题暴力可解,时间复杂度O(N^2)
2016-08-09 17:14:24
290
原创 【4】Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).转化为求两个有序数组第k小的数问题假设原
2016-08-09 17:02:01
310
原创 【3】Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.Examples:Given "abcabcbb", the answer is "abc", which the length is 3.Given "bbbbb", the answer is "
2016-08-09 16:33:00
197
原创 【2】Add Two Numbers
链表基础题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 i
2016-08-09 16:28:42
330
原创 【1】Two Sum
为每个num加一个ID,以记录其位置,并按num排序。然后用O(n)的算法求两数之和为一指定的数typedef struct{ int num; int id;}NODE;static bool cmp(const NODE& a,const NODE& b){ return a.num<b.num;} vector twoSum(vector& nums
2016-08-09 16:21:10
281
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人