作业1:数组与字符串学习小结

一、知识点小结:

1.集合、列表和数组

集合:由一个或多个确定的元素所构成的整体。特点:无序,集合内元素类型不一定相同。

列表:一种数据项构成的有限序列。特点:有序,长度可变。一般占据连续的储存空间储存,而以链表的形式则为非连续储存。常见表现形式:数组、列表、栈、队列。

数组:列表的表现形式之一。特点:有序,长度可变。与列表不同的地方在于数组有索引。

2.数组元素的查询:
(1)根据数组的索引查找元素:
直接查询对应索引的元素的值。
(2)根据数组元素的值查找元素:
从数组索引为0的元素开始,依次对比数组元素的值和目标值;

 

二、练习题:

程序题1: 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。你可以假设数组中无重复元素。

测试用例:[1,3,5,6],5

核心代码及其功能: 在排序数组中找到目标值并返回其索引,若目标值不存在于数值中,返回它将被插入的位置

public class Solution {

    public int SearchInsert(int[] nums, int target) {

        int i;

        for(i=0;i<nums.Length;i++)

        {

            if(target<=nums[i])

                return i;

        }

        return i++;

    }

}

结果截图:

 

 

 

 

程序题2:编写一种算法,若M × N矩阵中某个元素为0,则将其所在的行与列清零。

测试用例:[[0,1,2,0],[3,4,5,2],[1,3,1,5]]

核心代码及其功能:若M × N矩阵中某个元素为0,则将其所在的行与列清零

public class Solution

    {

        public void SetZeroes(int[][] matrix)

        {

            bool Firstrow = false;

            bool Firstcol = false;

            int onerow, onecol, i, j;

            //确定第一行是否有0

            for (onerow = 0; onerow < matrix[0].GetLength(0); onerow++)

            {

                if (matrix[0][onerow] == 0) Firstrow = true;

            }

            //确定第一列是否有0

            for (onecol = 0; onecol < matrix.GetLength(0); onecol++)

            {

                if (matrix[onecol][0] == 0) Firstcol = true;

            }

            //遍历数组,

            for (i = 0; i < matrix.GetLength(0); i++)

            {

                for (j = 0; j < matrix[0].GetLength(0); j++)

                {

                    if (matrix[i][j] == 0)

                    {

                        matrix[i][0] = 0;

                        matrix[0][j] = 0;

                    }

                }

            }

            for (onecol = 1; onecol < matrix[0].GetLength(0); onecol++)

            {

                if (matrix[0][onecol] == 0)

                {

                    for (i = 1; i < matrix.GetLength(0); i++)

                        matrix[i][onecol] = 0;

                }

            }

            for (onerow = 1; onerow < matrix.GetLength(0); onerow++)

            {

                if (matrix[onerow][0] == 0)

                {

                    for (j = 1; j < matrix[0].GetLength(0); j++)

                        matrix[onerow][j] = 0;

                }

            }

            if (Firstrow)

            {

                for (j = 0; j < matrix[0].GetLength(0); j++)

                {

                    matrix[0][j] = 0;

                }

            }

            if (Firstcol)

            {

                for (i = 0; i < matrix.GetLength(0); i++)

                {

                    matrix[i][0] = 0;

                }

            }

        }

    }

结果截图:

 

 

 

 

 

程序题3:给你一个整数数组 nums,请编写一个能够返回数组 “中心下标” 的方法。

数组 中心下标 是数组的一个下标,其左侧所有元素相加的和等于右侧所有元素相加的和。

如果数组不存在中心下标,返回 -1 。如果数组有多个中心下标,应该返回最靠近左边的那一个。注意:中心下标可能出现在数组的两端。

测试用例:[1,7,3,6,5,6]

核心代码及其功能:寻找数组中兴下标

public class Solution {

    public int PivotIndex(int[] nums) {

        for(int i=0;i<nums.Length;i++)

        {

            if(Left(nums,i)==Right(nums,i))

            {

                return i;

            }

        }

        return -1;

    }

    public int Left(int[] nums,int index)

    {

        int sum=0;

        for(int i=0;i<index;i++)

        {

            sum+=nums[i];

        }

        return sum;

    }

    public int Right(int[] nums,int index)

    {

        int sum=0;

        for(int i=index+1;i<nums.Length;i++)

        {

            sum+=nums[i];

        }

        return sum;

    }

}

结果截图:

 

 

三、 学习小结:

  1. 增强了对数组的理解,对Array类的函数有了进一步的熟悉,比如GetLength()是用于获取数组维度的长度的,运用在交错数组中时,获取列的方法为Array[0].GetLength(0),而不像二维数组可以直接Array.GetLength(1)。
  2. 大大加强了自己时刻牢记写好备注的认识,在第二个程序题中,由于出现了逻辑错误导致程序结果出错,浪费了很多时间检查,最后发现读取行数的循环写成了列数。

 

posted @ 2021-07-05 22:27  殷慧001  阅读(97)  评论(0)    收藏  举报