当前位置:网站首页>力扣刷题日记/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。
边栏推荐
- Initial experience of domestic database tidb: simple and easy to use, quick to start
- Once the "king of color TV", he sold pork before delisting
- 内核中时间相关的知识介绍
- Superscalar processor design yaoyongbin Chapter 5 instruction set excerpt
- 【系统盘转回U盘】记录系统盘转回U盘的操作
- ISO27001 certification process and 2022 subsidy policy summary
- You should know something about ci/cd
- 项目通用环境使用说明
- SIGMOD’22 HiEngine论文解读
- Reptile elementary learning
猜你喜欢
Five thousand words to clarify team self-organization construction | Liga wonderful talk
Weima, which is going to be listed, still can't give Baidu confidence
估值900亿,超级芯片IPO来了
Unity 制作旋转门 推拉门 柜门 抽屉 点击自动开门效果 开关门自动播放音效 (附带编辑器扩展代码)
ISO27001 certification process and 2022 subsidy policy summary
Mysql5.7 installation tutorial graphic explanation
[test development] software testing - Basics
【Hot100】32. Longest valid bracket
uni-app与uviewUI实现仿小米商城app(附源码)
Superscalar processor design yaoyongbin Chapter 5 instruction set excerpt
随机推荐
【209】go语言的学习思想
Open source PostgreSQL extension age for graph database was announced as the top-level project of Apache Software Foundation
Face_ Attendance statistics of recognition face recognition
【系统盘转回U盘】记录系统盘转回U盘的操作
【系统分析师之路】第七章 复盘系统设计(结构化开发方法)
Lua emmylua annotation details
Neglected problem: test environment configuration management
超标量处理器设计 姚永斌 第7章 寄存器重命名 摘录
【Hot100】32. 最长有效括号
Is it safe to open an account online? is that true?
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)
如何提高开发质量
High school physics: force, object and balance
I wrote a learning and practice tutorial for beginners!
[system disk back to U disk] record the operation of system disk back to U disk
线上MySQL的自增id用尽怎么办?
力扣刷题日记/day6/6.28
Flask lightweight web framework
LD_ LIBRARY_ Path environment variable setting
Detailed explanation of the maturity classification of ITSS operation and maintenance capability | one article clarifies the ITSS certificate