力扣练习题二

1 对任何数据结构链式存储结构一定优于顺序存储结构()

A 对
B 错

 分析:

顺序表

  • 优点:查找和修改(首先要查找到)效率高,空间占用比链表小,时间复杂度 O(1)
  • 缺点:插入和删除元素时,后面的元素都需要进行移动,编译时确定大小,时间复杂度 O(n)

链表

  • 优点:插入和删除元素比较方便,只需要修改指针,空间大小不必指定,时间复杂度 O(n)
  • 缺点:查询和修改(首先要查找到)效率并不高,而且因为添加了指针等中间数据结构,所以空间占用比顺序表大,时间复杂度 O(1)

2 要表示10个学生的成绩,下列声明并初始化数组正确的是()

A int[] score=new int[ ]
B int score[10]
C int score[]=new int[9]


D int score[]=new int[10]

 分析:数组的初始化方式:数据类型[] 数组名=new 数据类型[长度];  

                                             数据类型[] 数组名=new 数据类型[]{1,2,3,4,5};

                                             数据类型[] 数组名={1,2,3,4,5};

3 已知 10*12 的二维数组 A ,以行序为主序进行存储,每个元素占 1 个存储单元,已知 A[1][1] 的存储地址为 420 ,则 A[5][5] 的存储地址为 ( )

A 470
B 471
C 472
D  473

 分析:A[5][5]与A[1][1]之间有4*12+4=52个元素,而A[1][1]地址为420,那么A[5][5]的地址就为420+52=472

4 给出一个由 n 个数组成的序列 A[1…n] ,要求找出它的最长单调上升子序列,设 m[i] ( 1 ≤ i ≤ n ),表示以 A[i] 结尾的最长单调上升子序列的长度,则 m[1] = 1 , m[i] ( 1 < i ≤ n )为( A)。

A  m[i] = 1 + max{0,m[k](A[k] < A[i],1≤k < i )}
B  m[i] = 1 + m[k] (k = i - 1 && i > 1)
C  m[i] = 1 + max{-1,m[k] (A[k] ≤ A[i],1≤k < i )}
D  m[i] = max{0,m[k](A[k] < A[i],1≤k < i )}

5 C#程序段的结果: int[][] array = new int[3][]{ new int[3]{5,6,2}, new int[5]{6,9,7,8,3}, new int[2]{3,2} }; array[2][2] 返回(D)

A 9
B 6
C 2
D 溢出

 分析:array[2][2]  (数组存在三行数据,但是第三行只有两个。)

6 已知二维数组A[1: 4, 1: 6]采用列序为主序方式存储,每个元素占用4个存储单元,并且A[3,4]的存储地址为1234,元素A[1, 1]的存储地址是(A)

A 1178
B 1190
C 1278
D 1290

 分析:

首先计算a00的位置:a00+(4*4+3)*4=1234  求得:a00=1158

有了a00的位置在求a11位置:a00+(1*4+1)*4=a11 即a11=1178

7 假设要存储一个数据集,数据维持有序,对其的操作只有插入、删除和顺序遍历,综合存储效率和运行速度,下列哪种数据结构是最适合的是?B

A  数组
B  链表
C  哈希表
D  队列

 分析:

数组可以实现顺序遍历但是插入删除操作复杂,平均移动n/2个元素

链表因为存储的地址不连续(逻辑上连续实际上不连续),可以实现顺序遍历

哈希表是随机存储,所以是离散分布,顺序遍历实现不了

队列只可以在队尾插入队头删除,不可以实现中间插入和删除,不满足条件

综上,链表最合适

8 c中,二维数组初始化的方法是:int a[3][3]={{1},{2},{3}};说法是否正确?

A 正确


B 错误

 分析:实际上是int a[3][3]={{1,0,0},{2,0,0},{3,0,0}}所以正确

9 设有一个 10 阶的下三角矩阵 A (包括对角线),按照从上到下、从左到右的顺序存储到连续的 55 个存储单元中,每个数组元素占 1 个字节的存储空间,则 A[5][4] 地址与 A[0][0] 的地址之差为( A)。

A  19
B  28
C  55

 分析:第一行有1个元素

            第二行有2个元素

            第三行有3个元素

            第四行有4个元素

            第五行有5个元素

            第六行有5个元素

(1 +2+3+4+5+5-1)*1=19;

 

10 从一个长度为n的顺序表中删除第i个元素(1≤i≤n)时,需向前移动(A)_个元素。

