all


[leetcode] Restore IP Addresses

Restore IP Addresses Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example: Given “25525511135”, return [“255.255.11.135”, “255.255.111.35”]. (Order does not matter) tag: backtracking, string 花了我好久才搞通这题! 其实大方向是对的,带剪枝的回溯算法。先一个个插dot进去,发现不行后再返回上一层,逐个测试。 有三个点需要注意: 1. 传参数时,result是要随时被更改的,所以传参时要传它的引用。即使我是调用它的push_back()函数,也需要传引用,这个好奇怪。 2. substr的参数是(index, count)表示取[index, index + cout)的子字符串。开始我以为是[start, end)。结果改了好久,在本地上cout才测试出来。 3. stoi不能转换过长的字符串,否则会报RUN TIME ERROR 错误。所以我在stoi函数前,加了s.size() […]


[转载]Linux——VIM 中文显示乱码解决

有时候在使用vim的时候,显示中文为乱码,这个时候我们可以修改vimrc文件解决问题!   首先,你需要搞清楚vimrc所在的位置。一般来说,在linux系统里,应该是这样   Linux: /usr/share/vim/vimrc在Windows系统,应该是在vim的安装目录   Windows: c:\program files\vim\vimrc我目前使用的是Windows7 ,路径显示为   C:\Program Files\Vim\_vimrc   这里所说的都是全局设定,打开vimrc文件后,只需要在文件最后添加以下代码就可以了:   set fileencodings=utf-8,gb2312,gbk,gb18030   set termencoding=utf-8   set fileformats=unix   set encoding=prc   这样,你的vim中文乱码问题就解决了!   需要注意的是,在Windows7和vista下,由于加强版的管理员权限,你用vim直接打开vimrc文件,所做的修改是无法保存的!哪怕你使用的 是:wq! 命令!一个简单的方法就是先在开始里面用管理员权限启动vim,然后通过vim打开vimrc文件做修改就可以了! 原文链接: http://www.2cto.com/os/201111/110622.html


[leetcode] Maximal Rectangle

Maximal Rectangle Given a 2D binary matrix filled with 0’s and 1’s, find the largest rectangle containing all ones and return its area. tags: array, stack, hash table, dynamic programming 很烦leetcode的一点就是,大部分题都不给时间复杂度和空间复杂度的要求。还得自己一点点来试。 这也是我见到的tag最多的一题,难度也为hard。 先写了暴力的O(nm)^2的算法,超时。 版本一:暴力搜索,O((nm)^2)。超时 class Solution { public: /** * assume the size of 2D array is n*m. * this […]


[leetcode] Edit Distance

Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3 operations permitted on a word: a) Insert a character b) Delete a character c) Replace a character tag: […]


[leetcode] Pow(x, n)

Pow(x, n) Implement pow(x, n). 数据结构书上的原题,也是一个经典题.直接上手写了,但第一次没过.因为没考虑到n为负数的情况.第二次AC. 时间复杂度O(logn) C++: class Solution { public: double pow(double x, int n){ return n<0?1/_pow(x,-n):_pow(x,n); } double _pow(double x, int n) { if (n == 0){ return 1; } else if(n == 1){ return x; } else{ double y = pow(x, n / 2); if(n % […]


[leetcode] Combination Sum

Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (including target) will be positive integers. Elements […]


把操作系统(Linux)运行在内存上

0. 写在前面 笔者把Linux装在了USB3.0的U盘上。但运行速度还是很慢。于是想更改Linux的Boot Loader,让它在每次启动时,把整个操作系统加载到内存。这样Linux的运行速度将有极大的提升。()此外,为了保存系统更改,作者把U盘上的一个分区挂载到/home位置。这样用户的数据(音乐、文档、应用程序等)就会在关机后得到保存。但是,系统数据(安装的Package,设置等)在重启后还是会被还原,文末给出了一个解决办法。 本文章翻译自:Making Ubuntu Fast using RAM (Updated and Simplified)和 Making Ubuntu Insanely Fast using RAM 。笔者适当地修改了原文的流程,并增添了一些终端命令的解释,使之更适合大陆用户和Linux初学者。 谨向原文作者terminator14和GNU开源社区的贡献者们致敬。 1. 适用人群 觉得Linux运行较慢,电脑内存较大(最少2G,推荐4G或更多),爱折腾的同学。 2. 总体流程 a. 将系统文件拷贝到临时文件夹; b. 将临时文件夹中的文件制作成内存可载入的映像; c. 更改Boot Loader,新增启动项,并将其指向我们制作的映像; d. 重启,享受飞一般的感觉(insanely fast)。 3. 操作步骤 3.1 原料 一份Linux内核的操作系统。文章以Ubuntu 14.04为例,其他发行版本与之类似。 注:Linux是一些操作系统的内核(Kernel)。Linux是开源的(免费且公开),很多发行商将Linux打包,并添加了自己开发的系统程序和图形界面(GUI),于是便有了各种不同的发行版本(Distribution)。常见的发行版本有Ubuntu,Red Hat,Debian,CentOS等,但他们的内核都是Linux。 举个例子:Android的内核是开源的,很多手机厂商(如三星,HTC)把Android的内核做修改优化,添加了自己的系统程序和图形界面封装成ROM,或继续开发硬件,制作成手机发售。但他们本质上都是Android机。 这里,Android内核就相当于Linux,而三星,HTC发行的ROM就相当于Ubuntu,Red Hat。 3.2检查磁盘容量 在Ubuntu命令行中输入: sudo du -hcs […]