首页 \ 问答 \ 查找峰值元素(多个)SWIFT(Find peak elements (multiple) SWIFT)

查找峰值元素(多个)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)

相关问答

更多
  • 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服务。 注意Images属性是一个字节数组的数组; 这是因为你可以包含多个图像。 [DataContract] public class InputObject { [DataMember] public byte[][] Images { get; set; } [DataMember] pu ...
  • 是的,您需要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".
  • 如果我理解正确,您的设备对象可以与多个用户,UserInfo或DisplayInfo相关联,这种情况下我会将它们逻辑地分组为DeviceUser,DeviceUserInfo和DeviceDisplayInfo对象,并在其中包含User / UserInfo / DisplayInfo的IEnumerable集合。 例, DeviceUser - DeviceId (int) - Users (IEnumerable) - Make (string) - Year (int) If I und ...
  • WCF可以通过许多不同的传输协议(如MSMQ和http)发送数据。 它还支持消息安全性,分布式事务和分布式系统的其他更复杂的功能。 您需要创建一个WCF服务,该服务在Visual Studio中作为模板提供。 服务器应作为独立程序os托管,作为IIS中的Web应用程序。 之后,您需要创建一个可以与服务器通信的客户端。 然而,WCF是一个庞大而复杂的框架,您不应该期望能够只抓住表面并构建系统。 你需要一些谷歌搜索,并可能从MS自己的教程开始。 如果您需要真正有用的答案,您应该更加具体地了解您的程序以及客户端和 ...
  • 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来使用属性来定义我们的数据合同和数据方法。 基本上我们创建一个程序集来定义我们所有的类和另一个程序集,以提供WCF连接位 OPur类程序集包含一个服务类和几个消息类。 我们为我们的服务定义一个接口,并将其标记为相关的WCF标记。 这是我们的服务合同。 [ServiceContract] public interface IExampleWebService { [OperationContract] CreateAccountResponse CreateAccount(int ...
  • 你可以使用你想要的任何数据技术。 但是,直到.NET 4中的实体框架4(目前处于发布候选状态),建议不要从Web服务返回实体框架实体或LINQ to SQL类。 不幸的是,这两种技术都通过网络泄露了它们的实现 - 客户端代理类将具有与数据框架使用的基类相对应的客户端类。 相反,请使用数据传输对象,该对象只具有与要传输的数据的属性一一对应的属性。 You can use whichever data technology you want. However, up to Entity Framework 4 ...
  • 您可以尝试使用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. Som ...
  • 我最近尝试了同样的事情但失败了。 在这种情况下最好使用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 failed ...

相关文章

更多

最新问答

更多
  • 如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
  • linux的常用命令干什么用的
  • 由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)
  • 如何交换返回集中的行?(How to swap rows in a return set?)
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • 使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)
  • Java中的不可变类(Immutable class in Java)
  • WordPress发布查询(WordPress post query)
  • 如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)
  • 是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)
  • GEP分段错误LLVM C ++ API(GEP segmentation fault LLVM C++ API)
  • 绑定属性设置器未被调用(Bound Property Setter not getting Called)
  • linux ubuntu14.04版没有那个文件或目录
  • 如何使用JSF EL表达式在param中迭代变量(How to iterate over variable in param using JSF EL expression)
  • 是否有可能在WPF中的一个单独的进程中隔离一些控件?(Is it possible to isolate some controls in a separate process in WPF?)
  • 使用Python 2.7的MSI安装的默认安装目录是什么?(What is the default installation directory with an MSI install of Python 2.7?)
  • 寻求多次出现的表达式(Seeking for more than one occurrence of an expression)
  • ckeditor config.protectedSource不适用于editor.insertHtml上的html元素属性(ckeditor config.protectedSource dont work for html element attributes on editor.insertHtml)
  • linux只知道文件名,不知道在哪个目录,怎么找到文件所在目录
  • Actionscript:检查字符串是否包含域或子域(Actionscript: check if string contains domain or subdomain)
  • 将CouchDB与AJAX一起使用是否安全?(Is it safe to use CouchDB with AJAX?)
  • 懒惰地初始化AutoMapper(Lazily initializing AutoMapper)
  • 使用hasclass为多个div与一个按钮问题(using hasclass for multiple divs with one button Problems)
  • Windows Phone 7:检查资源是否存在(Windows Phone 7: Check If Resource Exists)
  • 无法在新线程中从FREContext调用getActivity()?(Can't call getActivity() from FREContext in a new thread?)
  • 在Alpine上升级到postgres96(/ usr / bin / pg_dump:没有这样的文件或目录)(Upgrade to postgres96 on Alpine (/usr/bin/pg_dump: No such file or directory))
  • 如何按部门显示报告(How to display a report by Department wise)
  • Facebook墙贴在需要访问令牌密钥后无法正常工作(Facebook wall post not working after access token key required)
  • Javascript - 如何在不擦除输入的情况下更改标签的innerText(Javascript - how to change innerText of label while not wiping out the input)
  • WooCommerce / WordPress - 不显示具有特定标题的产品(WooCommerce/WordPress - Products with specific titles are not displayed)