当前位置:网站首页>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); } }
边栏推荐
- 性能超群!牛津&上海AI Lab&港大&商汤&清华强强联手,提出用于引用图像分割的语言感知视觉Transformer!代码已开源...
- 国产API管理工具Eolink太好用了,打造高效的研发利器
- Strict mode -- let and const -- arrow function -- Deconstruction assignment -- string template symbol -- set and map -- generator function
- C语言:随机生成数+归并排序
- 火山石投资章苏阳:硬科技,下一个10年相对确定的答案
- What if the server cannot be connected (the original server cannot find the target resource)
- [ecmascript6] function and its related use
- 从手机厂高位“出走”的三个男人
- Rust from introduction to mastery 01 introduction
- Leetcode · daily question · 1331. array sequence number conversion · discretization
猜你喜欢

火山石投资章苏阳:硬科技,下一个10年相对确定的答案

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

Shell基础概念和变量

面经整理,助力秋招,祝你称为offer收割机

I miss the year of "losing" Li Ziqi

I copied the bottom of the liquidated NFT, but was locked by opensea

Using auto.js to realize the function of fifaol3 mobile terminal card interceptor

半波整流点亮LED

Cool operation preheating! Code to achieve small planet effect

Half wave rectification light LED
随机推荐
Cesium pit -- pit used by various API calls and API itself
leetcdoe-342. 4的幂
PHP生成随机数(昵称随机生成器)
paddleClas分类实践记录
After finishing, help autumn move, I wish you call it an offer harvester
[报错]使用ssh登陆到另一台机器后,发现主机名还是自己|无法访问yarn8088
jar包
Chapter 6 promotion
Analyzing the principle of DNS resolution in kubernetes cluster
IP电话系统和VoIP系统使用指南
Leetcode notes 566. Reshaping the matrix
用非递归的方法实现二叉树中的层遍历,先序遍历,中序遍历和后序遍历
Gamestop bear market entered NFT trading, and established game retailers took advantage of Web3 to make a second spring
如何配置adb环境变量(环境变量在哪打开)
火山石投资章苏阳:硬科技,下一个10年相对确定的答案
力扣 剑指 Offer 51. 数组中的逆序对
【ECMAScript6】Promise
I miss the year of "losing" Li Ziqi
Countdown 2 days! 2022 China Computing Conference: Mobile cloud invites you to meet with computing network for innovative development
Go language - Application of stack - expression evaluation
https://www.nowcoder.com/practice/bd891093881d4ddf9e56e7cc8416562d?tpId=85&&tqId=29864&rp=1&ru=/activity/oj&qru=/ta/2017test/question-ranking


