查找峰值元素(多个)SWIFT(Find peak elements (multiple) SWIFT)

我将如何在数组中找到多个峰?

给定数组:[1,2,3,4,5,4,3,2,3,4,5,6,5,4,3,2,4,2]我可以使用代码找到最大峰值下面。

func findPeakElement(_ readings: [Int]) -> Int {
   if readings.count == 0 {
      return 0
   }

   while left < right {
      middle1 = (left + right) / 2
      middle2 = middle1 + 1
      if readings[middle1] < readings[middle2] {
         left = middle2
      } else {
         right = middle1
      }
   }         
   return left
}

然而; 我需要找到所有的高峰; [5,6,4]我需要什么代码? 即使它只是告诉我峰值的数量(3)就足够了。


how would i find multiple peaks in an array?

given the array: [1,2,3,4,5,4,3,2,3,4,5,6,5,4,3,2,4,2] i can find the max peak using the code below.

func findPeakElement(_ readings: [Int]) -> Int {
   if readings.count == 0 {
      return 0
   }

   while left < right {
      middle1 = (left + right) / 2
      middle2 = middle1 + 1
      if readings[middle1] < readings[middle2] {
         left = middle2
      } else {
         right = middle1
      }
   }         
   return left
}

however; i need to find ALL the peaks ;[5,6,4] what code would i need for this? even if it just told me the number of peaks (3) it would be enough.


原文:https://stackoverflow.com/questions/48235912
2023-11-06 08:11

满意答案

如果我理解正确,您的设备对象可以与多个用户,UserInfo或DisplayInfo相关联,这种情况下我会将它们逻辑地分组为DeviceUser,DeviceUserInfo和DeviceDisplayInfo对象,并在其中包含User / UserInfo / DisplayInfo的IEnumerable集合。

例,

DeviceUser
- DeviceId (int)
- Users (IEnumerable<User>)
- Make (string)
- Year (int)

If I understand right, your device object can be associated with multiple Users, UserInfo's or DisplayInfo's, which case I would logically group them as DeviceUser, DeviceUserInfo and DeviceDisplayInfo objects and have a IEnumerable collection of the User / UserInfo / DisplayInfo inside it.

Example,

DeviceUser
- DeviceId (int)
- Users (IEnumerable<User>)
- Make (string)
- Year (int)

相关问答

更多

从WCF发送信息到JavaScript(Send information from the WCF to JavaScript)

SignalR是一个很好的方法来做到这一点, 您可以在以下问题中详细了解这一点: 使用SignalR从wcf通知JS SignalR is a good way to do it, you can see this in detail in the following question: Use SignalR to notify from wcf to JS

将多个文件发送到WCF服务的最佳方式是什么?(What is the best way to send multiple files to a WCF service?)