A  n-i
B  n-i+1
C  n-i-1
D  i

 分析:第i个元素后面有n-i个元素。所以删除第i个元素(1≤i≤n)时,需向前移动n-i个元素。

11 对有序数组{2、11、15、19、30、32、61、72、88、90、96}进行二分查找,则成功找到15需比较(C)次

A  3
B  4
C  2
D  5

 分析:这个数组里面一共有11个元素,第一回查找中间值的下标是6,值是32,第一回查找中间值的下标是2,值是15

12 若用一个大小为 6 的数组来实现循环队列,且当 rear 和 front 的值分别为 0 和 3 。当从队列中删除一个元素,再加入两个元素后, rear 和 front 的值分别为 (B)。

A  1和5
B  2和4
C  4和2
D  5和1

13 Which statement is true for the class java.util.ArrayList?(A)

A  The elements in the collection are ordered.
B  The collection is guaranteed to be immutable.
C  The elements in the collection are guaranteed to be unique.
D  The elements in the collection are accessed using a unique key.
E  The elements in the collections are guaranteed to be synchronized.

 分析:

A.The elements in the collection are ordered.

集合中的元素是排序的

B.The collection is guaranteed to be immutable.

集合不可改变

C.The elements in the collection are guaranteed to be unique.

集合中的元素必须唯一

D.The elements in the collection are accessed using a unique key.

集合中元素的键是唯一的

E.The elements in the collections are guaranteed to be synchronized.

集合中的元素是线程同步的

选A,元素在集合中有序,指的是元素插入过程中记录了元素的插入顺序。

14 在 C 语言中,一维数组的定义方式为:

元素类型 数组名[E];

E 为(  D )。

A 常量表达式
B 整型表达式
C 整型常量或整型表达式
D 整型常量表达式

 分析:E表示的是一维数组的大小所以应该为一个整型常量表达式

15 请问下面哪种方式可以在不改变原来数组的情况下,拷贝出数组 b ,且满足 b!=a 。例如数组 a 为 [1,2,3] 。(D)

A let b=a;
B let b=a.slice();
C let b=a.splice(0,0);
D let b=a.concat();

 分析:lice和concat方法均返回新数组,而splice方法的主要作用就是对原数组进行增删改操作,返回值为截取删除掉的子数组

16 下列数组定义及赋值,错误的是(B)

A int intArray[ ];
B int intArray=new int[3]; intArray[1]=1; intArray[2]=2;
C int a[ ]={1, 2, 3, 4, 5};
D int a[ ] [ ]=new int[2] [ ]; a[0]=new int[3]; a[1]=new int[3];

 分析:

数组的初始化方式:数据类型[] 数组名=new 数据类型[长度];  

                                             数据类型[] 数组名=new 数据类型[]{1,2,3,4,5};

                                             数据类型[] 数组名={1,2,3,4,5};

17 用数组r存储静态链表,结点的next域指向后继,工作指针j指向链中结点,使j沿链移动的操作为(A)

A j=r[j].next
B j=j+1
C j=j->next
D j=r[j]->next

18 在一个有8个int数据的数组中,随机给出数组的数据,找出最大和第二大元素一定需要进行(B)次比较:

A 8
B 9
C 10
D 11

19 将数组 var a=[1,2,3] 变成数组 [4,3,2,1] 下面的方式正确的是?(AC)

A a.reverse().unshift(4)
B a.push(4).reverse()
C a.push(4); a.reverse()
D a.splice(3,1,4).reverse()

 

1、 concat()

  • 连接两个或多个数组
  • 不改变原数组
  • 返回被连接数组的一个副本

2、join()

  • 把数组中所有元素放入一个字符串
  • 不改变原数组
  • 返回字符串

3、 slice()

  • 从已有的数组中返回选定的元素
  • 不改变原数组
  • 返回一个新数组

4、 toString()

  • 把数组转为字符串
  • 不改变原数组
  • 返回数组的字符串形式

改变原数组:

5、 pop()

  • 删除数组最后一个元素,如果数组为空,则不改变数组,返回undefined
  • 改变原数组
  • 返回被删除的元素


6、 push()

  • 向数组末尾添加一个或多个元素
  • 改变原数组
  • 返回新数组的长度

7、 reverse()

  • 颠倒数组中元素的顺序
  • 改变原数组
  • 返回该数组

8、 shift()

  • 把数组的第一个元素删除,若空数组,不进行任何操作,返回undefined
  • 改变原数组
  • 返回第一个元素的值

9、 sort()

  • 对数组元素进行排序(ascii)
  • 改变原数组
  • 返回该数组

