题目 Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 分析 求解一个二维平面上所有点中,位于同一直线上的最多点数. 首先想到的算法就是首先固定两个点求其斜率,然后在从剩余节点中计算该直线中的点,累计,比较...该方法时间复杂度要O(n^3),肯定不是最优解. 其实,可以略去一层循环,固定一个点,遍历剩余点,求每个斜率,借助一个map存储每个斜率上…
Max Points on a Line Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 点和方向确定一条直线. 需要两重循环,第一重循环遍历起始点a,第二重循环遍历剩余点b. a和b如果不重合,就可以确定一条直线. 对于每个点a,构建 斜率->点数 的map. (1)b与a重合,以a起始的所有直线点数+1 (用dup统一相加) (2)b与a不重…
Max Points on a Line 题目描述: Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 解题思路: 1.首先由这么一个O(n^3)的方法,也就是算出每条线的方程(n^2),然后判断有多少点在每条线上(N).这个方法肯定是可行的,只是复杂度太高2.然后想到一个O(N)的,对每一个点,分别计算这个点和其他所有点构成的斜率,具有相同斜率最…
新概念英语(1-49)At the butcher's What does Mr. Bird like? A:Do you want any meat today, Mrs. Bird? B:Yes, please. A:Do you want beef or lamb?(牛肉还是羊肉) B:Beef, please. A:This lamb's very good. B:I like lamb, but my husband doesn't. A:What about some steak?(…
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm? 分析 同LeetCode(274)H-Index第二个版本,给定引用数量序列为递增的:这就省略了我们的第一个排序步骤: O(n)的时间复杂度,遍历一次即可. AC代码 class Solution { public: int hIndex(vector<int>…
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array such that the difference between nums[i] and nums[j] is at most t and the difference between i and j is at most k. 分析 题目描述:给定一个整数序列,查找是否存在两个下标分别为i和j的元…
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh…
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times).…
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL. Initially,…
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 分析 本题目与上一题LeetCode(112) Path Sum虽然类型相同,但是需要在以前的基础上,多做处理一些: 与Path Sum相比,本题是求出路径,所以,在找到满足的路…
题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example: Given binary tree {3,9,20,#,#,15,7}, return its bottom-up level order traversal as: 分析 与…
题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). 分析 给定两个有序序列,要求两个序列综合后的中位数.关键:算法复杂度T(n)=O(log(m+n)) 该问题的关键就在于复杂度的限制,有了这个限制,就…
Leetcode(6)Z字形变换 [题目表述]: 将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下: L C I R E T O E S I I G E D H N 之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:"LCIRETOESIIGEDHN". 第一次:找下标规律按行输出 执行用时:84 ms: 内存消耗:11.8MB 效果:还行 cla…
Max Points on a Line Submission Details 27 / 27 test cases passed. Status: Accepted Runtime: 472 ms Submitted: 0 minutes ago Submitted Code Language: java Edit Code /** * Definition for a point. * class Point { * int x; * int y; * Point() {…
Max Points on a Line Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. SOLUTION 1: 全部的点扫一次,然后计算每一个点与其它点之间的斜率. 创建一个MAP, KEY-VALUE是 斜率:线上的点的数目. 另外,注意重合的点,每次都要累加到每一条线上. 注意: 1. k = 0 + (double)(points[i].…
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. Have you met this question in a real interview? Example Given 4 points: (1,2), (3,6), (0,0), (1,3). The maximum number is 3. LeetCode上的原题,请参见我之前的博…
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. Example 1: Input: [[1,1],[2,2],[3,3]] Output: 3 Explanation: ^ | | o | o | o +-------------> 0 1 2 3 4 Example 2: Input: [[1,1],[3,2],[5,3],[4,1],[2,3…
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 这道题给了我们一堆二维点,然后让我们求最大的共线点的个数,根据初中数学我们知道,两点确定一条直线,而且可以写成y = ax + b的形式,所有共线的点都满足这个公式.所以这些给定点两两之间都可以算一个斜率,每个斜率代表一条直线,对每一条直线,带入所有的点看是否共线并计算个数,这是整体的思路.但是还有…
题目: Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 题解: 这道题就是给你一个2D平面,然后给你的数据结构是由横纵坐标表示的点,然后看哪条直线上的点最多. (1)两点确定一条直线 (2)斜率相同的点落在一条直线上 (3)坐标相同的两个不同的点 算作2个点 利用HashMap,Key值存斜率,Value存此斜率下的点的个数.同时考虑特殊情况,如…
题目 Given n non-negative integers representing the histogram’s bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3]. The large…