2013年9月22日

Remove Duplicates from Sorted List

摘要: 基本的链表操作,只需一次遍历即可 1 ListNode *deleteDuplicates(ListNode *head) { 2 if(!head) 3 return NULL; 4 ListNode *cur, *next; 5 cur = head; 6 while(cur){ 7 next = cur->next; 8 while(next){ 9 if(next->val != cur->val)10 ... 阅读全文

posted @ 2013-09-22 19:45 waruzhi 阅读(221) 评论(0) 推荐(1) 编辑

Remove Element

摘要: Given an array and a value, remove all instances of that value in place and return the new length.The order of elements can be changed. It doesn't mat... 阅读全文

posted @ 2013-09-22 19:29 waruzhi 阅读(156) 评论(0) 推荐(1) 编辑

Binary Tree Inorder Traversal

摘要: 二叉树的中序遍历,需要注意的是需要在每次运行之前初始化结果vector vector aVector; void dfs(TreeNode *root){ if(root->left != NULL) dfs(root->left); aVector.push_back(root->val); if(root->right != NULL) dfs(root->right); } vector inorderTraversal(TreeNode *root) { // S... 阅读全文

posted @ 2013-09-22 19:16 waruzhi 阅读(115) 评论(0) 推荐(1) 编辑

2013年9月21日

Symmetric Tree

摘要: 判断一棵二叉树是否是镜面对称的,可以有递归和循环两种方法以下是递归的方法,增加一个函数判断两棵树是对称的 bool isSymmetric2(TreeNode *tree1, TreeNode *tree2){ if(tree1 == NULL && tree2 == NULL) return true; if(tree1 == NULL || tree2 == NULL) return false; if(tree1->val != tree2->val) return fals... 阅读全文

posted @ 2013-09-21 22:31 waruzhi 阅读(166) 评论(0) 推荐(0) 编辑

Roman to Integer

摘要: 首先要知道罗马数字的表示法,百度百科的解释是: 基本字符: I、V、X、L、C、D、M 相应的阿拉伯数字表示为: 1、5、10、50、100、500、1000相同的数字连写,所表示的数等于这些数字相加得到的数,如:Ⅲ = 3;小的数字在大的数字的右边,所表示的数等于这些数字相加得到的数, 如:Ⅷ = 8;Ⅻ = 12;小的数字,(限于Ⅰ、X 和C)在大的数字的左边,所表示的数等于大数减小数得到的数,如:Ⅳ= 4;Ⅸ= 9;正常使用时,连写的数字重复不得超过三次。(表盘上的四点钟“IIII”例外)在一个数的上面画一条横线,表示这个数扩大1000倍。需要一个map获取每个基本字符到数字的映... 阅读全文

posted @ 2013-09-21 22:07 waruzhi 阅读(196) 评论(0) 推荐(0) 编辑

Search Insert Position

摘要: 插入排序 int searchInsert(int A[], int n, int target) { // Start typing your C/C++ solution below // DO NOT write int main() function int i; for(i = 0; i = target) return i; else if(A[i] < target) continue; } if(i == n... 阅读全文

posted @ 2013-09-21 19:46 waruzhi 阅读(113) 评论(0) 推荐(0) 编辑

Reverse Integer

摘要: 这道题需要注意一些特殊情况:1、如果数字的最后几位是0的话,那么结果的前几位不能是02、对于有的数字,比如1000000003, 翻转过来会出现越界的情况。对于这种情况要怎样处理应该注意。抛出异常是一种解决方法,以下代码没有考虑第二种情况的处理。 int reverse(int x) { // Start typing your C/C++ solution below // DO NOT write int main() function if(x == 0) return 0; int result = 0... 阅读全文

posted @ 2013-09-21 19:42 waruzhi 阅读(142) 评论(0) 推荐(0) 编辑

2013年9月12日

Pascal's Triangle

摘要: 帕斯卡三角形,主要考察vector的用法。vector > generate(int numRows){ vector > result; vector tmp; result.clear(); tmp.clear(); int i,j; if(numRows == 0) return result; else if(numRows == 1){ tmp.push_back(1); result.push_back(tmp); return result; } else if(numR... 阅读全文

posted @ 2013-09-12 20:49 waruzhi 阅读(210) 评论(0) 推荐(0) 编辑

Merge Sorted Array

摘要: 归并排序,实现了一个需要额外m+n空间的方法,应该可以优化使用更少的空间void merge(int A[], int m, int B[], int n){ int C[100000]; int i=0,j=0; while(i < m && j < n){ if(A[i] <= B[j]){ C[i+j] = A[i]; i++; } else{ C[i+j] = B[j]; j++; } } while(i < m)... 阅读全文

posted @ 2013-09-12 20:47 waruzhi 阅读(108) 评论(0) 推荐(0) 编辑

Same Tree

摘要: 判断两个二叉树是否相等,入门级递归。bool isSameTree(TreeNode *p, TreeNode *q) { if(p == NULL && q == NULL) return true; if(p == NULL || q == NULL) return false; if(p->val != q->val) return false; return isSameTree(p->left, q->left)&&isSameTree(p->right, q->right); } 阅读全文

posted @ 2013-09-12 20:46 waruzhi 阅读(84) 评论(0) 推荐(0) 编辑

导航