有可能有很多方法来完成这个,但这里是我的想法。 请注意,我只包含了任意数量的字符串,您可以根据需要添加/删除。 首先,您需要某种“输入”对象,该对象将传递给包含图像和字符串值的WCF服务。 注意Images属性是一个字节数组的数组; 这是因为你可以包含多个图像。 [DataContract] public class InputObject { [DataMember] public byte[][] Images { get; set; } [DataMember] ...

需要保护WCF数据,但仅限于一个部分(Need to secure WCF data, but just for one section)

是的,您需要2个具有单独合同的WCF服务,但它们可以托管在同一个站点上。 使用具有“传输安全性”的绑定配置要使用SSL的那个。 Yes you need 2 WCF services with seperate contracts, but they can be hosted onn the same site. Configure the one that you want to use SSL with a binding that has "Transport Security".

使用不同Structs将相关数据发送到WCF的最佳方法(Best way to send related data to WCF with different Structs)

如果我理解正确,您的设备对象可以与多个用户,UserInfo或DisplayInfo相关联,这种情况下我会将它们逻辑地分组为DeviceUser,DeviceUserInfo和DeviceDisplayInfo对象,并在其中包含User / UserInfo / DisplayInfo的IEnumerable集合。 例, DeviceUser - DeviceId (int) - Users (IEnumerable<User>) - Make (string) - Year (int) If ...

通过WCF发送数据(Sending data via WCF)

WCF可以通过许多不同的传输协议(如MSMQ和http)发送数据。 它还支持消息安全性,分布式事务和分布式系统的其他更复杂的功能。 您需要创建一个WCF服务,该服务在Visual Studio中作为模板提供。 服务器应作为独立程序os托管,作为IIS中的Web应用程序。 之后,您需要创建一个可以与服务器通信的客户端。 然而,WCF是一个庞大而复杂的框架,您不应该期望能够只抓住表面并构建系统。 你需要一些谷歌搜索,并可能从MS自己的教程开始。 如果您需要真正有用的答案,您应该更加具体地了解您的程序以...

WCF发送大量数据(WCF sending huge data)

WCF支持流式传输,允许以高效的方式将大文件传输到服务端点或从服务端点传输。 有关更多信息,请查看此文章 。 WCF supports streaming to allow large files to be transferred to/from a service endpoint in a performant fashion. Check out this article for more information.

WCF:将表单数据发送到Web服务的最佳实践?(WCF: Best practice to send Form Data to a Web Service?)

我们使用WCF来使用属性来定义我们的数据合同和数据方法。 基本上我们创建一个程序集来定义我们所有的类和另一个程序集,以提供WCF连接位 OPur类程序集包含一个服务类和几个消息类。 我们为我们的服务定义一个接口,并将其标记为相关的WCF标记。 这是我们的服务合同。 [ServiceContract] public interface IExampleWebService { [OperationContract] CreateAccountResponse CreateAccoun...

WCF。(WCF. to send data through service and client)

你可以使用你想要的任何数据技术。 但是,直到.NET 4中的实体框架4(目前处于发布候选状态),建议不要从Web服务返回实体框架实体或LINQ to SQL类。 不幸的是,这两种技术都通过网络泄露了它们的实现 - 客户端代理类将具有与数据框架使用的基类相对应的客户端类。 相反,请使用数据传输对象,该对象只具有与要传输的数据的属性一一对应的属性。 You can use whichever data technology you want. However, up to Entity Framewo...

WCF:发送多个具有相同名称的参数(WCF : send multiple params with same name)

您可以尝试使用params关键字。 就像是: public void MyMethod(params object[] list) { // access by index like list[0] etc. } 如果需要,你可以使用int或string代替object类型。 您可以在http://msdn.microsoft.com/en-us/library/w5zay9db.aspx上阅读有关params更多信息。 You can try to use params keyword...

将fileupload发送到WCF(send fileupload to WCF)

我最近尝试了同样的事情但失败了。 在这种情况下最好使用js。 http://www.enfew.com/12-excellent-ajax-upload-plugins/ 如果您使用的是现代浏览器,或者使用旧版浏览器的iFrame,他们中的许多人都会使用ajax。 因此浏览器兼容性也不是问题。 我自己使用https://code.google.com/p/upload-at-click/。它非常简单易用,可以满足您的需求。 I tried the same thing recently and f...

相关文章

更多

千锋首发Swift视频教程

千锋Swift视频教程-7.Swift结构体.mp4 千锋Swift视频教程-13.代理反向传值.mp ...

Swift入门视频教程-尚学堂视频教程

最新Swift语言语法介绍,包括Swift流程控制语句、Swift各种构造函数、closure、泛型、 ...

Solr4:Tomcat7与Solr之多核配置(Multiple Cores)

1. 背景 多核,官方说法,让你只用一个Solr实例,实现多配置多索引的功能,为不同的应用保留不同的 ...

学习di'z地址

杨丰盛cocos2d-x教程下载地址:http://www.dwz.cn/gwPgd 无限互联新浪微博 ...

Hadoop配置多个硬盘

在Hadoop的文档里明确提出了不需要使用RAID(见http://wiki.apache.org/h ...

HTML5 Audio元素【HTML5教程 - 第五篇】

直到目前为止,web页面上没有标准的方式来播放音频文件。目前大多数的音频文件是使用插件来播放。但是,很 ...

Prince Of Persia III - The Two Thrones 通关完成

&quot;Prince,there still something I don't understa ...

Hadoop下运行多个SecondaryNameNode的配置

光从字面上来理解,很容易让一些初学者先入为主的认为:SecondaryNameNode(snn)就是N ...

could not find system property or JNDI

Thanks everyone!! Finally got a solution for this p ...

最新问答

更多

获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)

我用Google搜索了一个解决方案。 “EnumDisplayModeProvider”是我自己设置网站的各种模式的枚举。 public EnumDisplayModeProvider GetDisplayModeId() { foreach (var mode in DisplayModeProvider.Instance.Modes) if (mode.CanHandleContext(HttpContext)) {

如何通过引用返回对象?(How is returning an object by reference possible?)

这相对简单:在类的构造函数中,您可以分配内存,例如使用new 。 如果你制作一个对象的副本,你不是每次都分配新的内存,而是只复制指向原始内存块的指针,同时递增一个也存储在内存中的引用计数器,使得每个副本都是对象可以访问它。 如果引用计数降至零,则销毁对象将减少引用计数并仅释放分配的内存。 您只需要一个自定义复制构造函数和赋值运算符。 这基本上是共享指针的工作方式。 This is relatively easy: In the class' constructor, you allocate m

矩阵如何存储在内存中?(How are matrices stored in memory?)

正如它在“熵编码”中所说的那样,使用Z字形图案,与RLE一起使用,在许多情况下,RLE已经减小了尺寸。 但是,据我所知,DCT本身并没有给出稀疏矩阵。 但它通常会增强矩阵的熵。 这是compressen变得有损的点:输入矩阵用DCT传输,然后量化量化然后使用霍夫曼编码。 As it says in "Entropy coding" a zig-zag pattern is used, together with RLE which will already reduce size for man

每个请求的Java新会话?(Java New Session For Each Request?)

你是如何进行重定向的? 您是否事先调用了HttpServletResponse.encodeRedirectURL()? 在这里阅读javadoc 您可以使用它像response.sendRedirect(response.encodeRedirectURL(path)); The issue was with the path in the JSESSIONID cookie. I still can't figure out why it was being set to the tomca

css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)

我认为word-break ,如果你想在一个单词中打破行,你可以指定它,这样做可以解决问题: .column { word-break:break-all; } jsFiddle演示。 您可以在此处阅读有关word-break属性的更多信息。 I think word-break, with which you can specify if you want to break line within a word, will do the trick: .column { word-break

无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)

我认为您忘记在分类时间内缩放输入图像,如train_test.prototxt文件的第11行所示。 您可能应该在C ++代码中的某个位置乘以该因子,或者使用Caffe图层来缩放输入(请查看ELTWISE或POWER图层)。 编辑: 在评论中进行了一次对话之后,结果发现在classification.cpp文件中错误地删除了图像均值,而在原始训练/测试管道中没有减去图像均值。 I think you have forgotten to scale the input image during cl

xcode语法颜色编码解释?(xcode syntax color coding explained?)

转到: Xcode => Preferences => Fonts & Colors 您将看到每个语法高亮颜色旁边都有一个简短的解释。 Go to: Xcode => Preferences => Fonts & Colors You'll see that each syntax highlighting colour has a brief explanation next to it.

在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)

你考虑过第三方拼写检查吗? 您可以将在C#中开发的自定义WinForms控件插入访问数据库吗? VB6控件怎么样? 如果你能找到一个使用第三方库进行拼写检查的控件,那可能会有效。 Have you considered a third party spell checker? Can you insert a custom WinForms controls developed in C# into an access database? What about a VB6 control? If

从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)

我有同样的问题,因为我在远程服务器上有两个图像,我需要在每天的预定义时间复制到我的本地服务器,这是我能够提出的代码... try { if(@copy('url/to/source/image.ext', 'local/absolute/path/on/server/' . date("d-m-Y") . ".gif")) { } else { $errors = error_get_last(); throw new Exception($err

从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))

我不确定我完全明白你在说什么。 你能编辑你的帖子并包含你正在做的Subversion命令/操作的特定顺序吗? 最好使用命令行svn客户端,以便容易为其他人重现问题。 如果您只是想获取文件的旧副本(即使该文件不再存在),您可以使用如下命令: svn copy ${repo}/trunk/moduleA/file1@${rev} ${repo}/trunk/moduleB/file1 其中${repo}是您的存储库的URL, ${rev}是您想要的文件的版本。 这将恢复该文件的旧版本,包括最高版本