- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我们有一个任务来搜索排序数组的最小元素,然后向右移动。例如:[1, 5, 6, 19, 56, 101] 变为 [19, 56, 101, 1, 5, 6]。该方法应该使用分治算法来实现,并且应该具有比 O(n) 更好的渐近时间复杂度。编辑:我忘记添加数组中的元素是唯一的。
我已经实现了一个方法,想问一下这是否比 O(n) 更好,以及是否有方法可以改进我的方法。
public class FindMinimum {
public void findMinimum(int[] arr) {
// the recursive method ends when the length of the array is smaller than 2
if (arr.length < 2) {
return;
}
int mid = arr.length / 2;
/*
* if the array length is greater or the same as two, check if the middle
* element is smaller as the element before that. And print the middle element
* if it's true.
*/
if (arr.length >= 2) {
if (arr[mid - 1] > arr[mid]) {
System.out.println("Minimum: " + arr[mid]);
return;
}
}
/*
* separate the array in two sub-arrays through the middle and start the method
* with those two arrays again.
*/
int[] leftArr = new int[mid];
int[] rightArr = new int[arr.length - mid];
for (int i = 0; i < mid; i++) {
leftArr[i] = arr[i];
}
for (int i = mid; i < arr.length; i++) {
rightArr[i - mid] = arr[i];
}
findMinimum(leftArr);
findMinimum(rightArr);
}
}
最佳答案
在 Java 中,您可以使用列表,因为您可以创建子列表。
private Integer findMinimum(List<Integer> list) {
if (list.size() < 2)
return list.get(0);
int mid = list.size() / 2;
// create left and right list
List<Integer> leftList = list.subList(0, mid);
List<Integer> rightList = list.subList(mid, list.size());
if (leftList.get(leftList.size() - 1) <= rightList.get(rightList.size() - 1))
return findMin(leftList);
else
return findMin(rightList);
}
当您使用 Java 创建子列表时,没有副本。因此创建一个新的子列表需要 O(1) 的复杂度。所以该函数的复杂度为O(logn)。
关于java - 查找已排序和移位数组的最小值,时间复杂度优于 O(n),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56313529/
我想显示一个图像(大于 iPhone 的屏幕),用户可以滚动。不难,我已经用这段代码完成了:在我的 .h 文件中 @interface mappa1 : UIViewController { IBO
bash 联机帮助页说: Redirecting Standard Output and Standard Error Bash allows both the standard output (fi
我将大量数据绑定(bind)到 TreeView 控件,因为数据是自然的类别层次结构。问题是它有很多。我已经设法通过仅绑定(bind)那些出现在可见树中的节点来消除大量开销,但这仍然在 ViewSta
我正在尝试为 mysql 操作与 redis 操作制作基准测试脚本。 这是我尝试过的: 1./ List of comment ids with a separate hash of comment
我很想知道是否有比 FileObserver 更好的方法来监视目录的文件更改/创建/删除。 FileObserver 需要持续引用它,这需要持续运行的服务,这很困难,也是一种不好的做法,对吧? 对于如
我创建了一个带有 NSOpenGLView 的窗口,我正在将 openGL 内容渲染到其中。 我想向 View 添加一些按钮和文本字段:我可以使用界面生成器(或代码)添加 NSTextFields 和
如何编码 JAXBElement 列表? 例如,我有一个无法注释的 POJO: public class APojo { private String aString; public APojo()
我正在开发一个博客,我的客户希望在其中使用大量图像(文章、标题、广告等)。他几乎不想要任何文本,因为他希望用阿拉伯语开发博客并且他对网络浏览器支持的任何字体都不满意,他也不想采用 EOT,他将每天更新
我想在文本字段上添加一个标签,该标签会在用户键入时发生变化。问题是文本字段的插入符号位于标签后面(如果我将标签放在文本字段前面)。我始终可以使文本字段的背景透明,并在标签和文本字段后面添加另一个禁用的
我正在尝试在 iOS 应用程序中创建一个特定的布局,其中“背景”是一个 MKMapView,覆盖层是一个 UIScrollView。这个想法是有两个屏幕,一个带有 map ,另一个带有一些附加信息。用
我需要在 UITableViewController 上放置一个 UIView,目前我是这样放置的 [self.navigationController.view addSubview:searchV
我有一个非常简单的问题要问:我需要在屏幕右下角的 ImageView 上放一个小 Logo ,整个屏幕都很大,但我不知道如何设置坐标或如何设置说 ImageViews 处于相对位置。 像这样: 最佳答
我试图让 mapView 覆盖整个 UITableViewCell 并禁用此 mapView 上的所有用户事件,但仍然可以单击单元格。但是,此 mapView(即使我将 subview 发送回)正在拦
我们即将获得 Java EE6(使用 Glassfish v3 作为引用实现)。计划发布时间为 12 月 9 日。虽然仍有相当多的公司正在努力将他们的代码库从早期版本迁移到 EE5,但我们处于开始开发
为什么(在 WPF、C#、 Entity Framework 中)将 ListBox 绑定(bind)到在 ObjectSet 上创建的 ObservableCollection(来自 Entity框
在 hibernate 中使用 @NamedQuery 而不是 @NamedNativeQuery 有什么好处,反之亦然。我无法发现确切的区别或在什么情况下我们应该使用 @NamedQuery 而不是
在 Dart 中,检查值是否为 == null似乎类似于检查它是否is Null .为什么前者更可取? 最佳答案 这是您正在进行的比较类型。在 == null ,您正在将对象/原始对象与 null 进
我使用的是 Java 1.6。我有一组项目,每个项目都有一个名称和一组组件。每个组件也有一个名称。 Set Class Item String name Set Class Compo
如果我想在我的应用程序中支持脚本,是否 scriptcs提供比仅使用普通 Vanilla 的任何特殊优势 Roslyn脚本引擎? 最佳答案 不幸的是,目前还没有太多关于托管 scriptcs 的文档,
我正在我的应用程序中使用 Struts 和 Spring jdbc 模板。 我必须在我们的代码中使用 Hibernate 模板。 谁能告诉我为什么要使用 Hibernate 模板? 使用 Spring
我是一名优秀的程序员,十分优秀!