代码改变世界

阅读排行榜

Rotate Image

2015-04-11 15:25 by 笨笨的老兔子, 154 阅读, 收藏,
摘要: 给定一个n*n的二维向量,顺时针旋转90度 class Solution {public: void rotate(vector > &matrix) { int length = matrix.size(); for (int i = 0; i < length/2; i++) { for (int j = i; j < le... 阅读全文

Remove Duplicates from Sorted Array

2015-04-01 09:27 by 笨笨的老兔子, 151 阅读, 收藏,
摘要: 将一个排好序的数组中重复的数字删除 思路:维护头和尾指针,头指针始终指向最后一个不重复的数字,尾指针不断向数组尾部探索找到不重复的值之后将其拷贝到头指针 class Solution {public: int removeDuplicates(int A[], int n) { if (n <= 1) { return n; }... 阅读全文

Valid Parentheses

2015-04-01 10:15 by 笨笨的老兔子, 150 阅读, 收藏,
摘要: 有一个由各种括号组成的字符串,判断其是否合法 合法准则即是否成对匹配(())合法({])不合法())(不合法思路:用栈模拟即可 class Solution {public: bool isValid(string s) { stack stk; for (size_t i = 0; i < s.size(); i++) { i... 阅读全文

Length of Last Word

2015-04-11 11:16 by 笨笨的老兔子, 149 阅读, 收藏,
摘要: 一个字符串由单词和空格组成,求最后一个单词的长度 注意点:小心最后有一串空格的样例 class Solution {public: int lengthOfLastWord(const char *s) { int length = 0; int i = strlen(s) - 1; while (i>=0 &&s[i] == ' ') ... 阅读全文

Container With Most Water

2015-03-30 14:19 by 笨笨的老兔子, 149 阅读, 收藏,
摘要: 给定一个有高度数组a,其中两个元素ai和aj构成一个水箱,水箱体积为V=min(ai,aj)∗abs(i−j),求maxV 基本思路,首先我们设定两块木板head和tail一个在头,一个在尾部,这个时候(tail−head)一定是最大的。然后开始往中间收缩,选择从短的板子开始往中间收拢,原因是水箱能够放的水的容量有最短的板决定。然后每次都比较收拢之后的容量是否大于原有容量,直到所有板子都被遍历到。... 阅读全文