10、 splice()

  • 从数组中添加/删除项目
  • 改变原数组
  • 返回被删除的元素

11、 unshift()

  • 向数组的开头添加一个或多个元素
  • 改变原数组
  • 返回新数组的长度

 

20 下列给定程序中,函数fun的功能是:进行数字字符转换。若形参ch中是数字字符'0'~'9',则将'0'转换成'9','1'转换成'8', '2'转换成'7',…, '9'转换成'0';如果是其他字符则保持不变,并将转换后的结果作为函数值返回。
请在程序的下画线处填入正确的内容并将下画线删除,使程序得出正确的结果。(AC)
试题程序。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

#include<stdio.h>

_____ fun(char ch)

{

    if(ch>='0'&& _______)

        return'9'-(ch- ______);

    return ch;

}

main( )

    char c1,c2;

    printf("\nThe result:\n");

    c1='2';

    c2=fun(c1);

    printf("c1=%c c2=%c\n",c1,c2);

    c1='8';

    c2=fun(c1);

    printf("c1=%c c2=%c\n",c1,c2);

    c1='a';

    c2=fun(c1);

    printf("c1=%c c2=%c\n",c1,c2);

}

A  int ch<='9'  '0'
B  char   ch<='9'  '0'
C  char   ch<='9'  0
D  int ch<='9'  0

 分析:可以返回int和char,字符只能和字符相减

题目链接:https://leetcode-cn.com/problems/distance-between-bus-stops/

class Solution {
    public int distanceBetweenBusStops(int[] distance, int start, int destination) {
        int route1=0;
        int route2=0;
        int i=start;
        while(true){
            if(i%distance.length==destination){
                break;
            }else{
                route1+=distance[i%distance.length];
                
            }
            i++;
        }
        System.out.println(route1);
        int j=destination;
        while(true){
            if(j%distance.length==start){
                break;
            }else{
                route2+=distance[j%distance.length];
            }
            j++;
        }
        System.out.println(route2);
        return route1<=route2?route1:route2;
    }
}

 题目链接:https://leetcode-cn.com/problems/duplicate-zeros/

class Solution {
    public void duplicateZeros(int[] arr) {
        for(int i=0;i<arr.length;){
            if(arr[i]==0&&i<=arr.length-2){
                for(int j=arr.length-1;j>i;j--){
                    arr[j]=arr[j-1];
                }
                arr[i+1]=0;
                i+=2;
                continue;
            }
            i++;
        }
    }
}

题目链接:https://leetcode-cn.com/problems/max-consecutive-ones/submissions/

class Solution {
    public int findMaxConsecutiveOnes(int[] nums) {
        int count=0;
        int maxcount=0;
        for(int i=0;i<nums.length;i++){
            if(nums[i]==1){
                count++; 
                if(i==nums.length-1){
                    maxcount=(maxcount>=count?maxcount:count);
                }                  
            }else{
                maxcount=(maxcount>=count?maxcount:count);
                count=0;
                
            }  
                       
        }
        return maxcount;
    }
}

题目链接:https://leetcode-cn.com/problems/image-smoother/submissions/

class Solution {
    public int[][] imageSmoother(int[][] M) {
        int [][]M2=new int[M.length][M[0].length];
        for(int i=0;i<M.length;i++){
            for(int j=0;j<M[0].length;j++){
                M2[i][j]=M[i][j];
            }
        }
        for(int i=0;i<M.length;i++){
            for(int j=0;j<M[i].length;j++){
                int countsum=M[i][j];
                int countnum=1;
                //左边
                if(j>=1){
                    countsum+=M[i][j-1];
                    countnum++;
                }
                //右边
                if(j<M[i].length-1){
                    countsum+=M[i][j+1];
                    countnum++;
                }
                //上边
                if(i>=1){
                    countsum+=M[i-1][j];
                    countnum++;
                }
                //下边
                if(i<M.length-1){
                    countsum+=M[i+1][j];
                    countnum++;
                }
                //左上
                if(j>=1&&i>=1){
                    countsum+=M[i-1][j-1];
                    countnum++;
                }
                //左下
                if(j>=1&&i<M.length-1){
                    countsum+=M[i+1][j-1];
                    countnum++;
                }
                //右上
                if(j<M[i].length-1&&i>=1){
                    countsum+=M[i-1][j+1];
                    countnum++;
                }
                //右下
                if(j<M[i].length-1&&i<M.length-1){
                    countsum+=M[i+1][j+1];
                    countnum++;
                }
                M2[i][j]=countsum/countnum;
                
            }
            
        }
        return M2;
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值