这是六则或许对你有些许帮助的信息:

1、阿秀在工作之余开发了一个编程资源网站,目前已经收集了很多不错的学习资源和黑科技(附带下载地址),如你有意欢迎体验以及推荐自己认为不错的资源,众人拾柴火焰高,我为人人,人人为我🔥!

2、👉23年5月份我从字节跳动离职跳槽到某外企期间,为方便自己找工作,增加上岸几率,我自己从0开发了一个互联网中大厂面试真题解析网站,包括两个前端和一个后端。能够定向查看某些公司的某些岗位面试真题,比如我想查一下行业为互联网,公司为字节跳动,考察岗位为后端,考察时间为最近一年之类的面试题有哪些?

网站地址:InterviewGuide大厂面试真题解析网站。点此可以查看该网站的视频介绍:B站视频讲解 如果可以的话求个B站三连,感谢!

3、😊 分享一个学弟发给我的20T网盘资源合集点此白嫖,主要是各类高清影视、电视剧、音乐、副业、纪录片、英语四六级考试、考研考公等资源。

4、😍免费分享阿秀个人学习计算机以来收集到的免费学习资源,点此白嫖;也记录一下自己以前买过的不错的计算机书籍、网络专栏和垃圾付费专栏;也记录一下自己以前买过的不错的计算机书籍、网络专栏和垃圾付费专栏

5、🚀如果你想在校招中顺利拿到更好的offer,阿秀建议你多看看前人踩过的坑留下的经验,事实上你现在遇到的大多数问题你的学长学姐师兄师姐基本都已经遇到过了。

6、🔥 欢迎准备计算机校招的小伙伴加入我的学习圈子,一个人踽踽独行不如一群人报团取暖,圈子里沉淀了很多过去21/22/23届学长学姐的经验和总结,好好跟着走下去的,最后基本都可以拿到不错的offer!此外,每周都会进行精华总结和分享!如果你需要《阿秀的学习笔记》网站中📚︎校招八股文相关知识点的PDF版本的话,可以点此下载

# 303. 区域和检索 - 数组不可变

力扣原题链接(点我直达) (opens new window)

给定一个整数数组 nums,求出数组从索引 ij (ij) 范围内元素的总和,包含 i, j 两点。

示例:

给定 nums = [-2, 0, 3, -5, 2, -1],求和函数为 sumRange()

sumRange(0, 2) -> 1
sumRange(2, 5) -> -1
sumRange(0, 5) -> -3
1
2
3
4
5

# 第一版,自己写的,效果比较差

执行用时 :612 ms, 在所有 cpp 提交中击败了5.09%的用户

内存消耗 :518.1 MB, 在所有 cpp 提交中击败了5.10%的用户

class NumArray {
public:
    NumArray(vector<int>& nums) {
    int n = nums.size();
	// vector<vector<int>> res(n,vector<int>(n,0));
    res.resize(n);
	for (int i = 0; i < n; ++i) {
		res[i].resize(n);
	}
	if (n == 0) return;
    if (n == 1) { res[0][0] = nums[0]; return;}
	for (int i = 0; i < n - 1; ++i) {
		res[i][i] = nums[i];
		for (int j = i; j < n-1; ++j) {
			
			res[i][j+1] = res[i][j] + nums[j + 1];
			//cout << i << " " << j << " ";
			//cout << res[i][j] << " " << nums[i + 1] << " " << res[i][j+1] << endl;
		}
	}

	//cout << "ooo" << endl;
	//res[n - 1][0] = nums[0];
	for (int i = 0; i < n; ++i) {
		res[i][n-1] = res[i][n-2] + nums[n-1];
	}
        
	// for (auto a : res) {
	// 	for (auto b : a) {
	// 		cout << b << " ";
	// 	}
	// 	cout << endl;
	// }
        
        
        
    }
    
    int sumRange(int i, int j) {
        return res[i][j];
    }
    
    vector<vector<int>> res;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

# 第二版,时间还是比较久

执行用时 :220 ms, 在所有 cpp 提交中击败了28.08%的用户

内存消耗 :17.1 MB, 在所有 cpp 提交中击败了80.26%的用户

class NumArray {
public:
	NumArray(vector<int>& nums) {
		res.assign(nums.begin(), nums.end());
			// for (auto a : res)
			// cout << a << " ";

	}

	int sumRange(int i, int j) {
        if(i==j) return res[i];
		return accumulate(i+res.begin(),1+j+res.begin(),0);
	}

	vector<int> res;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# 第三版,真的厉害,速度也上来了

执行用时 :40 ms, 在所有 cpp 提交中击败了75.29%的用户

内存消耗 :17.3 MB, 在所有 cpp 提交中击败了24.84%的用户

class NumArray {
public:
	 NumArray(vector<int>& nums) {
        if(!nums.empty())
        {
            res.push_back(nums[0]);
            for(int i=1;i<nums.size();i++)
                res.push_back(sum[i-1]+nums[i]);
        }
    }
    
    int sumRange(int i, int j) {
        if(i==0) return res[j];
        else return res[j]-res[i-1];
    }
	vector<int> res;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17