如果给定的数组是单调序列,我想返回true。这是我到目前为止的地方,但这个功能并没有按预期工作:
Public Class Foo
Public Class SampleRating
Enum SampleRateSetting As Byte
SR0 = 1
SR1 = 2
SR2 = 4
SR3 = 8
End Enum
Public Setting As SampleRateSetting
Public Noise As UInt16
Public Bandwidth As UInt16
Public SampleRate As UInt32
Public BinWidth As Double
Public Shared Function FindSetting(Setting As SampleRateSetting) As Predicate(Of SampleRating)
Return Function(Rating As SampleRating) Rating.Setting = Setting
End Function
End Class
Public Shared SampleRatings As New List(Of SampleRating) From {
New SampleRating With {.Setting = SampleRating.SampleRateSetting.SR0, .SampleRate = 100189, .BinWidth = 196, .Bandwidth = 26000, .Noise = 467},
New SampleRating With {.Setting = SampleRating.SampleRateSetting.SR1, .SampleRate = 12524, .BinWidth = 25, .Bandwidth = 6262, .Noise = 260},
New SampleRating With {.Setting = SampleRating.SampleRateSetting.SR2, .SampleRate = 1566, .BinWidth = 3.1, .Bandwidth = 783, .Noise = 100},
New SampleRating With {.Setting = SampleRating.SampleRateSetting.SR3, .SampleRate = 196, .BinWidth = 0.38, .Bandwidth = 98, .Noise = 38}
}
' Have also tried the below instead, with no luck
Public Shared SampleRatings As New ReadOnlyCollection(Of SampleRating)(New List(Of SampleRating)() From {
New SampleRating With {.Setting = SampleRating.SampleRateSetting.SR0, .SampleRate = 100189, .BinWidth = 196, .Bandwidth = 26000, .Noise = 467},
New SampleRating With {.Setting = SampleRating.SampleRateSetting.SR1, .SampleRate = 12524, .BinWidth = 25, .Bandwidth = 6262, .Noise = 260},
New SampleRating With {.Setting = SampleRating.SampleRateSetting.SR2, .SampleRate = 1566, .BinWidth = 3.1, .Bandwidth = 783, .Noise = 100},
New SampleRating With {.Setting = SampleRating.SampleRateSetting.SR3, .SampleRate = 196, .BinWidth = 0.38, .Bandwidth = 98, .Noise = 38}
})
Function Shared Bar() As Boolean
' Should NOT be able to change the list or list item members here.
End Function
End Class 'End of Foo
http://jsfiddle.net/marcusdei/t0L1wryy/6/
这不是作业,我想为我的职业生涯学习JS。谢谢!
答案 0 :(得分:2)
您可以像$("label[for='record_type']").each(function(){
var parent = $(this).parent();
var clone = $(this).clone();
$(parent).replaceWith(clone);
});
一样使用{只需将every
替换为[1, 2, 3, 4]
)
arr
您需要调整>取决于您是否要检查单调减少/增加。
答案 1 :(得分:0)
我只需要该功能(ES6)的高性能实现。 这是我想出的:
const is_monotonically_decreasing = (array) => {
const n = array.length
let i = 1
while (i < n && array[i] - array[i - 1] < 0) {
i++
}
return i === n
}
这将保存第一次迭代并使用回调(较小的堆栈,较少的内存)。
可以通过相应地调整array[i] - array[i - 1] < 0
来更改行为。
答案 2 :(得分:0)
JavaScript 单行解决方案:
var isMonotonic = (arr) => {
return (
arr.join`` == [...arr].sort((a, b) => a - b).join`` ||
arr.join`` == [...arr].sort((a, b) => b - a).join``
);
};
console.log(isMonotonic([8, 4, 2, 1]));
console.log(isMonotonic([8, 0, 5, 1]));
console.log(isMonotonic([7, 7, 7]));
var isMonotonic = (arr) => {
return (
arr.every((v, i) => i === 0 || v <= arr[i - 1]) ||
arr.every((v, i) => i === 0 || v >= arr[i - 1])
);
};
console.log(isMonotonic([8, 4, 2, 1]));
console.log(isMonotonic([8, 0, 5, 1]));
console.log(isMonotonic([7, 7, 7]));