leetcode第一刷_Merge Sorted Array】的更多相关文章

水题,只是思想还是实用的. 当然能够新建出一个数组.比較着拷贝过去.可是没有必要啊亲.想想为什么用源数组会麻烦,由于确定了前面的数.为了后面的数字不被覆盖,要依次往后移,转念一想,先确定后面的数字.就不用操心会覆盖的问题了.像不像杨辉三角形中仅仅要求O(N)空间时的思想,好多好多样例. class Solution { public: void merge(int A[], int m, int B[], int n) { int i=m-1, j = n-1, k = m+n-1; while…
晕.竟然另一样的一道题.换成sorted array的话.找到中间位置更加方便了. TreeNode *sortTree(vector<int> &num, int start, int len){ if(len <= 0) return NULL; int middle = len/2; TreeNode *root = new TreeNode(num[start+middle]); root->left = sortTree(num, start, middle);…
看到这个题我就伤心啊,去微软面试的时候,第一个面试官让我做的题目就是实现集合的交操作,这个集合中的元素就像这里的interval一样.是一段一段的.当时写的那叫一个慘不忍睹.最后果然被拒掉了. .好好练习算法,争取正式招聘的时候拒一次微软.哈哈~ 说归说,这道题事实上还是比較简单的.先考虑什么样子的集合是能够合并的.设两段集合是[a, b]和[c, d],不失一般性的,如果a<c,那么有以下几种情况: 1. b<c,这说明两段是全然不相交的,没办法合并. 2. b>=c&&…
好,二叉搜索树粉末登场,有关他的问题有这么几个,给你一个n,如何求全部的n个节点的二叉搜索树个数?能不能把全部的这些二叉搜索树打印出来? 这道题倒不用考虑这么多,直接转即可了,我用的思想是分治,每次找到一半的位置,分离出中间节点,作为新子树的根节点,然后递归构造前半部分和后半部分. class Solution { public: TreeNode *sortedListToBST(ListNode *head) { if(head == NULL) return NULL; int len =…
Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the arr…
LeetCode:Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, other…
LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constan…
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example,Given input array A = […
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/#/description 题目描述 Given an array where elements are sorted in ascending order,…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 新建数组 日期 题目地址:https://leetcode.com/problems/merge-sorted-array/description/ 题目描述 Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Not…