随笔分类 -  LeetCode

摘要:bool isPalindrome(int x) { if (x < 0)return false; // int d = 1; while (x / d >= 10)d *= 10; while (x>0) { int p = x / d;//取首位 int q = x % 10;//取末位 if 阅读全文
posted @ 2016-08-02 14:33 牧马人夏峥 阅读(96) 评论(0) 推荐(0)
摘要:int reverse(int x) { int r = 0; for (; x; x /= 10) { r = r * 10 + x % 10; } } 阅读全文
posted @ 2016-08-02 14:32 牧马人夏峥 阅读(84) 评论(0) 推荐(0)
摘要:深感自己智商不足啊,看了好久才看明白。 void dfs(int n, int k, int start, int cur, vector<vector<int>> &result, vector<int> &path) { if (cur == k) { result.push_back(path 阅读全文
posted @ 2016-07-16 19:15 牧马人夏峥 阅读(124) 评论(0) 推荐(0)
摘要:这题不好想,可参考:http://www.cnblogs.com/felixfang/p/3775712.html,思路很清楚。同理这种解法也满足有重复元素的情况。 vector<vector<int>> subsets( vector<int> &S) { vector<vector<int>> 阅读全文
posted @ 2016-07-16 15:49 牧马人夏峥 阅读(138) 评论(0) 推荐(0)
摘要:bool searchMatrix(const vector<vector<int>> &matrix, int target) { int m = matrix.size();//行 int n = matrix.front.size(); int first = 0; int last = m* 阅读全文
posted @ 2016-07-16 14:43 牧马人夏峥 阅读(108) 评论(0) 推荐(0)
摘要:本质上是lower_bound,注意其与upper_bound之间的差别 int searchInsert(int A[], int n, int target) { //本质上实现lower_bound函数 return lower_bound(A, A + n, target) - A; } t 阅读全文
posted @ 2016-07-16 10:10 牧马人夏峥 阅读(103) 评论(0) 推荐(0)
摘要:在已经排好序的数组中查找,可用二分查找法效率更高,这里使用STL更方便。 vector<int> searchRange(int A[], int n, int target) { int l = distance(A, lower_bound(A, A + n, target)); int r = 阅读全文
posted @ 2016-07-16 08:59 牧马人夏峥 阅读(126) 评论(0) 推荐(0)
摘要:第一种思路是计数排序,不过需要两趟才能完成。第二种思路是定义两个index,每次将红色交换至最前,将蓝色交换至最后,白色保持不变,很巧妙的思路。 void sortColors(int A[], int n) { int counts[3] = { 0 }; for (int i = 0; i < 阅读全文
posted @ 2016-07-13 09:02 牧马人夏峥 阅读(130) 评论(0) 推荐(0)
摘要:不好想,用桶排序解决。 int findMissingPostive(int A[], int n) { bucket_sort(A, n); for (int i = 0; i < n; i++) if (A[i] != i + 1) return i + 1; return n + 1; } v 阅读全文
posted @ 2016-07-12 17:29 牧马人夏峥 阅读(98) 评论(0) 推荐(0)
摘要:采用归并排序,通过定义快、慢两个指针来找到中点,再采用之前的排序算法进行归并。 ListNode *listSort(ListNode *head) { //定义快慢指针,找到链表中心 ListNode *slow=head,*fast=head; while (fast->next!=nullpt 阅读全文
posted @ 2016-07-12 16:52 牧马人夏峥 阅读(127) 评论(0) 推荐(0)
摘要:对链表进行插入排序,比对数组排序麻烦一点。 ListNode *insertSortList(ListNode *head) { ListNode dummy(-1); for (ListNode *cur = head; cur != nullptr;) { //将当前结点插入到此结点之后 aut 阅读全文
posted @ 2016-07-12 16:27 牧马人夏峥 阅读(121) 评论(0) 推荐(0)
摘要:和合并数组类似。 ListNode *mergeList(ListNode *l1, ListNode *l2) { if (l1 == nullptr)return l2; if (l2 == nullptr)return l1; ListNode dummy(-1); ListNode *p = 阅读全文
posted @ 2016-07-12 15:40 牧马人夏峥 阅读(95) 评论(0) 推荐(0)
摘要:比较简单的一道题,这个方法是从后往前比较,效率更高。 void merge(int A[], int m, int B[], int n) { int ia = m - 1, ib = n - 1, icuur = m + n - 1; while (ia >= 0 && ib >= 0) { // 阅读全文
posted @ 2016-07-12 15:20 牧马人夏峥 阅读(100) 评论(0) 推荐(0)
摘要:int sumNumbers(TreeNode *root) { return dfs(root, 0); } int dfs(TreeNode *root, int sum) { if (root == nullptr)return 0; if (root->left == nullptr && 阅读全文
posted @ 2016-07-09 20:05 牧马人夏峥 阅读(121) 评论(0) 推荐(0)
摘要:这题代码简单,不过不容易想到。 void connect(TreeLinkNode *root) { if (root == nullptr ||root->left==nullptr)return; root->left->next = root->right; //关键 if (root->ne 阅读全文
posted @ 2016-07-09 16:17 牧马人夏峥 阅读(101) 评论(0) 推荐(0)
摘要:最近忙着水论文,好久没刷题了,现在真是看到论文就烦啊,来刷刷题。 返回最大值,这题需要注意的是,在递归的时候不能返回最大值,只能返回单向的值,最大值每次保留即可。 int maxPathSum(TreeNode *root) { max_sum = INT_MIN; dfs(root); retur 阅读全文
posted @ 2016-07-09 15:14 牧马人夏峥 阅读(90) 评论(0) 推荐(0)
摘要:bool hasPathSum(TreeNode *root, int sum) { if (root == nullptr)return false; if (root->left == nullptr && root->right == nullptr) return sum == root-> 阅读全文
posted @ 2016-06-21 15:13 牧马人夏峥 阅读(181) 评论(0) 推荐(0)
摘要:二叉树的最小深度 采用递归的方式求左右结点的高度,注意判断一个结点是否是叶子结点(左右子树都不存大)。 int minDepth(TreeNode *root) { return minDepth(root, false); } int minDepth(TreeNode *root, bool h 阅读全文
posted @ 2016-06-21 13:50 牧马人夏峥 阅读(79) 评论(0) 推荐(0)
摘要:和上题思路基本一致,不同的地方在于,链表不能随机访问中间元素。 int listLength(ListNode* node) { int n = 0; while (node) { n++; node = node->next; } return n; } ListNode* nth_node(Li 阅读全文
posted @ 2016-06-17 16:43 牧马人夏峥 阅读(104) 评论(0) 推荐(0)
摘要:思路很简单,用二分法,每次选中间的点作为根结点,用左、右结点递归。 TreeNode* sortedArrayToBST(vector<int> &num) { return sortedArrayToBST(num.begin(), num.end()); } template<typename 阅读全文
posted @ 2016-06-17 16:04 牧马人夏峥 阅读(117) 评论(0) 推荐(0)