- 浏览: 141206 次
-
文章分类
- 全部博客 (189)
- Tree (14)
- Dynamic Programming (34)
- Array (20)
- Search (1)
- Hash (12)
- Backtracking (22)
- Divide and Conque (8)
- Greedy (6)
- Stack (12)
- software (0)
- List (7)
- Math (22)
- Two pointers (16)
- String (20)
- Linux (1)
- Sliding Window (4)
- Finite State Machine (1)
- Breadth-first Search (7)
- Graph (4)
- DFS (6)
- BFS (3)
- Sort (9)
- 基础概念 (2)
- 沟通表达 (0)
- Heap (2)
- Binary Search (15)
- 小结 (1)
- Bit Manipulation (8)
- Union Find (4)
- Topological Sort (1)
- PriorityQueue (1)
- Design Pattern (1)
- Design (1)
- Iterator (1)
- Queue (1)
最新评论
-
likesky3:
看了数据结构书得知并不是迭代和递归的区别,yb君的写法的效果是 ...
Leetcode - Graph Valid Tree -
likesky3:
迭代和递归的区别吧~
Leetcode - Graph Valid Tree -
qb_2008:
还有一种find写法:int find(int p) { i ...
Leetcode - Graph Valid Tree -
qb_2008:
要看懂这些技巧的代码确实比较困难。我是这么看懂的:1. 明白这 ...
Leetcode - Single Num II -
qb_2008:
public int singleNumber2(int[] ...
Leetcode - Single Num II
An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black pixels are connected, i.e., there is only one black region. Pixels are connected horizontally and vertically. Given the location (x, y) of one of the black pixels, return the area of the smallest (axis-aligned) rectangle that encloses all black pixels.
For example, given the following image:
[
"0010",
"0110",
"0100"
]
and x = 0, y = 2,
Return 6.
[备注]
使用二分查找找出上下左右边界,其中上边界和左边界分别是包含了black区域最上面和最左边的pixel,而下边界和右边界分别是black区域下面和右边第一个white pixel,这样处理代码可以精简些,但我的思维方式却是僵硬地去找准确的四个边界。
另外,二分查找中左右边界调整的代码不容易理解,注释中给出了精简前的写法。
[ref]
https://leetcode.com/discuss/71898/1ms-java-binary-search-dfs-is-4ms
For example, given the following image:
[
"0010",
"0110",
"0100"
]
and x = 0, y = 2,
Return 6.
[备注]
使用二分查找找出上下左右边界,其中上边界和左边界分别是包含了black区域最上面和最左边的pixel,而下边界和右边界分别是black区域下面和右边第一个white pixel,这样处理代码可以精简些,但我的思维方式却是僵硬地去找准确的四个边界。
另外,二分查找中左右边界调整的代码不容易理解,注释中给出了精简前的写法。
[ref]
https://leetcode.com/discuss/71898/1ms-java-binary-search-dfs-is-4ms
public class Solution { // Solution 1: DFS, 4ms public int minArea1(char[][] image, int x, int y) { m = image.length; n = image[0].length; int[] edgePoints = {y, y, x, x}; recur(image, x, y, edgePoints); int width = edgePoints[1] - edgePoints[0] + 1; int height = edgePoints[3] - edgePoints[2] + 1; return width * height; } private int m, n; private int[] xDelta = {0, 0, 1, -1}; private int[] yDelta = {1, -1, 0, 0}; public void recur(char[][] image, int x, int y, int[] edgePoints) { if (image[x][y] != '1') return; // 0 or 2(visited) edgePoints[0] = Math.min(edgePoints[0], y); edgePoints[1] = Math.max(edgePoints[1], y); edgePoints[2] = Math.min(edgePoints[2], x); edgePoints[3] = Math.max(edgePoints[3], x); image[x][y] = '2'; for (int i = 0; i < 4; i++) { int x2 = x + xDelta[i]; int y2 = y + yDelta[i]; if (x2 >= 0 && x2 < m && y2 >= 0 && y2 < n) recur(image, x2, y2, edgePoints); } } // Solution 2: binary search, 1ms public int minArea(char[][] image, int x, int y) { int m = image.length, n = image[0].length; int left = binarySearch(image, 0, y, 0, m, false, true); // search the first pos which belong to black int right = binarySearch(image, y + 1, n, 0, m, false, false); // search the first pos which not belong to black int top = binarySearch(image, 0, x, 0, n, true, true); int bottom = binarySearch(image, x + 1, m, 0, n, true, false); return (right - left) * (bottom - top); } public int binarySearch(char[][] image, int lower, int upper, int min, int max, boolean horizontal, boolean goLower) { int mid; while (lower < upper) { mid = lower + (upper - lower) / 2; boolean inside = false; for (int i = min; i < max; i++) { if ((horizontal ? image[mid][i] : image[i][mid]) == '1') { inside = true; break; } } if (inside == goLower) { upper = mid; } else { lower = mid + 1; } /* if (inside) { // find black pixel, mid is in black region. if (goLower) { upper = mid; } else { lower = mid + 1; } } else { if (goLower) { lower = mid + 1; } else { upper = mid; } } */ } return lower; } }
发表评论
-
Lowest Common Ancestor of A Binary Tree
2015-09-26 11:39 589[分析] 最近公共祖先(LCA)是一个经典问题,以前没有好好 ... -
Leetcode - H-Index II
2015-09-06 09:39 1114Follow up for H-Index: What if ... -
Leetcode - Number of Islands
2015-09-02 09:40 2966[分析] BFS & DFS法详见实现。 这里阐述下u ... -
Leetcode - Strobogrammatic Number II
2015-08-22 11:34 1620A strobogrammatic number is a n ... -
Leetcode - Insert Interval
2015-08-14 10:12 645[分析] 这道题思路直接,但bug free需要费些心力。第一 ... -
Leetcode - Pow(x, n)
2015-08-11 09:45 481[分析] 数值计算类型题目,二分法或者借助位运算,本题两种方法 ... -
Leetcode - sqrt(x)
2015-08-10 21:40 833[分析] 这是一道数值计算的题目,Code Ganker中指出 ... -
Leetcode - Kth Smallest Element in BST
2015-08-11 10:26 674[分析] 思路1: 递归中序遍历,返回中序遍历中的第k个数。 ... -
Leetcode - Search for a Range
2015-08-10 08:32 518[分析] 思路1: 伪二分查找。若中间元素正好是target时 ... -
二分查找算法小结
2015-08-10 08:32 969在做leetcode二分查找类 ... -
Leetcode - Search in Rotated Sorted Array II
2015-08-09 18:47 495[分析] 同Search in Rotated Sorted ... -
Leetcode - Search in Rotated Sorted Array
2015-08-09 17:47 544[分析] 这题可以看做是Find Minimum in Rot ... -
Leetcode - Find Peak Element
2015-08-09 16:56 619[分析] 暴力法,按序扫描数组,找到peak element。 ... -
Leetcode - Find Minimum in Rotated Sorted Array II
2015-08-09 12:19 826[分析] Find Minimum in Rotated So ... -
Leetcode - Search Insert Position
2015-08-09 11:06 440[分析] 经典二分查找,下面代码实现了迭代方式,可以验证,若直 ... -
Leetcode - Minimum Size Subarray Sum
2015-07-22 21:01 774Given an array of n positive in ... -
Leetcode - Recover Binary Search Tree
2015-07-02 09:27 548Two elements of a binary search ... -
Leetcode - Validate Binary Search Tree
2015-07-01 08:31 616Given a binary tree, determine ... -
Leetcode - Count Complete Tree Nodes
2015-06-07 20:51 789Definition of a complete binary ...
相关推荐
标题 "the kth smallest elem" 暗示我们要讨论的是如何在数组或集合中找到第k小的元素,这是一个常见的算法问题,在计算机科学和数据结构领域具有重要意义。描述中提到的 "Clion" 是一个流行的C/C++集成开发环境,...
"smallest-width"这个概念正是Android系统提供的一种解决方案,用于帮助开发者创建能够适应不同屏幕尺寸的应用。本文将深入探讨"smallest-width"的概念,以及如何利用它来实现Android自定义`dimens.xml`文件,以适应...
373._Find_K_Pairs_with_Smallest_Sums查找和最小的k对数字【LeetCode单题讲解系列】
代码中使用了多个HALCON函数,如 fast_threshold, connection, select_shape, dilation_rectangle1, area_center_gray, smallest_rectangle2, gen_region_points, hom_mat2d_identity, hom_mat2d_rotate, hom_mat2d_...
我的思路是,用smallest_rectangle1_xld求一下CAD轮廓的最小外接矩形,得到矩形两个角的坐标(row1,column1),(row2,column2)。然后用set_part(windowhandle,row1,column1,row2,column2)。但是这样显示出来的轮廓...
本篇文章主要讲解了如何使用Python语言来解决LeetCode上的一个特定问题——“Smallest Even Multiple”(最小偶数倍数)。具体到该问题,它要求编写一个函数,接收一个整数n作为输入,返回比n大的最小偶数倍数。 此...
代码:// find K-th Smallest Pair Distancepublic int smallestDistancePair(int[] nums
smallest_rectangle2(Regions : : : Row, Column, Phi, Length1, Length2) draw_rectangle2:窗口有个箭头方向,这个方向就是矩形的角度Phi,和Phi方向一致的边为Length1,和Phi方向垂直的边为Length2 small
将您的过程解决方案编码到lib/smallest_multiple.rb文件中。 将您的面向对象的解决方案编码到lib/oo_smallest_multiple.rb文件中。 运行learn直到所有RSpec测试通过。 来源 -- 在Learn.co上查看并开始免费学习编码。
AT_code_festival_2017_qualb_f Largest Smallest Cyclic Shift 的 AC 代码
c c语言_leetcode题解之0230_kth_smallest_element_in_a_bst
在本文档中,我们将会探索Java语言编写的LeetCode题解,该题解针对的是"Find K-th Smallest Pair Distance"这一具体问题。此问题要求我们对一个整数数组找出第k小的数对距离。数对距离是指一对整数之间的差的绝对值...
在这个名为"the-smallest-tree.rar_The Tree"的压缩包中,包含了关于最小生成树算法的源代码,采用的是Visual C++ 6.0编程环境。 在图论中,一个无向图的最小生成树是指包含图中所有顶点的一个子图,且该子图是树形...
在编程题库平台LeetCode上,有一道著名的算法题,名为“Find K Pairs with Smallest Sums”。这道题要求求出两个未排序数组的k个最小和的组合。具体来说,假设有两个整数数组nums1和nums2,返回这两数组中所有可能的...
378.Kth_Smallest_Element_in_a_Sorted_Matrix有序矩阵中第K小的元素【LeetCode单
"smallest3hq"可能指的是C8051F单片机的最小系统设计,该设计通常包括电源、晶振、复位电路以及必要的I/O接口。在C语言编程中,我们需要初始化这些硬件资源,如设置时钟频率、配置I/O口等。 五、实践与研究 ...
在Java语言实现的LeetCode题解中,"Kth Smallest Element in a BST"这一问题尤为引人注目,它涉及到了二叉树的遍历以及对树的特性进行深度利用。 二叉搜索树具有独特的特性:对于任意节点,其左子树中的所有元素都...
在处理这个问题之前,我们首先需要理解题目“Kth Smallest Element in a Sorted Matrix”的意思。这道题是LeetCode上的算法题目,要求求解在一个按行和列都排好序的矩阵中,找到第k小的元素。此类问题通常涉及到矩阵...
在这个"smallest_tree.rar_最小生成树_最小生成树 C++"的压缩包中,包含了C++语言实现的最小生成树算法,对于学习数据结构和算法的初学者来说,是一个非常实用的学习资源。 最小生成树的算法主要有两种经典方法:...