数组的旋转是指将数组假设为圆形数组,每次旋转时将数组的元素向左或向右旋转一个索引,一端的元素可以采用另一端的值。递增数组意味着每个元素将大于或等于其前一个元素,递减数组意味着每个元素将小于或等于前一个元素。
在这个问题中,我们给定一个数组,我们可以向左或向右旋转数组,我们必须找出在一定的旋转(可能为零)之后是否可以使数组增加或减少。 p>
天真的方法
在这种方法中,我们将旋转数组,并且对于每次旋转,我们将检查当前数组是增加还是减少。
示例
在下面的示例中,我们检查是否可以通过旋转给定数组来增加或减少它。以下是输入和预期输出。
输入:arr = [3, 4, 5, 6, 1, 2]
预期输出:是
输入:arr = [ 5, 1, 6, 2, 5, 3 ]
预期输出:否
示例
// function to rotate the given array
function rotate(arr){
var l = 0;
var r = arr.length-1;
while(l < r){
arr[l] += arr[r];
arr[r] = arr[l]-arr[r];
arr[l] = arr[l]-arr[r];
l++;
}
return arr;
}
// function to check if the given array is increasing or not
function increasing(arr){
// getting the size of array
var len = arr.length
// traversing over the array
for(var i = 1; i < len; i++){
if(arr[i] < arr[i-1]){
return false;
}
}
return true;
}
// function to check if the given array is decreasing or not
function decreasing(arr){
// getting the size of array
var len = arr.length
// traversing over the array
for(var i = 1; i < len; i++){
if(arr[i] > arr[i-1]){
return false;
}
}
return true;
}
// function to check whether the given array can become
// increasing or decreasing after certain rotations
function check(arr){
var k = arr.length
while(k--){
if(increasing(arr) || decreasing(arr)){
return true;
}
arr = rotate(arr);
}
return false;
}
// defining the arr's
var arr1 = [3, 4, 5, 6, 1, 2]
var arr2 = [5, 1, 6, 2, 5, 3]
console.log("The given array is: ");
console.log(arr1)
if(check(arr1) == true){
console.log("Yes, after some rotations given array can be transformed into an increasing or decreasing array");
}
else{
console.log("No, after some rotations given array cannot be transformed into an increasing or decreasing array");
}
console.log("The given array is: ");
console.log(arr2)
if(check(arr2) == true){
console.log("Yes, after some rotations given array can be transformed into an increasing or decreasing array");
}
else{
console.log("No, after some rotations given array cannot be transformed into an increasing or decreasing array");
}
输出
The given array is: [ 3, 4, 5, 6, 1, 2 ] Yes, after some rotations given array can be transformed into an increasing or decreasing array The given array is: [ 5, 1, 6, 2, 5, 3 ] No, after some rotations given array cannot be transformed into an increasing or decreasing array
上述代码的时间复杂度为O(N*N),空间复杂度为O(1)。
高效的方法
在前面的数组中,我们检查了每次旋转数组是否增加或减少,在这种方法中,我们将部分检查增加或减少的数组。
示例
// function to check if the given array is increasing or not
function increasing(arr){
// getting the size of array
var len = arr.length
// traversing over the array
var i = 0;
for(var i = 1; i < len; i++){
if(arr[i] < arr[i-1]){
break;
}
}
if(i == len) return true;
i++;
for(; i< len; i++){
if(arr[i] < arr[i-1]){
return false;
}
}
return arr[len-1] <= arr[0];
}
// function to check if the given array is decreasing or not
function decreasing(arr){
// getting the size of array
var len = arr.length
// traversing over the array
var i = 0;
for(var i = 1; i < len; i++){
if(arr[i] > arr[i-1]){
break;
}
}
if(i == len) return true;
i++;
for(; i< len; i++){
if(arr[i] > arr[i-1]){
return false;
}
}
return arr[len-1] >= arr[0];
}
// function to check whether the given array can become increasing or decreasing after certain rotations
function check(arr){
if(increasing(arr) || decreasing(arr)){
return true;
}
else{
return false;
}
}
// defining the arr's
var arr1 = [3, 4, 7, 6, 1, 2]
var arr2 = [5, 1, 6, 2, 5, 3]
console.log("The given array is: ");
console.log(arr1)
if(check(arr1) == true){
console.log("Yes, after some rotations given array can be transformed into an increasing or decreasing array");
}
else{
console.log("No, after some rotations given array cannot be transformed into an increasing or decreasing array");
}
console.log("The given array is: ");
console.log(arr2)
if(check(arr2) == true){
console.log("Yes, after some rotations given array can be transformed into an increasing or decreasing array");
}
else{
console.log("No, after some rotations given array cannot be transformed into an increasing or decreasing array");
}
输出
The given array is: [ 3, 4, 7, 6, 1, 2 ] No, after some rotations given array cannot be transformed into an increasing or decreasing array The given array is: [ 5, 1, 6, 2, 5, 3 ] No, after some rotations given array cannot be transformed into an increasing or decreasing array
上述代码的时间复杂度为O(N),空间复杂度为O(1)。
结论
在本教程中,我们实现了一个 JavaScript 程序,用于检查是否可以通过旋转给定数组来增加或减少它。我们实现了两种时间复杂度为 O(N*N) 和 O(N) 的方法,并且空间复杂度均为 O(1)。
以上就是JavaScript 程序检查是否可以通过旋转数组来增加或减少数组的详细内容,更多请关注双恒网络其它相关文章!
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
8. 精力有限,不少源码未能详细测试(解密),不能分辨部分源码是病毒还是误报,所以没有进行任何修改,大家使用前请进行甄别
9.本站默认解压密码为:www.sudo1.com
本站提供的一切软件、教程和内容信息仅限用于学习和研究目的。
不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。
本站信息来自网络收集整理,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑或手机中彻底删除上述内容。
如果您喜欢该程序和内容,请支持正版,购买注册,得到更好的正版服务。
我们非常重视版权问题,如有侵权请邮件与我们联系处理。敬请谅解!
云资源网 » JavaScript 程序检查是否可以通过旋转数组来增加或减少数组
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
8. 精力有限,不少源码未能详细测试(解密),不能分辨部分源码是病毒还是误报,所以没有进行任何修改,大家使用前请进行甄别
9.本站默认解压密码为:www.sudo1.com
本站提供的一切软件、教程和内容信息仅限用于学习和研究目的。
不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。
本站信息来自网络收集整理,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑或手机中彻底删除上述内容。
如果您喜欢该程序和内容,请支持正版,购买注册,得到更好的正版服务。
我们非常重视版权问题,如有侵权请邮件与我们联系处理。敬请谅解!
云资源网 » JavaScript 程序检查是否可以通过旋转数组来增加或减少数组
常见问题FAQ
- 免费下载或者VIP会员专享资源能否直接商用?
- 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
- 提示下载完但解压或打开不了?
- 你们有qq群吗怎么加入?