zoukankan      html  css  js  c++  java
  • Homework 2

    题目一代码:

    1. public intfindLast(int[] x, inty) {
    2. //Effects: If x==null throw
    3. NullPointerException
    4. // else return the index of the last element
    5. // in x that equals y.
    6. // If no such element exists, return -1
    7. for (inti=x.length-1; i> 0; i--)
    8. {
    9. if (x[i] == y)
    10. {
    11. return i;
    12. }
    13. }
    14. return -1;
    15. }
    16. // test: x=[2, 3, 5]; y = 2
    17. // Expected = 0

    1、第七行i>0,使得数组缺少第一个元素,应该改为i>=0

    2、x = [];

    3、x = [2,3,4] y=3;

    4、x = [1,2,0] y=3;

    题目二代码:

    1. public static intlastZero(int[] x) {
    2. //Effects: if x==null throw
    3. NullPointerException
    4. // else return the index of the LAST 0 in x.
    5. // Return -1 if 0 does not occur in x
    6. for (inti= 0; i< x.length; i++)
    7. {
    8. if (x[i] == 0)
    9. {
    10. return i;
    11. }
    12. } return -1;
    13. }
    14. // test: x=[0, 1, 0]
    15. // Expected = 2

    1、第六行,查找最后一个应该从数组最后一位查找for(int i=x.length-1;i>=0;i--);

    2、x = [];

    3、x = [2,3,4];

    4、x = [1,2,0];

  • 相关阅读:
    2020/12/2
    2020/12/1
    Cannot do a soft reset in the middle of a merge
    webpack img
    rm -fr ".git/rebase-apply"
    css 颜色
    初始化样式
    a标签
    esma 最新
    前端
  • 原文地址:https://www.cnblogs.com/z15349/p/5263744.html
Copyright © 2011-2022 走看看