当前位置:网站首页>30 day question brushing plan (II)
30 day question brushing plan (II)
2022-07-28 13:48:00 【Red apples are delicious】

perseverance prevails , come on. !!!
Catalog
1. Find the longest string in the string
2. A number that appears more than half the times in an array
1. Find the longest string in the string
① Topics and examples :
② Method resolution :
This question mainly considers the final storage and temporary storage , And if you update and replace . We set two strings here , One is used to store temporary results , The other is used to store the final results . It should be noted that , When the last one is a number , The original size may change , At this time, you need to compare the size of the two strings again . Assign a large result to the string where the final result is located , At the same time, we should pay attention to the process of comparison , In essence, the comparison is ASCLL The value of the code . The detailed process is embodied in the code .
The code is as follows :
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String str=sc.nextLine(); // Define two strings , One for splicing , The other is used to show the final result String tmp=""; String res=""; int i=0; for(i=0;i<str.length();i++){ // Determine whether the current is a number if(str.charAt(i)>='0'&&str.charAt(i)<='9'){ // The instructions are numbers , At this time, it is necessary to splice until it is not a number tmp+=str.charAt(i)+""; }else{// It means that this is not a number =》 It's letters or symbols // In order to welcome the next string of numbers , We need to compare the previous , Keep the longer one in the final string result if(res.length()<tmp.length()){ res=tmp; }else{ // When placed , We should set the temporarily stored as 0, So that it can be put in later tmp=""; } } } // When the last number of the string is also a number , It is possible to change the size , So we need to judge again for the last time if(i==str.length()&&res.length()<tmp.length()){ res=tmp; } System.out.println(res); } }
2. A number that appears more than half the times in an array
① Topics and examples :
② Method resolution :
a. Application map To solve ; utilize Map<key,value> Characteristics , We can use value And half the length of the array . It should be noted that there is a keySet() Returns the... Of the key contained in this map Set View . And then map All keys in the are stored in Set aggregate , because set With iterator , All iterations take out all keys . The code is as follows :
import java.util.*; public class Solution { public int MoreThanHalfNum_Solution(int [] array) { Map<Integer,Integer>map=new HashMap<>(); int i=0; for(;i<array.length;i++){ if(!map.containsKey(array[i])){ map.put(array[i],1); }else{ map.put(array[i],map.get(array[i])+1); } } int len=array.length/2; for(Integer key : map.keySet()) { if(map.get(key) > len ) { return key; } } return 0; } }b. We use the number in the middle of the array to judge . It's easy for us to think of , When an array is sorted from small to large , Then if the middle number is not what we are looking for greater than 1/2 The number of array lengths , Then this number does not exist , therefore , Let's find the value of the middle number first , utilize count Count , To compare other numbers in the array , If equal to it , be count++, Last comparison count and 1/2 Size of array , To determine whether the number exists . The code is as follows :
import java.util.*; public class Solution { public int MoreThanHalfNum_Solution(int [] array) { Arrays.sort(array); int len=array.length/2; int m=array[len]; int count=0; for(int i=0;i<array.length;i++){ if(array[i]==m){ count++; } } if(count>=len){ return m; }else{ return 0; } } }c. We use modes to solve problems . We all know that mode is the number that appears most in a group of data , Then if there is no mode >1/2 Array length of , Then there can be no such number . We use mode , take result give 0 The number of subscripts , If the following number is the same , be count++; conversely count--; When count=0 when , It indicates that this number is not the final number for the time being , We re endow result New value . If in the end count>0, Then it shows that such a number may exist , Then access the array , See whether the number of times this value appears in the array is greater than 1/2 Team leader , If it is greater than, the number is returned , conversely , return 0;
import java.util.*; public class Solution { public int MoreThanHalfNum_Solution(int [] array) { int result=array[0]; int count=1;// here result The number in appears once ,count Write it down as 1 for(int i=0;i<array.length;++i){ // Guarantee result The premise that the value does not change is count!=0, therefore count It should be on the outer layer if(count!=0){ if(array[i]==result){ ++count; }// If you don't wait -- else{ --count; } }else{// Here is count==0 array[i]=result; count=1; } }count=0; for(int i=0;i<array.length;++i){ if(array[i]==result){ ++count; } } return (count > array.length/2) ? result : 0; } }
3. Calculate candy
① Topics and examples :
② Method resolution :( To put it simply, it is a problem of judging whether there is a solution to a system of ternary linear equations )
This topic is mainly about finding rules . We can get four equations according to the known Title , If the final result is true , Then each value can only be unique , So we get the result equation through the example , It can be used as a judgment condition . The reason to use B Because B There are four times in the equation , There is a corresponding relationship between two .
The code is as follows :
import java.util.*; public class Main { public static void main(String[]args){ Scanner sc=new Scanner(System.in); int a=sc.nextInt(); int b=sc.nextInt(); int c=sc.nextInt(); int d=sc.nextInt(); int A = (a+c)/2; int C = (d-b)/2; int B1 = (b+d)/2; int B2 = (c-a)/2; if(B1 != B2) { System.out.print("No"); }else{ System.out.print(A+" "+B2+" "+C); } } }
4. Hexadecimal conversion
① Topics and examples :
② Method resolution :
The following questions should be considered in this topic .
(1) The treatment of negative numbers . If the number is negative , How to handle , Here's what I'm using flg==-1 Or set it to false To process .
(2) Because the title requires , If N Greater than 9, The corresponding number rules refer to 16 Base number ( such as ,10 use A Express , wait ) So we introduce strings here , Using the relationship between remainder and subscript . For example, Yu 12, It's in 12 Position as C.
And then use it append Function to add to the end .
(3) If the front is negative , It's in append Finally, add the symbol . Last inverted string .
a. Use string to solve : The code is as follows :
import java.util.*; class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); StringBuilder sb=new StringBuilder(); int m=sc.nextInt(); int n= sc.nextInt(); String ret="0123456789ABCDEF"; // Deal with negative numbers int flg=-1; if(m==0){ System.out.println(0); } if(m<0){ m=m*flg; flg=1; } while(m!=0){ sb.append(ret.charAt(m%n)); m/=n; } if(flg==1){ sb.append("-"); } sb.reverse(); System.out.println(sb); } }b. In the above process, the stack is introduced to find , Because for the stack, it is last in first out , Therefore, there is no need to reverse the result , But we still need to add a minus sign to negative numbers , This operation should be completed before the stack . The code is as follows :( But this efficiency is not high , Because the stack is created again )
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); Stack <Character>stack=new Stack<>(); StringBuilder sb=new StringBuilder(); int m=sc.nextInt(); int n= sc.nextInt(); String ret="0123456789ABCDEF"; // Deal with negative numbers int flg=-1; if(m==0){ System.out.println(0); } if(m<0){ m=m*flg; flg=1; } while(m!=0){ stack.push(ret.charAt(m%n)); m/=n; } if(flg==1){ sb.append("-"); } while(!stack.isEmpty()){ sb.append(stack.pop()); } System.out.println(sb); } }
边栏推荐
- Leetcode notes 118. Yang Hui triangle
- To build agile teams, these methods are indispensable
- 性能超群!牛津&上海AI Lab&港大&商汤&清华强强联手,提出用于引用图像分割的语言感知视觉Transformer!代码已开源...
- R语言ggplot2可视化:可视化散点图并为散点图中的数据点添加文本标签、使用ggrepel包的geom_text_repel函数避免数据点标签互相重叠(自定义指定字体类型font family)
- C语言:顺序存储结构的快速排序
- PHP generates random numbers (nickname random generator)
- POJ3259虫洞题解
- R language ggplot2 visualization: use the ggviolin function of ggpubr package to visualize violin diagrams, set the palette parameter, and customize the border colors of violin diagrams at different l
- Remember to use pdfbox once to parse PDF and obtain the key data of PDF
- 30天刷题计划(二)
猜你喜欢

