当前位置:网站首页>30天刷题计划(二)
30天刷题计划(二)
2022-07-28 12:38:00 【红苹果超好吃】

坚持就是胜利,加油!!!
目录
1.字符串中找出最长的字符串
①题目及示例:
②方法解析:
这个题主要考虑的是最终存放和暂时存放,以及如果进行更新替代。我们这里设置了两个字符串,一个用来存放临时结果,另一个用来存放最后的结果。需要注意的是,当最后一个是数字时,原来的大小可能发生改变,这个时候就需要再次比较两个字符串的大小。将大的结果赋值给最终结果所在的字符串,同时我们应当注意比较的过程中,实质上比较的是ASCLL码的值。详细过程在代码中得以体现。
代码如下:
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String str=sc.nextLine(); //定义两个字符串,一个用来拼接,另一个用来展示最终的结果 String tmp=""; String res=""; int i=0; for(i=0;i<str.length();i++){ //判断当前是否是数字 if(str.charAt(i)>='0'&&str.charAt(i)<='9'){ //说明是数字,这个时候要进行拼接到不是数字为止 tmp+=str.charAt(i)+""; }else{//说明此时不是数字=》是字母或者符号 //为了迎接下一串数字串,我们需要把之前的进行比较,把较长的保留到最后的字符串结果中 if(res.length()<tmp.length()){ res=tmp; }else{ //放置好的同时,我们要把暂时存储的置为0,以便后续的放入 tmp=""; } } } //当字符串最后一个数也是数字时,就有可能使大小发生改变,所以我们还需要最后再一次判断 if(i==str.length()&&res.length()<tmp.length()){ res=tmp; } System.out.println(res); } }
2.数组中出现次数超过一半的数字
①题目及示例:
②方法解析:
a.运用map来解决;利用Map<key,value>的特点,我们可以根据value 的次数来和数组长度的一半来进行比较。需要注意的是这里用到了一个keySet()返回此映射中包含的键的Set视图。然后将map中所有的键存入到Set集合,因为set具备迭代器,所有迭代方式取出所有的键。代码如下:
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.我们利用数组最中间的数来进行判断。我们可以很容易想到,当一个数组按从小到大排序后,那么要是中间的数不是我们要找的大于1/2数组长度的数,那么这个数就不存在,所以,我们先找到中间数的值,利用count计数,来对数组中其它数进行比较,要是与它相等,则count++,最后比较count和1/2数组的大小,来判断是否存在该数。代码如下:
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.我们利用众数来解决问题。我们都知道众数是一组数据中出现最多的数,那么要是这个众数都没有>1/2的数组长度,那么就不可能有这样的数的存在。我们采用众数,将result赋予0下标的数,要是后面的数与其相同,则count++;反之count--;当count=0时,说明该数暂时不是最终的数,我们重新赋予result新的值。要是最后count>0,则说明这样的数可能存在,再去访问数组,看这个值在数组中出现的次数是否大于1/2数组长,大于了则返回该数,反之,返回0;
import java.util.*; public class Solution { public int MoreThanHalfNum_Solution(int [] array) { int result=array[0]; int count=1;//此时result中的数出现一次,count记为1 for(int i=0;i<array.length;++i){ //保证result值不改变的前提是count!=0,所以count应该在外层 if(count!=0){ if(array[i]==result){ ++count; }//不等则-- else{ --count; } }else{//这里是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.计算糖果
①题目及示例:
②方法解析:(简单来说是一个判断三元一次方程组是否有解的问题)
本题主要是找规律。我们可以根据题目已知来得到四个等式,若是最后结果成立,则每个值均只能是唯一的,所以我们通过示例得到的结果等式,可以作为判断条件。之所以用B是因为B在等式中出现了四次,产生了两两对应关系。
代码如下:
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.进制转换
①题目及示例:
②方法解析:
本题主要要考虑以下几个问题。
(1)负数的处理。如果该数为负数,怎么样进行处理,这里用到的是flg==-1或者置为false来进行处理。
(2)因为题目中要求了,如果N大于9,则对应的数字规则参考16进制(比如,10用A表示,等等)所以我们这里引进字符串,利用余数与下标的关系。比如余12,则在12位为C。
然后利用append函数来对末尾进行添加。
(3)如果前面为负,则在append最后添加上符号。最后反转字符串。
a.利用字符串求解:代码如下:
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"; //处理为负数的情况 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.在上述过程中引入栈来求,因为对于栈而言是后进先出的,所以就不用对其结果进行逆置,但是我们仍然需要对负数进行添加负号,这个的操作应该在出栈之前操作完成。代码如下:(但是这样的效率并不高,因为又创建了栈)
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"; //处理为负数的情况 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); } }
边栏推荐
- What if the server cannot be connected (the original server cannot find the target resource)
- [ecmascript6] symbol and its related use
- Children's programming electronic society graphical programming level examination scratch Level 2 real problem analysis (judgment question) June 2022
- 基于深度学习的超分辨率重建
- UVA11175有向图D和E From D to E and Back题解
- [ecmascript6] function and its related use
- C语言:归并排序
- 屈辱、抗争、逆转,三十年,中国该赢微软一次了
- 用非递归的方法实现二叉树中的层遍历,先序遍历,中序遍历和后序遍历
- Is azvudine, a domestic oral new coronal drug, safe? Expert authority interpretation
猜你喜欢

You have to apologize if you get involved in the funny shop?

Using auto.js to realize fifaol3 brush teaching assistant

Blue Bridge Training (additional interview questions) day 7

基于神经网络的帧内预测和变换核选择

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

Shell基础概念和变量

Half wave rectification light LED

酷炫操作预热!代码实现小星球特效

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

Rust from introduction to mastery 01 introduction
随机推荐
C language: random number + quick sort
今日睡眠质量记录75分
倒计时 2 天!2022 中国算力大会:移动云邀您共见算力网络,创新发展
Deployment之滚动更新策略。
使用 IPtables 进行 DDoS 保护
Chapter 6 promotion
[报错]使用ssh登陆到另一台机器后,发现主机名还是自己|无法访问yarn8088
Unity - "synthetic watermelon" small game notes
C语言:优化后的归并排序
ES6 null merge operator (?)
剖析 kubernetes 集群内部 DNS 解析原理
You have to apologize if you get involved in the funny shop?
To build agile teams, these methods are indispensable
Debezium系列之:2.0.0.Beta1的重大变化和新特性
Force buckle 2354. Number of high-quality pairs
nport串口服务器配置网址(串口服务器是不是网口转串口)
Why is crypto game changing the game industry?
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
GO语言-栈的应用-表达式求值
朋友发来几个面试题
https://www.nowcoder.com/practice/bd891093881d4ddf9e56e7cc8416562d?tpId=85&&tqId=29864&rp=1&ru=/activity/oj&qru=/ta/2017test/question-ranking


