难度:简单 题目描述: 给你一个二叉搜索树的根节点 root ,返回 树中任意两不同节点值之间的最小差值 。
/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode() : val(0), left(nullptr), right(nullptr) {}* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}* TreeNode(int x, TreeNode *left, TreeNode *right) : val(...
今天下午做了一道题。leetcode merge intervals 属于比较难的题目。 首先用collections.sort 给list排序,然后用两个while loop来比较两个interval 的start, end 。 从而生成新的interal,再插入到新的list 返回结果。 下面给出自己的代码:/*50 Merge Intervals https://leetcode.com/problems/merge-intervals/Given a collection of intervals, merge all overlapping intervals.For example,Given [1,3],[2,6],[8,10],[15,18]...
这道题目让我们求出一个线性数组中最大的连续子数组的和,题目要求如下: 这道题的题目很简单,解法也有多种,在这里总结3种解法去解决这个问题,在比较中加深对不同思路算法的理解,这三种算法是动态规划、模拟法、滑动窗口。 首先是动态规划法,正如我们之前提过的,递推形式的动态规划基本上就是三部曲: 1.确定DP数组的含义 2.递推奠基 3.基于递推方程来进行反复递推,直到最终答案 就这道题而言,DP[i]的含义是以nums[i]为结...
The set [1,2,3,…,n] contains a total of n! unique permutations.By listing and labeling all of the permutations in order,We get the following sequence (ie, for n = 3):"123""132""213""231""312""321"Given n and k, return the kth permutation sequence.Note: Given n will be between 1 and 9 inclusive.【问题描述】给定1到n这n个数字,他们的排列组合共有n!个, 把这些数字排列组合的得到的数升序排序,返回其中第...
今天刷的题是LeetCode第88题,题目要求和示例是:给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组。说明:初始化 nums1 和 nums2 的元素数量分别为 m 和 n。你可以假设 nums1 有足够的空间(空间大小大于或等于 m + n)来保存 nums2 中的元素。示例:输入:nums1 = [1,2,3,0,0,0], m = 3nums2 = [2,5,6], n = 3输出: [1,2,2,3,5,6]这个题很简单,我的代码是:class Solution {publ...
这种连续区间的加法处理,是典型的差分数组优化。
class Solution {
public:bool carPooling(vector<vector<int>>& trips, int capacity) {vector<int> delta(1010);for(auto trip: trips){delta[trip[1]] += trip[0];delta[trip[2]] -= trip[0]; }int cur = 0;for(int i = 0; i <= 1000; i++){cur += delta[i];if(cur > capacity){return false;}}return true;}
};
1.暴力法: 本题让求给定字符串的最长的无重复字符的子串,首先想到暴力解法,穷举出字符串的所有子串,并判断每个子串是否是不重复子串,具体使用hashset或set判是否有重复字符;暴力法效率很差,时间O(n^3),空间O(n);参考代码如下: 1class Solution {2public:3int lengthOfLongestSubstring(string s){4int res = 0;5constint size = s.size();6if(s.empty()) return0;7if(size<=1) return size;8for(int i = 0;i<size;++i...
LeetCode: Binary Tree Inorder TraversalGiven a binary tree, return the inorder traversal of its nodes‘ values. For example:
Given binary tree {1,#,2,3}, 12/3
return [1,3,2].Note: Recursive solution is trivial, could you do it iteratively?地址:https://oj.leetcode.com/problems/binary-tree-inorder-traversal/算法:要求用非递归来进行中序遍历。初始时,从根节点开始,一直往左走,并把每个节点入栈。在wh...
Search for a Range
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 the array, return [-1, -1].
For example,
Given [5, 7, 7, 8, 8, 10] and target value 8,return [3, 4].思路:此题在思考的时候走了些弯路,一心想着一个循环解决问题,但是写代码的时候总是不...
是之前两道题leetcode Best Time to Buy and Sell Stock和leetcode Best Time to Buy and Sell Stock II的加强版。这里要求只能买两次股票。做了一个多小时,试了好多次,终于AC了。思路:找到有可能为断点的地方,也就是出现递减的地方,递减了就说明有损失所以有可能卖出。对每个可能的分割点,计算左右两边的最大收益,求最大收益和leetcode Best Time to Buy and Sell Stock这个差不多。然后把所有可能的解记录,返回最大的解就...
Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.You may assume that each input would have exactly one solution.Input: numbers={2, 7, ...
Given a set of distinct integers, S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain duplicate subsets.
For example,
If S = [1,2,3],a solution is:[[3],[1],[2],[1,2,3],[1,3],[2,3],[1,2],[]
]基本思路:1, 先插入一个空集作为起始。2, 然后对结果集中的每个元素,复制并添加上新增字符。此代码,在leetcode上实际执行时间为16ms。class Sol...
Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps until the tree is empty. Example:
Given binary tree 1/ 2 3/ \ 4 5
Returns [4, 5, 3], [2], [1]. Explanation:
1. Remove the leaves [4, 5, 3] from the tree1/ 2
2. Remove the leaf [2] from the tree 1
3. Remove the leaf [1] from the tree [...
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).The replacement must be in-place, do not allocate extra memory.Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the ri...
You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?实现题。从最外圈顺时针交换,最多交换n/2圈就行。 1class Solution {2public:3void rotate(vector<vector<int> > &matrix) {4int n = matrix.size();5int tmp;6// 这个row其实就是圈数,最后中间可能会剩下一个点,这个点不需要旋转,无碍 7for (int row = 0; row < n / 2; ++row) {8 ...