从阵列中找到领导者(Find the leader from an Array)
领导者是一个数组,它比数组元素的半角出现更多。 如果数组中的两个连续部分具有THAT领导者(即,领导者出现在
respective
部分中的其他元素的一半以上),则等同领导者将被定义。 由于该部分可以变化,该阵列可以有多个Equi-Leader。 说,我找到了一个阵列的领导者x,现在我想找到一些领导者。如果一个部分和连续部分x出现的次数多于其他元素(该部分的x> non_X),是否意味着在这两个部分x中出现了超过一半的其他元素? 请记住,
x_Total > Array_Length/2
一个部分是一个子阵列,也就是说,将这个元素放在S的索引中。这样就创建了2个连续的[0,1,.....,S]和[S + 1,S + 2,...的子阵列。 。N-1],我们需要两个部门的领导。
我知道这是事实,但是,如何证明这一点? 不寻找代码实现。
A leader is a number that occurs MORE than HALF of the elements of an array. The Equi-leader will be defined if two consecutive section in the array has THAT leader (ie the leader occurs more than half of other elements in the
respective
section). As the section can vary, the array can have more than one Equi-leader. Say, I find the leader x of an array and now I would like to find the number of equi-leaders.If a section and the consecutive section x occurs more than other elements (x > non_X for the segment), does it mean that in those 2 segments x occurred more than half of other elements as well? Remember,
x_Total > Array_Length/2
A section is a subarray, say, take the element in the index of S. That creates 2 consecutive subarrays of [0,1,....., S] and [S+1, S+2, ..... N-1] and we need leaders both of the sections.
I know this is true, but, how to prove that? Not looking for code implementation.
原文:https://stackoverflow.com/questions/50888814
最满意答案
有很多方法可以解决这个问题,这实际上取决于你如何进行导航。 如果您正在使用实际链接或使用
router.navigate()
更改路由,您的router
将调度route:<route name>
事件,您可以侦听,将相同的参数传递给处理程序,因为它传递给路由功能。结果证明用Backbone设置测试用例代码需要多长时间,我让你成为一个jsFiddle来说明这种方法: http : //jsfiddle.net/nrabinowitz/ZrgJF/7/
很多这只是设置代码; 这个问题的重要部分是路由器:
var MyRouter = Backbone.Router.extend({ routes: { 'view/:id' : 'openView' }, openView: function(id) { app.openView(id) } }); router = new MyRouter();
并且视图将删除绑定到路由:
var MyView = Backbone.View.extend({ initialize: function(opts) { this.id = opts.id; router.bind('route:openView', this.dispose, this); }, // id is the same as the route argument dispose: function(id) { if (id != this.id) { this.remove(); } } // etc });
There are lots of ways to approach this, and it really depends on how you're doing the navigation. If you're changing routes with actual links, or by using
router.navigate()
, yourrouter
will dispatch aroute:<route name>
event that you can listen to, passing the same arguments to the handler as it passes to the route function.In what turned out to be a demonstration of just how long it takes to set up test case code with Backbone, I made you a jsFiddle to illustrate this approach: http://jsfiddle.net/nrabinowitz/ZrgJF/7/
A lot of this is just setup code; the important parts for this question are the router:
var MyRouter = Backbone.Router.extend({ routes: { 'view/:id' : 'openView' }, openView: function(id) { app.openView(id) } }); router = new MyRouter();
And the view, which binds removal to the route:
var MyView = Backbone.View.extend({ initialize: function(opts) { this.id = opts.id; router.bind('route:openView', this.dispose, this); }, // id is the same as the route argument dispose: function(id) { if (id != this.id) { this.remove(); } } // etc });
相关问答
更多-
TCP/IP模型是一个________。[2023-10-02]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
你可以实现一个自己的功能,扩展骨干路径,以你想要的方式工作。 如果您阅读路由源代码,您将看到它们只转换您的regexp模式的模式,非常简单,然后使用regexp匹配的参数应用方法目标。 因此,如果您想要一个反向路线进行导航或类似的东西,您可以应用自我扩展的Backbone.Route 像这样的东西: myrouter = Backbone.Router.extends({ navigate: function(a, b) { // Do something with my args ...
-
这就是我怀疑问题所在。 您的Departments集合仅实例化一次, app.Departments = new DepartmentList(); 。 然后在DepartmentsView init函数中,分配this.collection = app.Departments; 这意味着第二次路由到DepartmentsView ,您将其集合分配给已有模型的现有集合。 当您调用fetch() ,Backbone会检测到没有新模型(因为您已经在集合中有模型),因此不会触发添加事件。 您可以做的一件事是使用r ...
-
导航回视图时是否有用于处理视图模型的良好模式?(Are there good patterns for disposing view models when navigating back to a view?)[2022-01-28]
您可以创建一个侦听Unloaded事件的Blend Behavior或附加属性,然后在对象的DataContext上调用Dispose()如果它是IDisposable 。 然后可以根据需要将其附加到您的视图中。 You could make a Blend Behavior or attached property that listens for the Unloaded event, and then calls Dispose() on the DataContext of the object i ... -
您可以使用backbone.js 路由器组件 ,并在特定URL上分配视图的执行。 这可能是最优雅的方式。 You can use backbone.js router component, and assign execution of views on specific urls. It is probably the most elegant way.
-
Backbone:如何在导航到路线时处置视图/模型?(Backbone: How to dispose a view/model when navigating to a route?)[2023-02-10]
有很多方法可以解决这个问题,这实际上取决于你如何进行导航。 如果您正在使用实际链接或使用router.navigate()更改路由,您的router将调度route:事件,您可以侦听,将相同的参数传递给处理程序,因为它传递给路由功能。 结果证明用Backbone设置测试用例代码需要多长时间,我让你成为一个jsFiddle来说明这种方法: http : //jsfiddle.net/nrabinowitz/ZrgJF/7/ 很多这只是设置代码; 这个问题的重要部分是路由器: var M ... -
这是因为你正在更改Preloader(导航道具)收到的道具,所以你将输入它的“ComponentWillRecieveProps”,它将触发登录导航,你可以用以下方法修复: componentWillReceiveProps(nextProps) { if(this.props.auth.localTokenStatus!=nextProps.auth.localTokenStatus){ console.log("NXT props - PL", ne ...
-
如果我理解正确,应永久显示文件夹列表。 您的应用程序有两个大视图,文件夹列表和内容。 并且必须始终显示文件夹列表。 var AppRouter = Backbone.Router.extend({ // it's better to use REST names than custom ones routes: { '': 'index', 'get/:id/:name': 'show' }, initialize: function(options) { this ...
-
AngularJs - 在导航到新路线时如何清理控制器(AngularJs - how to clean up controllers when navigating to a new route)[2023-08-31]
您可以收听$destroy事件,并执行一些清理/拆除: module.controller("SomeController", function($scope) { $scope.$on("$destroy", function() { // clean up here }); }); 资源: $on : https : //docs.angularjs.org/api/ng/type/$rootScope.Scope#$on $destroy : https : //d ...