半波整流点亮LED

GO语言-栈的应用-表达式求值

【ECMAScript6】Promise

Map tiles: detailed explanation of vector tiles and grid tiles

火山石投资章苏阳:硬科技,下一个10年相对确定的答案

沾上趣店,都得道歉?

【黑马早报】字节估值缩水,降至2700亿美元;“二舅”视频作者回应抄袭;任泽平称取消商品房预售制是大势所趋;美联储宣布再加息75个基点...

Humiliation, resistance, reversal, 30 years, China should win Microsoft once
JWT login authentication + token automatic renewal scheme, well written!

蓝桥集训(附加面试题)第七天
随机推荐
Debezium series: major changes and new features of 2.0.0.beta1
C语言:优化后的归并排序
Go language - Application of stack - expression evaluation
性能超群!牛津&上海AI Lab&港大&商汤&清华强强联手,提出用于引用图像分割的语言感知视觉Transformer!代码已开源...
Map tiles: detailed explanation of vector tiles and grid tiles
R语言检验样本比例:使用prop.test函数执行单样本比例检验计算总体中成功样本比例p值的置信区间(设置conf.level参数指定置信水平、置信区间的大小)
数据库系统原理与应用教程(062)—— MySQL 练习题:操作题 32-38(六)
30天刷题计划(四)
Leetcode notes 566. Reshaping the matrix
Remember to use pdfbox once to parse PDF and obtain the key data of PDF
合并表格行---三层for循环遍历数据
不用Swagger,那我用啥?
Rust from introduction to mastery 01 introduction
I'm bald! Who should I choose for unique index or general index?
P1797重型运输 题解
使用 IPtables 进行 DDoS 保护
图的遍历(BFS&&DFS基础)
产品经理:岗位职责表
FFT wave simulation
SAP UI5 FileUploader 控件实现本地文件上传,接收服务器端的响应时遇到跨域访问错误的试读版
https://www.nowcoder.com/practice/bd891093881d4ddf9e56e7cc8416562d?tpId=85&&tqId=29864&rp=1&ru=/activity/oj&qru=/ta/2017test/question-ranking


