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

Insist on Duck !!!
Catalog
1. Statistical palindrome
① Topics and examples :
② Method resolution :
This question mainly focuses on palindrome judgment and how to insert . Here we can use double pointers to judge palindromes , For string insertion , We can use str.insert(i,str2) Method , Namely the str2 This string is inserted str A string of i Location .
a. Use double pointers to judge palindromes :
The code is as follows :
import java.util.*; public class Main { public static boolean huiwen(StringBuffer str){ int left=0; int right=str.length()-1; while(left<=right){ if(str.charAt(left)!=str.charAt(right)){ return false; } left++; right--; } return true; } public static void main(String[]args){ Scanner sc=new Scanner(System.in); String str1=sc.nextLine(); String str2=sc.nextLine(); int count=0; int i=0; for(i=0;i<=str1.length();i++){ StringBuffer str = new StringBuffer(str1); str.insert(i, str2); if(huiwen(str)){ count++; } } System.out.println(count); } }b. Using inversion , Invert the inserted string , Then judge whether the content of the string is consistent with that before insertion , Here we use equal() Method to operate . The code is as follows :
import java.util.*; public class Main { public static void main(String[]args){ Scanner sc=new Scanner(System.in); String str1=sc.nextLine(); String str2=sc.nextLine(); int count=0; int i=0; for(i=0;i<=str1.length();i++){ StringBuffer str = new StringBuffer(str1); str.insert(i, str2); StringBuffer tmp=new StringBuffer(str); StringBuffer str3=tmp.reverse(); //equals It's a string method , and toString Here is the general stringBuilder Converts to string type , To use this method . if(str.toString().equals(str3.toString())){ count++; } } System.out.println(count); } }
2. Continuous maximum sum
① Topics and examples :
② Method resolution :
This problem is mainly a dynamic programming problem .
Before we solve the problem , You need to know what the equation of state is ?
Equation of state : max( dp[ i ] ) = getMax( max( dp[ i -1 ] ) + arr[ i ] ,arr[ i ] ) ;among ,dp[i] I mean from 0 Start adding i This position ,arr[i] Indicates the subscript of the current element . So we can get the following code :import java.util.*; public class Main { public static void main(String[]args){ Scanner sc=new Scanner(System.in); int num=sc.nextInt(); int []array=new int[num]; for(int i=0;i<array.length;i++){ array[i]=sc.nextInt(); } int max=array[0]; int max1=0; int sum=array[0]; for(int i=1;i<array.length;i++){ max1=(sum+array[i])>array[i]?(sum+array[i]):array[i]; sum=max1; if(sum>=max){ max=sum;// Because it's using max To store the final maximum } } System.out.println(max); } }
3. No two
① Topics and examples :
② Method resolution :
According to the title , We can easily find that this is a two-dimensional array , We square the square root of the arithmetic to get
(x1-x2) * (x1-x2) + (y1-y2) * (y1-y2)=4; namely
(x1-x2)^2+(y1-y2)^2=4, We take the critical value , We can know
0+4=4;
4+0=4;
If 2+2=4 Then it does not satisfy the integer situation . So we get x1=x2 and y1-y2=2 or x1-x2=2 and y1=y2;
That means when there's a cake somewhere , It can't put cake in these two positions .
We initially set the value of the entire array to 0, Then use one for each cake count Count , The place where you can't put the cake is 1.
The code is as follows :
import java.util.*; public class Main{ public static void main(String[]args){ Scanner sc=new Scanner(System.in); int W=sc.nextInt(); int H=sc.nextInt(); int count=0; // Set the size of all values of the two-dimensional array to 0, When it can store cakes ,count++; If you can't put the cake, set it to 1 int [][]tmp=new int[W][H]; for(int i=0;i<W;i++){ for(int j=0;j<H;j++){ if(tmp[i][j]==0){ count++; // When the cake has been put here , Then the Euclidean distance from it cannot exceed 2 // situation ①: Same as i;j+2, Judge j Is it across the border? if(j+2<H){ tmp[i][j+2]=1; } // situation ②: Same as j,i+2; Judge i Is it across the border? if(i+2<W){ tmp[i+2][j]=1; } } } } System.out.println(count); } }
4. String to integer
① Topics and examples :
② Method resolution :
Title , We mainly consider that the first character of the string is +, still -; Because for the plus sign , Its value is equal to itself , For the minus sign, its value is equal to its opposite number , utilize flg==1/flg==-1 To restore the last value . But what we need to know is , We can't get rid of the number at the beginning of it , You can only set that position to an empty position , But remember to subtract when you finally calculate , Because for symbols , The calculation is ASCLL value , Just subtract that placeholder . Because for toCharArray() In terms of method , Copying the past is just a copy . It is also a character type , So at this point -‘0’ It is equivalent to itself .
The code is as follows :
public class Solution { public int StrToInt(String str) { char[]tmp=str.toCharArray(); int flg=1; // When the array is empty or the array length is 0 when , There's no need to go down , Go straight back to 0 if(tmp==null||tmp.length==0){ return 0; } // If the first position is + if(tmp[0]=='+'){ flg=1; // At the same time, set this position to empty tmp[0]='0'; } // If the first position is - if(tmp[0]=='-'){ flg=-1; tmp[0]='0'; } int sum=0; for(int i=0;i<tmp.length;i++){ if(tmp[i]<'0'||tmp[i]>'9'){ return 0; }else{ sum=sum*10+tmp[i]-'0';// Subtract the placeholder 0 } } return sum*flg; } }
边栏推荐
- Leetcode notes 566. Reshaping the matrix
- Generation of tables and contingency tables (cross tables) of R language factor data: use the summary function to analyze the list, view the chi square test results, and judge whether the two factor v
- Debezium series: major changes and new features of 2.0.0.beta1
- Half wave rectification light LED
- C语言:随机生成数+快速排序
- R语言检验样本比例:使用prop.test函数执行单样本比例检验计算总体中成功样本比例p值的置信区间(设置conf.level参数指定置信水平、置信区间的大小)
- R语言ggplot2可视化:可视化散点图并为散点图中的数据点添加文本标签、使用ggrepel包的geom_text_repel函数避免数据点标签互相重叠(自定义指定字体类型font family)
- 要想组建敏捷团队,这些方法不可少
- POJ3268最短路径题解
- paddleClas分类实践记录
猜你喜欢

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

SAP ui5 fileuploader control realizes local file upload, and trial version of cross domain access error encountered when receiving server-side response

最强分布式锁工具:Redisson

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

我秃了!唯一索引、普通索引我该选谁?

Org.apache.ibatis.exceptions.toomanyresultsexception

【安全】 阅读 RFC6749 及理解 Oauth2.0 下的授权码模式

30天刷题计划(二)

Beyond istio OSS -- current situation and future of istio Service Grid

微念“失去”李子柒的这一年
随机推荐
I'm bald! Who should I choose for unique index or general index?
C language: random number + quick sort
R language uses LM function to build multiple linear regression model, writes regression equation according to model coefficient, and uses conflict function to give 95% confidence interval of regressi
R语言使用lm函数构建线性回归模型、使用subset函数指定对于数据集的子集构建回归模型(使用floor函数和length函数选择数据前部分构建回归模型)
Go language - Application of stack - expression evaluation
屈辱、抗争、逆转,三十年,中国该赢微软一次了
Rust from introduction to mastery 01 introduction
Beyond Istio OSS——Istio服务网格的现状与未来
数据库系统原理与应用教程(060)—— MySQL 练习题:操作题 11-20(四)
Tutorial on the principle and application of database system (062) -- MySQL exercise questions: operation questions 32-38 (6)
倒计时 2 天!2022 中国算力大会:移动云邀您共见算力网络,创新发展
POJ1860货币兑换题解
要想组建敏捷团队,这些方法不可少
word打字时后面的字会消失是什么原因?如何解决?
Lyscript get previous and next instructions
力扣 剑指 Offer 51. 数组中的逆序对
SQL daily practice (Niuke new question bank) - day 4: advanced operators
UVA11175有向图D和E From D to E and Back题解
Li Kou sword finger offer 51. reverse order pairs in the array
R language test sample proportion: use prop The test function performs the single sample proportion test to calculate the confidence interval of the p value of the successful sample proportion in the



