微信小程序 判断数组是否有该元素

微信小程序 判断数组是否有该元素,第1张

遍历数组,个一个的对比,比如

var arr=[xxxxxxxxxxxxx]

var aa='你想要找的御祥脊数'镇渗

for(宴森x in arr)

{

if(x==aa)

{alert('有这个数')}

}

挺简单的啊。。 关键在于理解迟宽heap

import java.util.Arrays

public class Du {

public static void main(String[] args) {

int[] testAry1 = {1, 3, 5, 7, 9, 11}

System.out.println("{1, 3, 5, 7, 9, 11} is " + (isHeap(testAry1)? "": "NOT ") + "heap array.")

int[] testAry2 = {1, 3, 5, 7, 13, 9, 11}

System.out.println("枝隐{1, 3, 5, 7, 13, 9, 11} is "猛旦厅 + (isHeap(testAry2)? "":"NOT ") + "heap array.")

}

/**

* Will receive an integer heap array, method will return true or false

* depending on whether the array is believed to be a valid heap or not

*

* @param heap

*0-based integer array

* @return true if integer array is a heap, else false

*/

public static boolean isHeap(int[] heap) {

if(heap == null || heap.length == 0){

return false

}

int[] ary = new int[heap.length]

for(int i = 0i <heap.lengthi++){

ary[i] = heap[i]

}

Arrays.sort(ary)

for(int i = 0i <heap.lengthi++){

if(ary[i] != heap[i]){

return false

}

}

return true

}

}

---------------

{1, 3, 5, 7, 9, 11} is heap array

{1, 3, 5, 7, 13, 9, 11} is NOT heap array


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12311858.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-24
下一篇 2023-05-24

发表评论

登录后才能评论

评论列表(0条)

保存