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

Catalog
1. Team competition
① Topics and examples :
② Method resolution :( The algorithm involved in this problem is greedy algorithm )
a. According to the meaning , You can't find the largest , Go back and find the second largest number . Then we get through relationship , The subscript of the second largest number should be array[array.length-(2*(i+1))]; among i Is the number of trips . So the code is as follows :
import java.util.*; public class Main{ public static void main(String[]args){ Scanner sc=new Scanner(System.in); while(sc.hasNextInt()){ int n=sc.nextInt(); int []array= new int[3*n]; for(int i=0;i<(3*n);i++){ array[i]=sc.nextInt(); } Arrays.sort(array); long sum=0; ; for(int i=0;i<n;i++){ sum+=array[array.length-(2*(i+1))]; } System.out.println(sum); } } }A few noteworthy places :
(1)hasNext() Usage of :
When there is no input , It will be blocked . Or block
(2) Right in the title a_i The numerical requirement of is <10^6, however int The approximate value range of is 10^6-10^7, So this place is still usable int To embellish .
b. Use to remove the smallest in each group , Then find the smaller of the remaining two values and add them . After sorting, we can directly remove the front n Number , Because before necessity n The number is the smallest , Then use the subscript separation to find the smaller value of the two numbers . The code is as follows :
import java.util.*; public class Main{ public static void main(String[]args){ Scanner sc=new Scanner(System.in); while(sc.hasNextInt()){ int n=sc.nextInt(); int []array= new int[3*n]; for(int i=0;i<(3*n);i++){ array[i]=sc.nextInt(); } Arrays.sort(array); long sum=0; // 1、 Before removal n A smaller number // 2、 Take the rest 2*n Combine numbers in pairs // 3、 Take the smaller number in each group , Sum up ; for(int i=n;i<array.length;i+=2){ sum+=array[i]; } System.out.println(sum); } } }
2. Delete public characters
① Topics and examples :
② Method resolution :
Reverse thinking , For the output to be . Do not consider deleting , Because deleting means reordering , Instead, it increases the time complexity . We can record what we want to remove , Then words that do not involve records in the whole exist in another string . At this time, we use hash map To do this . Here are the methods involved .
a.map.put(key,value);
b.map.get(key); Used to get key The number of times it exists
c.map.containsKey(i); Boolean type , Used to determine whether the value exists
d.s.length() Method is used to find the length of a string
e.s.charAt(i); Reduce string s, And return to its number i Characters
The difference between the following two methods , It depends on whether to splice strings directly or construct a StringBuffer object , Use it append Methods to solve . Obviously , The former costs less , It's also something we need to learn .
(1) The string splicing code is as follows :
import java.util.*; public class Main{ public static void main(String[]args){ Scanner sc=new Scanner(System.in); String m=sc.nextLine(); String n=sc.nextLine(); Map<Character,Integer> map=new HashMap<>(); for(int i=0;i<n.length();i++){ if(map.get(n.charAt(i))==null){ map.put(n.charAt(i),1); }else{ map.put(n.charAt(i),map.get(n.charAt(i))+1); } } String sbu=""; for(int i=0;i<m.length();i++){ if(!map.containsKey(m.charAt(i))){ sbu+=m.charAt(i); } } System.out.println(sbu); } }(2) utilize StringBuilder/StringBuffer To create object solutions :
import java.util.*; public class Main{ public static void main(String[]args){ Scanner sc=new Scanner(System.in); String m=sc.nextLine(); String n=sc.nextLine(); Map<Character,Integer> map=new HashMap<>(); for(int i=0;i<n.length();i++){ if(map.get(n.charAt(i))==null){ map.put(n.charAt(i),1); }else{ map.put(n.charAt(i),map.get(n.charAt(i))+1); } } StringBuffer sbu=new StringBuffer(); for(int i=0;i<m.length();i++){ if(!map.containsKey(m.charAt(i))){ sbu.append(m.charAt(i)); } } System.out.println(sbu); } }
3. Inverted string
① Topics and examples :
② Method resolution :
We analyze the whole string , It's just that each word as a whole is inverted , In fact, there is no inversion inside each word , So we can have the following two ideas to complete this problem .
a. Use between words to “ ” Separated , So we use split function , Divide it into words to form an array , Then use string splicing to splice from back to front , After each word , add “ ”; Finally, in order to ensure preciseness , Remove the blanks before and after the sentence , Use trim Method to do .
The code is as follows :
import java.util.Scanner; import java.util.Stack; public class Main{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); String ret=sc.nextLine(); String []array=ret.split(" "); String str=""; for(int i=0;i<array.length;i++){ str+=array[array.length-1-i]; str=str+" "; } // Remove the spaces before and after the sentence , Keep the space in the middle String str1=str.toString().trim(); System.out.println(str1); } }b. First, the whole is reversed , Then partially reverse . Use the double pointer method to solve this problem . Pay attention to two variables when local inversion i,j Change position of . Mainly used the following methods .
(1)s.toCharArray(); Return a copy of the data to the array you need , At the bottom is an array .
The code is as follows :
import java.util.Scanner; import java.util.*; public class Main{ public static void reverse(char[]array,int left,int right){ while(left<=right){ char tmp=array[left]; array[left]=array[right]; array[right]=tmp; left++; right--; } } public static void main(String[] args) { Scanner sc=new Scanner(System.in); String ret=sc.nextLine(); char[] ch=ret.toCharArray(); reverse(ch,0,ch.length-1); int i=0; while(i<ch.length){ int j=i; while(j<ch.length&&ch[j]!=' '){ j++; } if(j<ch.length){ reverse(ch,i,j-1); i=j+1; j++; }else{ reverse(ch,i,j-1); i=j; } } String str = new String(ch); System.out.println(str); } }
4. Sort subsequence
① Topics and examples :
② Method resolution :
This topic mainly lies in the understanding of the meaning of the topic . among , What does non increasing and non decreasing represent respectively . Then there is flexible use if and while In the cycle , It is worth noting that , When a condition is not met , Whether it is not satisfied until it is finished in another condition , This is a key point .
The code is as follows :
import java.util.*; public class Main { public static void main(String[]args){ Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int []array=new int[n+1]; // Assign values to arrays for(int i=0;i<n;i++){ array[i]=sc.nextInt(); } int i=0;// use i To move int count=0;// use count To count while(i<n){ // The decreasing if(array[i]<array[i+1]){ while(i<n&&array[i]<=array[i+1]){ i++;// When a i When not satisfied , First, jump out of the current cycle , Then execute the if // After the sentence in, make a subsequent judgment } count++; i++; }// The same situation will not change the results before and after else if(array[i]==array[i+1]){ i++; }// Non-increasing else if(array[i]>array[i+1]){ while(i<n&&array[i]>=array[i+1]){ i++; } count++; i++; } } System.out.println(count); } }
insist !!!!
边栏推荐
- .NET桌面开发的一些思考
- 接口调不通,如何去排查?没想到10年测试老鸟栽在这道面试题上
- Beyond istio OSS -- current situation and future of istio Service Grid
- Rust from introduction to mastery 01 introduction
- 不用Swagger,那我用啥?
- DDoS protection with iptables
- 倒计时 2 天!2022 中国算力大会:移动云邀您共见算力网络,创新发展
- Use non recursive method to realize layer traversal, preorder traversal, middle order traversal and post order traversal in binary tree
- You have to apologize if you get involved in the funny shop?
- 蓝桥集训(附加面试题)第七天
猜你喜欢

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

Children's programming electronic society graphical programming level examination scratch Level 2 real problem analysis (judgment question) June 2022

半波整流点亮LED

不用Swagger,那我用啥?

今日睡眠质量记录75分

比XShell更好用、更现代的终端工具!

Countdown 2 days! 2022 China Computing Conference: Mobile cloud invites you to meet with computing network for innovative development

沾上趣店,都得道歉?

Can second uncle cure young people's spiritual internal friction?

最强分布式锁工具:Redisson
随机推荐
不用Swagger,那我用啥?
严格模式——let和const——箭头函数——解构赋值——字符串模板symbol——Set和Map——生成器函数
.NET的求复杂类型集合的差集、交集、并集
Humiliation, resistance, reversal, 30 years, China should win Microsoft once
[ecmascript6] symbol and its related use
C语言:归并排序
POJ1860货币兑换题解
屈辱、抗争、逆转,三十年,中国该赢微软一次了
今日睡眠质量记录75分
Force buckle 2354. Number of high-quality pairs
JWT 登录认证 + Token 自动续期方案,写得太好了!
Guide for using IP phone system and VoIP system
Some thoughts on.Net desktop development
Tutorial on the principle and application of database system (058) -- MySQL exercise (2): single choice question
【安全】 阅读 RFC6749 及理解 Oauth2.0 下的授权码模式
R语言使用lm函数构建多元回归模型(Multiple Linear Regression)、并根据模型系数写出回归方程、使用confint函数给出回归系数的95%置信区间
性能超群!牛津&上海AI Lab&港大&商汤&清华强强联手,提出用于引用图像分割的语言感知视觉Transformer!代码已开源...
使用 Fail2ban 保护 Web 服务器免受 DDoS 攻击
Rolling update strategy of deployment.
C language: random number + quick sort
https://www.nowcoder.com/questionTerminal/6736cc3ffd1444a4a0057dee89be789b?orderByHotValue=1&page=1&onlyReference=false

② Method resolution :
The code is as follows :