当前位置:网站首页>Li Kou brush question diary /day1/2022.6.23
Li Kou brush question diary /day1/2022.6.23
2022-07-04 18:35:00 【Bobo toilet cleaning spirit】
Novice Village
Failure is a fog , Through it, you can see success
Data structure algorithm , Array is an important concept , Today, I mainly study the concepts of arrays .
Array (Array) Is made up of elements of the same type (element) Is a collection of data structures composed of , Allocate a contiguous block of memory for storage . Use the index of the element (index) The storage address for this element can be calculated .
Simply speaking , An array consists of a piece continuity Data structure composed of memory . There is a keyword in this concept “ continuity ”, It reflects a major feature of arrays , It must be composed of a continuous memory

The advantages of arrays :
Array of “ continuity ” Its characteristics determine its fast access speed , Because it is continuously stored , So this determines that its storage location is fixed , So its access speed is very fast . For example, there is now 10 Rooms are booked in order of age , When we know that the first house is 20 After years old , Then we know that the second house is 21 A man of years old , The fifth house is 24 A man of years old ...... wait .
Disadvantages of arrays :
1. The memory requirements are relatively high , You must find a contiguous piece of memory .
2. Insertion and deletion are slow , Suppose we insert or delete a data in the non tail of the array , Then you have to move all the data after , This brings some performance overhead
3. Fixed size , Can't expand dynamically .
Array creation
arrayList = new ArrayList<Integer>();// The first way to create an array
int[] arr=new int[10]; // Declare an array object by creating the method of the object // The second way to create an array
int[] x={1,2,3,4,5,6,7,8,9,10}; // adopt {} To create // The third way to create an array .
int[] y= new int[]{1,2,3,4,5};// Declare an object and fill the array with values , Get array object [1,2,3,4,5]// The fourth way to create an array
int[] arr = new int[26]// Create a 26 An empty array of elements How to judge whether the array subscript is out of bounds
public static boolean isLength(int m,int arr[]){
boolean flag=false;
int length = arr.length;
if(m<length)
flag=true;
return flag;
} Example 1

solution Class contains a runningSum Method
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 Format .length Get the length of the array
c Format .size() Get the length of the array
Example 2


Their thinking :
Compare first ransomNote and magazine The length of , If ransomNote Is longer than magazine The length of , return false
Traverse ransomNote The characters in , use count[i] Express 26 Lower case English characters , And record the number of occurrences of characters count[i]++, Re traversal magazine character string , Record the number of character occurrences and subtract one count[i]--,count[i]++ It means that ransomNote The number of occurrences of a character in , stay magazine Traversal string in ,count[i]-- It means that magazine Subtract one from the corresponding characters in , If count[i] Last greater than 0, Description in ransomNote The number of occurrences of a string in is greater than that in magazine Number of occurrences ,magazine Characters in cannot form ransomNote
According to the prompt, the elements in the string are only composed of lowercase English letters , according to ASCLL surface , character -“a” It is the numerical value corresponding to the character

class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
int[] count = new int[26];
if(ransomNote.length()>magazine.length()){
return false; // First, judge the length of the two strings
}
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() Method to return the characters at the specified index . Index range is from 0 To length() - 1.

边栏推荐
- 一、C语言入门基础
- 创业两年,一家小VC的自我反思
- Self reflection of a small VC after two years of entrepreneurship
- 提升复杂场景三维重建精度 | 基于PaddleSeg分割无人机遥感影像
- Once the "king of color TV", he sold pork before delisting
- Performance test of Gatling
- Pytoch deep learning environment construction
- Android uses sqliteopenhelper to flash back
- 使用3DMAX制作一枚手雷
- Lua EmmyLua 注解详解
猜你喜欢

【2022年江西省研究生数学建模】冰壶运动 思路分析及代码实现

2022 national CMMI certification subsidy policy | Changxu consulting
![[go language question brushing chapter] go conclusion chapter | introduction to functions, structures, interfaces, and errors](/img/7a/16b481753d7d57f50dc8787eec8a1a.png)
[go language question brushing chapter] go conclusion chapter | introduction to functions, structures, interfaces, and errors

MySQL common add, delete, modify and query operations (crud)

ISO27001 certification process and 2022 subsidy policy summary

Mysql5.7 installation tutorial graphic explanation

Numpy 的仿制 2

上市公司改名,科学还是玄学?

输入的查询SQL语句,是如何执行的?

要上市的威马,依然给不了百度信心
随机推荐
Crawler (6) - Web page data parsing (2) | the use of beautifulsoup4 in Crawlers
SIGMOD’22 HiEngine论文解读
【Hot100】31. 下一个排列
力扣刷题日记/day2/2022.6.24
Redis master-slave replication
How to improve development quality
celebrate! Kelan sundb and Zhongchuang software complete the compatibility adaptation of seven products
I always thought that excel and PPT could only be used for making statements until I saw this set of templates (attached)
MySQL常用增删改查操作(CRUD)
What types of Thawte wildcard SSL certificates provide
五千字讲清楚团队自组织建设 | Liga 妙谈
[daily question] 556 Next bigger element III
创业两年,一家小VC的自我反思
Li Kou brush question diary /day7/2022.6.29
Heartless sword Chinese translation of Elizabeth Bishop's a skill
Li Kou brush question diary /day6/6.28
NBA赛事直播超清画质背后:阿里云视频云「窄带高清2.0」技术深度解读
【210】PHP 定界符的用法
Weima, which is going to be listed, still can't give Baidu confidence
如何使用 wget 和 curl 下载文件