编程基础 —— 数组

目录

1 数组的定义

2 数组的存储

(1)n维数组的定义

(2)数组存储的特点

3 练习

a 两数之和

b 三数之和

1 数组的定义

数组是有一定顺序关系的若干对象组成的集合,组成数组的对象称为数组元素。

例如 向量对应一维数组;矩阵对应二维数组

2 数组的存储

(1)n维数组的定义

下标由n个数组成的数组称为n维数组

// 一维数组(线)
int[] a = new int[10];

// 二维数组(面)
int[ , ] a = new int[2, 3];

// 三维数组(体)
int[ , , ] a = new int[2, 4, 3];

(2)数组存储的特点

  • 数组元素在内存中按照顺序连续存储
  • 数组的存储分配按行或列进行
  • 数组名表示该数组的首地址,是常量

3 练习

a 两数之和

给定一个整数数组nums和一个整数目标值target,请你在该数组中找出和为目标值target的那两个整数,并返回它们的数组下标。

可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现;可以按任意顺序返回答案。

示例1:

输入:num = [2,3,4,2,1], target = 6;
输出:[2, 5]

示例2:

输入:num = [2,3,1], target = 4;
输出:[1, 2]


public class Solution
{
    public int[] TwoSum(int[] nums, int target)
    {
        int[] result = new int[2];
        for (int i = 0; i < num.length - 1; int target)
            for (int j = i + 1; j < num.length; j++)
                {
                    if (num[i] + num[j] = target)
                        {
                            result[0] = i;
                            result[1] = j;
                            return result;
                         }
                }
                return result;
}
}

b 三数之和

一个长度为n的整数数组nums,和一个目标值target。

请从nums 中选出三个整数,使它们的和与target最接近;返回这三个数的和;假定每组输入只存在恰好一个解。

示例:

输入:num = [3,4,2,1], target = 6;
输出:7


public class Solution
{
    public int TreeSumClosest(int[] nums, int target)
    {
        double error = int.MaxValue;
        int sum = 0;
        for (int i = 0; i < num.length - 2; i++)
            for (int j = i + 1; j < num.length - 1; j++)
                for (int k = j + 1; k < num.length; k++)
                {
                    if (Math.Abs(num[i] + num[j] +num[k] - target) < error
                        {
                            sum = num[i] + num[j] +num[k];
                            error = Math.Abs(sum = target);
                         }
                 }
          return result;
     }
}

降低其时间复杂度 —— 排序

public class Solution
{
    public int TreeSumClosest(int[] nums, int target)
    {
        nums = nums.OrderBy(a => a).ToArray();
        int result = nums[0] + nums[1] + nums[2];
        for (int i = 0; i < num.length - 2; i++)
        {
            int start = i + 1, end = num.length - 1;
            while(strat < end)

            {
                int sum = nums[start] + nums[end] + nums[i]; 
                if (Math.Abs(target- sum) < Math.Abs(target- result))
                       result = sum;
                if (sum > target)
                    end--;
                else if (sum < target)
                    start++;
                else
                    return result;                                                                                                                    
             }
         }
          return result;
     }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

OR_0295

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值