当前位置:网站首页>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); } }
边栏推荐
- Countdown 2 days! 2022 China Computing Conference: Mobile cloud invites you to meet with computing network for innovative development
- 拒绝服务 DDoS 攻击
- 半波整流点亮LED
- Socket类关于TCP字符流编程的理解学习
- [dark horse morning post] byte valuation has shrunk to $270billion; "Second uncle" video author responded to plagiarism; Renzeping said that the abolition of the pre-sale system of commercial housing
- PHP generates random numbers (nickname random generator)
- Night God simulator packet capturing wechat applet
- 7.依赖注入
- Lyscript get previous and next instructions
- What is the reason why the words behind word disappear when typing? How to solve it?
猜你喜欢

jar包

Jenkins--持续集成服务器

蓝桥集训(附加面试题)第七天

从手机厂高位“出走”的三个男人

持续(集成--&gt;交付--&gt;部署)

How to check if the interface cannot be adjusted? I didn't expect that the old bird of the 10-year test was planted on this interview question

屈辱、抗争、逆转,三十年,中国该赢微软一次了

半波整流点亮LED

不用Swagger,那我用啥?

Go language - Application of stack - expression evaluation
随机推荐
[dark horse morning post] byte valuation has shrunk to $270billion; "Second uncle" video author responded to plagiarism; Renzeping said that the abolition of the pre-sale system of commercial housing
Analyzing the principle of DNS resolution in kubernetes cluster
数据库系统原理与应用教程(059)—— MySQL 练习题:操作题 1-10(三)
Today's sleep quality record 75 points
最强分布式锁工具:Redisson
No swagger, what do I use?
DOJP1520星门跳跃题解
paddleClas分类实践记录
数据库系统原理与应用教程(061)—— MySQL 练习题:操作题 21-31(五)
Excellent performance! Oxford, Shanghai, AI Lab, Hong Kong University, Shangtang, and Tsinghua have joined forces to propose a language aware visual transformer for reference image segmentation! Open
从手机厂高位“出走”的三个男人
DDoS protection with iptables
Jar package
jar包
POJ1860货币兑换题解
[ecmascript6] symbol and its related use
SQL每日一练(牛客新题库)——第4天:高级操作符
Blue Bridge Training (additional interview questions) day 7
【黑马早报】字节估值缩水,降至2700亿美元;“二舅”视频作者回应抄袭;任泽平称取消商品房预售制是大势所趋;美联储宣布再加息75个基点...
Paddleclas classification practice record
https://www.nowcoder.com/practice/bd891093881d4ddf9e56e7cc8416562d?tpId=85&&tqId=29864&rp=1&ru=/activity/oj&qru=/ta/2017test/question-ranking


