- 浏览: 138542 次
文章分类
- 全部博客 (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 574[分析] 最近公共祖先(LCA)是一个经典问题,以前没有好好 ... -
Leetcode - H-Index II
2015-09-06 09:39 1105Follow up for H-Index: What if ... -
Leetcode - Number of Islands
2015-09-02 09:40 2943[分析] BFS & DFS法详见实现。 这里阐述下u ... -
Leetcode - Strobogrammatic Number II
2015-08-22 11:34 1605A strobogrammatic number is a n ... -
Leetcode - Insert Interval
2015-08-14 10:12 634[分析] 这道题思路直接,但bug free需要费些心力。第一 ... -
Leetcode - Pow(x, n)
2015-08-11 09:45 468[分析] 数值计算类型题目,二分法或者借助位运算,本题两种方法 ... -
Leetcode - sqrt(x)
2015-08-10 21:40 817[分析] 这是一道数值计算的题目,Code Ganker中指出 ... -
Leetcode - Kth Smallest Element in BST
2015-08-11 10:26 663[分析] 思路1: 递归中序遍历,返回中序遍历中的第k个数。 ... -
Leetcode - Search for a Range
2015-08-10 08:32 504[分析] 思路1: 伪二分查找。若中间元素正好是target时 ... -
二分查找算法小结
2015-08-10 08:32 957在做leetcode二分查找类 ... -
Leetcode - Search in Rotated Sorted Array II
2015-08-09 18:47 484[分析] 同Search in Rotated Sorted ... -
Leetcode - Search in Rotated Sorted Array
2015-08-09 17:47 533[分析] 这题可以看做是Find Minimum in Rot ... -
Leetcode - Find Peak Element
2015-08-09 16:56 609[分析] 暴力法,按序扫描数组,找到peak element。 ... -
Leetcode - Find Minimum in Rotated Sorted Array II
2015-08-09 12:19 816[分析] Find Minimum in Rotated So ... -
Leetcode - Search Insert Position
2015-08-09 11:06 427[分析] 经典二分查找,下面代码实现了迭代方式,可以验证,若直 ... -
Leetcode - Minimum Size Subarray Sum
2015-07-22 21:01 765Given an array of n positive in ... -
Leetcode - Recover Binary Search Tree
2015-07-02 09:27 534Two elements of a binary search ... -
Leetcode - Validate Binary Search Tree
2015-07-01 08:31 600Given a binary tree, determine ... -
Leetcode - Count Complete Tree Nodes
2015-06-07 20:51 778Definition 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单题讲解系列】
java java_leetcode题解之Kth Smallest Number in Multiplication Table.java
java java_leetcode题解之K-th Smallest Prime Fraction.java
java java_leetcode题解之Kth Smallest Element in a BST.java
代码中使用了多个HALCON函数,如 fast_threshold, connection, select_shape, dilation_rectangle1, area_center_gray, smallest_rectangle2, gen_region_points, hom_mat2d_identity, hom_mat2d_rotate, hom_mat2d_...
java java_leetcode题解之Find K Pairs with Smallest Sums.java
NSudo可以帮助你获得系统中的最高权限——System。这使得您可以轻而易举的进行一些系统的灾难排查/修复/重建工作或Rootkit取样、系统修改等任务。非常感谢M2Team。
java java_leetcode题解之Kth Smallest Element in a Sorted Matrix.java
java java_leetcode题解之Find K-th Smallest Pair Distance.java
python python_leetcode题解之2413_Smallest_Even_Multiple.py
我的思路是,用smallest_rectangle1_xld求一下CAD轮廓的最小外接矩形,得到矩形两个角的坐标(row1,column1),(row2,column2)。然后用set_part(windowhandle,row1,column1,row2,column2)。但是这样显示出来的轮廓...
c++ C++_leetcode题解之744. Find Smallest Letter Greater Than Target.cpp
代码:// 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题解之668_Kth_Smallest_Number_in_Multiplication_Table
c c语言_leetcode题解之0230_kth_smallest_element_in_a_bst