当前位置:网站首页>力扣刷题日记/day1/2022.6.23
力扣刷题日记/day1/2022.6.23
2022-07-04 16:33:00 【bobo洁厕灵】
新手村
失败是迷雾,穿过它才能看见成功
数据结构的算法中,数组是个重要概念,今天主要学习数组的有关概念。
数组(Array)是由相同类型的元素(element)的集合所组成的数据结构,分配一块连续的内存来存储。利用元素的索引(index)可以计算出该元素对应的存储地址。
简单来说,数组就是由一块连续的内存组成的数据结构。这个概念中有一个关键词“连续”,它反映了数组的一大特点,就是它必须是由一个连续的内存组成的

数组的优点:
数组的“连续”特征决定了它的访问速度很快,因为它是连续存储的,所以这就决定了它的存储位置就是固定的,因此它的访问速度就很快。比如现在有 10 个房间是按照年龄顺序入住的,当我们知道第一房子住的是 20 岁的人之后,那么我们就知道了第二个房子是 21 岁的人,第五个房子是 24 岁的人......等等。
数组的缺点:
1.对内存的要求比较高,必须要找到一块连续的内存才行。
2.插入和删除的效率比较慢,假如我们在数组的非尾部插入或删除一个数据,那么就要移动之后的所有数据,这就会带来一定的性能开销
3.大小固定,不能动态拓展。
数组的创建
arrayList = new ArrayList<Integer>();//创建数组的第一种方法
int[] arr=new int[10]; //通过创建对象的方法来声明一个数组对象//创建数组的第二种方法
int[] x={1,2,3,4,5,6,7,8,9,10}; //通过{}来创建//创建数组的第三种方法。
int[] y= new int[]{1,2,3,4,5};//声明一个对象并将数值填入数组,得到数组对象[1,2,3,4,5]//创建数组的第四种方法
int[] arr = new int[26]//创建一个26个元素的空数组判断数组下标是否越界的方法
public static boolean isLength(int m,int arr[]){
boolean flag=false;
int length = arr.length;
if(m<length)
flag=true;
return flag;
}例题1

solution类中包含了一个runningSum方法
class Solution {
public int[] runningSum(int[] nums) {
int n = nums.length;
for (int i = 1; i < n; i++) {
nums[i] = nums[i] + nums[i - 1];
}
return nums;
}
}java格式中使用.length获得数组的长度
c格式中使用.size()获得数组的长度
例题2


解题思路:
先比较ransomNote和magazine的长度,如果ransomNote的长度大于magazine的长度,返回false
遍历ransomNote中的字符,用count[i]表示26个小写英文字符,并记录字符的出现次数count[i]++,再遍历magazine字符串,记录字符出现次数并减一count[i]--,count[i]++表示在ransomNote中一个字符的出现次数,在magazine中遍历字符串,count[i]--表示在magazine中对应出现过的字符减一,如果count[i]最后大于0,说明在ransomNote中某个字符串出现次数大于在magazine出现的次数,magazine中的字符就不能构成ransomNote
根据提示字符串中的元素只由小写英文字母构成,根据ASCLL表,字符-“a”就为字符对应的数值

class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
int[] count = new int[26];
if(ransomNote.length()>magazine.length()){
return false; //首先判断两者字符串长度
}
for(int i=0;i<ransomNote.length();i++){
count[ransomNote.charAt(i)-'a']++;
}
for(int i=0;i<magazine.length();i++){
count[magazine.charAt(i)-'a']--;
}
for(int i=0;i<26;i++){
if(count[i]>0) return false;
}
return true;
}
}charAt() 方法用于返回指定索引处的字符。索引范围为从 0 到 length() - 1。

边栏推荐
- 超标量处理器设计 姚永斌 第6章 指令解码 摘录
- Why are some online concerts always weird?
- 超标量处理器设计 姚永斌 第7章 寄存器重命名 摘录
- Just today, four experts from HSBC gathered to discuss the problems of bank core system transformation, migration and reconstruction
- Achieve animation effect through event binding
- [cloud native] what is the "grid" of service grid?
- 庆贺!科蓝SUNDB与中创软件完成七大产品的兼容性适配
- 为啥有些线上演唱会总是怪怪的?
- fopen、fread、fwrite、fseek 的文件处理示例
- High school physics: force, object and balance
猜你喜欢

How to test MDM products

一直以为做报表只能用EXCEL和PPT,直到我看到了这套模板(附模板)

TCP两次挥手,你见过吗?那四次握手呢?
![[HCIA continuous update] WAN technology](/img/31/8e9ed888d22b15eda5ddcda9b8869b.png)
[HCIA continuous update] WAN technology

力扣刷题日记/day5/2022.6.27

MVC mode and three-tier architecture

【Hot100】32. Longest valid bracket

78 year old professor Huake impacts the IPO, and Fengnian capital is expected to reap dozens of times the return

超标量处理器设计 姚永斌 第5章 指令集体系 摘录

Is it science or metaphysics to rename a listed company?
随机推荐
Redis主从复制
Lua emmylua annotation details
Interview summary of large factory Daquan II
Superscalar processor design yaoyongbin Chapter 7 register rename excerpt
ISO27001 certification process and 2022 subsidy policy summary
明星开店,退,退,退
I always thought that excel and PPT could only be used for making statements until I saw this set of templates (attached)
如何提高开发质量
Is it science or metaphysics to rename a listed company?
LD_LIBRARY_PATH 环境变量设置
超标量处理器设计 姚永斌 第6章 指令解码 摘录
78 year old professor Huake impacts the IPO, and Fengnian capital is expected to reap dozens of times the return
Tutorial on the use of Huawei cloud modelarts (with detailed illustrations)
Imitation of numpy 2
Introduction of time related knowledge in kernel
Analysis of I2C adapter driver of s5pv210 chip (i2c-s3c2410. C)
【Hot100】32. 最长有效括号
I2C子系统之适配器的设备接口分析(i2c-dev.c文件分析)
uni-app与uviewUI实现仿小米商城app(附源码)
Pytoch deep learning environment construction