当前位置:网站首页>力扣刷题日记/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。
边栏推荐
猜你喜欢
机器学习概念漂移检测方法(Aporia)
【Hot100】32. 最长有效括号
[cloud native] what is the "grid" of service grid?
With an estimated value of 90billion, the IPO of super chip is coming
同事悄悄告诉我,飞书通知还能这样玩
力扣刷题日记/day7/6.30
"In Vietnam, money is like lying on the street"
一直以为做报表只能用EXCEL和PPT,直到我看到了这套模板(附模板)
Load test practice of pingcode performance test
“在越南,钱就像躺在街上”
随机推荐
Machine learning concept drift detection method (Apria)
中断的顶半部和底半部介绍以及实现方式(tasklet 和 工作队列)
股价大跌、市值缩水,奈雪推出虚拟股票,深陷擦边球争议
输入的查询SQL语句,是如何执行的?
【系统盘转回U盘】记录系统盘转回U盘的操作
高中物理:力、物体和平衡
Mysql5.7 installation tutorial graphic explanation
7 RSA Cryptosystem
uni-app与uviewUI实现仿小米商城app(附源码)
Unity makes revolving door, sliding door, cabinet door drawer, click the effect of automatic door opening and closing, and automatically play the sound effect (with editor extension code)
[proteus simulation] printf debugging output example based on VSM serial port
SIGMOD’22 HiEngine论文解读
Lua EmmyLua 注解详解
怎么开户才是安全的,
Is it safe to download the mobile version of Anxin securities and open an account online
[system analyst's road] Chapter 7 double disk system design (structured development method)
Win32 API 访问路由的加密网页
估值900亿,超级芯片IPO来了
I2C子系统之适配器的设备接口分析(i2c-dev.c文件分析)
MVC mode and three-tier architecture