代码改变世界

随笔档案-2014年12月

leetcode:Compare Version Numbers

2014-12-29 11:52 by MengYu1, 135 阅读, 收藏,
摘要: Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 num2?1:-1; } return 0; } 这个题有意思的在于统一化的处理 先在串尾加顿号,这样每次都可以以顿号为判定条件 其次while (i<l1 || j<l2)这一句也很有意思... 阅读全文

leetcode:Search for a Range

2014-12-28 21:20 by MengYu1, 119 阅读, 收藏,
摘要: Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order of O(log n). If the target is not found in th... 阅读全文

csapp:无符号数可能造成的程序bug

2014-12-27 20:01 by MengYu1, 181 阅读, 收藏,
摘要: 出自csapp练习2.26 size_t strlen(const char *s); int strloner(char *s,char *t) { return strlen(s)-strlen(t); } 乍一看没什么问题,但是size_t是定义为unsigned int的,那么当s串长度小于t串,计算结果是负数,对于无符号数既是一个很大的无符号数,这样返回结果为1,结果错误 改正可... 阅读全文

leetcode:atoi

2014-12-14 12:32 by MengYu1, 139 阅读, 收藏,
摘要: 转换原则:忽略前导空格,从+-或数字开始转换,中间出现非数字break,注意判断乘法加法溢出,大于INT_MAX输出INT_MAX,小于INT_MIN输出INT_MIN int atoi(const char *str) { if (str==NULL) { return 0; } int ans=0,pos=1,pre; while (*s... 阅读全文

leetcode:min stack

2014-12-14 10:59 by MengYu1, 116 阅读, 收藏,
摘要: 实现O(1)时间取得栈最小值。基本思路是新建一个minstack的栈,维护minstack的从上到下递增序,栈顶位当前stack最小值。当push时比较如果比minstack栈顶小于或等于就push进去,pop的时候如果要pop的元素与minstack栈顶相等从minstack同时pop。class... 阅读全文

leetcode:Pascal's Triangle II

2014-12-09 17:34 by MengYu1, 82 阅读, 收藏,
摘要: vector getRow(int rowIndex) { int *arr = new int[rowIndex+2]; int *arr1 = new int[rowIndex+2]; std::vector v; if (rowIndex == 0) { v.push_back(1); ... 阅读全文