当前位置:网站首页>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 !!!!
边栏推荐
- 数据库系统原理与应用教程(058)—— MySQL 练习题(二):单选题
- Night God simulator packet capturing wechat applet
- 持续(集成--&gt;交付--&gt;部署)
- After finishing, help autumn move, I wish you call it an offer harvester
- 合并表格行---三层for循环遍历数据
- 我秃了!唯一索引、普通索引我该选谁?
- Leetcode notes 566. Reshaping the matrix
- Lyscript get previous and next instructions
- C语言:随机生成数+快速排序
- 剖析 kubernetes 集群内部 DNS 解析原理
猜你喜欢
![[security] read rfc6749 and understand the authorization code mode under oauth2.0](/img/dc/e6d8626195b2e09a6c06050a9b552e.jpg)
[security] read rfc6749 and understand the authorization code mode under oauth2.0

You have to apologize if you get involved in the funny shop?
![[dark horse morning post] byte valuation has shrunk to $270billion;](/img/58/8d5c78d919ed60bc833ec4daa22e23.jpg)
[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

Realize the mutual value transfer between main window and sub window in WPF

使用 IPtables 进行 DDoS 保护

《如何打一场数据挖掘赛事》入门版

111. SAP UI5 FileUploader 控件实现本地文件上传,接收服务器端的响应时遇到跨域访问错误

30天刷题计划(二)

Customized template in wechat applet

SAP ui5 fileuploader control realizes local file upload, and trial version of cross domain access error encountered when receiving server-side response
随机推荐
微念“失去”李子柒的这一年
微信小程序中自定义模板
Go language - Application of stack - expression evaluation
UVA11175有向图D和E From D to E and Back题解
少儿编程 电子学会图形化编程等级考试Scratch二级真题解析(判断题)2022年6月
我秃了!唯一索引、普通索引我该选谁?
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
国产API管理工具Eolink太好用了,打造高效的研发利器
Rust 从入门到精通01-简介
Denial of service DDoS Attacks
Tutorial on the principle and application of database system (060) -- MySQL exercise: operation questions 11-20 (IV)
R语言检验样本比例:使用prop.test函数执行单样本比例检验计算总体中成功样本比例p值的置信区间(设置conf.level参数指定置信水平、置信区间的大小)
Volcanic stone investment Zhang Suyang: hard technology, the relatively certain answer in the next 10 years
POJ3259虫洞题解
长封闭期私募产品再现 业内人士看法各异
图的遍历(BFS&&DFS基础)
力扣 剑指 Offer 51. 数组中的逆序对
Holes in [apue] files
Force buckle 2354. Number of high-quality pairs
SAP ui5 fileuploader control realizes local file upload, and trial version of cross domain access error encountered when receiving server-side response


② Method resolution :
The code is as follows :