Java从0到1刷题记录
目录
我几乎每天都会刷题训练来使自己对各种算法随时保持一个清晰的状态。要知道眼过千遍不如手过一遍,想成为一名合格的开发工程师,更要逼迫自己养成动手的好习惯。
我们都知道,算法的训练对程序员来说及其重要,语言和开发平台不断变化,但是万变不离其宗的是那些算法和理论,刷算法最最最直白的原因就是找一个好的工作,那刷题一定是必不可少的。
现在算法刷题平台还是蛮多的,给大家介绍一个我认为与大厂关联最深的平台——牛客网
相较于其他平台,他们的题单更和工作,大厂靠拢,不光有面试必刷的101到题目,还有大量大厂真题,内容也全程免费,相较于其它会员费结算的来说 非常的友好。
牛客网还支持ACM模式,没有练习过的一定要提前适应!像某团、某为,都要求自己处理输入输出,如果不提前练习会很吃亏的!
牛客的题解更新迭代也很快,讨论区也有技巧的分享,能帮你把所有盲点扫清楚,整体来说还是非常推荐去练习的~
已知抽象类Base中定义了calculate方法,该方法的计算过程依赖于sum()和avg(),而后两个方法均为抽象方法。要求定义Base的子类Sub类,并实现父类的抽象方法,使得main函数中的运算逻辑得以正确执行。
两个整数
两个整数的和除以两个整数的平均值(平均值为int类型,不考虑小数问题)
输入:
1 2
输出:
3
题解:
- import java.util.Scanner;
-
- public class Main {
-
- public static void main(String[] args) {
- // Sub是需要你定义的子类
- Base base = new Sub();
-
- Scanner scanner = new Scanner(System.in);
- while (scanner.hasNextInt()) {
- int x = scanner.nextInt();
- int y = scanner.nextInt();
- base.setX(x);
- base.setY(y);
- System.out.println(base.calculate());
- }
- }
-
- }
-
- abstract class Base {
-
- private int x;
- private int y;
-
- public int getX() {
- return x;
- }
-
- public void setX(int x) {
- this.x = x;
- }
-
- public int getY() {
- return y;
- }
-
- public void setY(int y) {
- this.y = y;
- }
-
- public int calculate() {
- if (avg() == 0) {
- return 0;
- } else {
- return sum() / avg();
- }
- }
-
- /**
- * 返回x和y的和
- */
- public abstract int sum();
-
- /**
- * 返回x和y的平均值
- */
- public abstract int avg();
-
- }
-
- class Sub extends Base {
-
- //write your code here......
- @Override
- public int sum(){
- return super.getX()+super.getY();
- }
- @Override
- public int avg(){
- return (super.getX()+super.getY())/2;
- }
-
- }
已知接口Comparator,内部定义了max函数,用于返回两个整数中的最大值。请定义该接口的实现类,使得main方法中的比较逻辑可以正确执行,要求实现类的名称为ComparatorImpl。
两个整数
两个整数中的最大值
输入:
1 3
输出:
3
题解
- import java.util.Scanner;
-
- public class Main {
-
- public static void main(String[] args) {
- Comparator comparator = new ComparatorImpl();
-
- Scanner scanner = new Scanner(System.in);
- while (scanner.hasNextInt()) {
- int x = scanner.nextInt();
- int y = scanner.nextInt();
- System.out.println(comparator.max(x, y));
- }
- }
-
- }
-
- interface Comparator {
- /**
- * 返回两个整数中的最大值
- */
- int max(int x, int y);
- }
-
- class ComparatorImpl implements Comparator {
- @Override
- public int max(int x,int y){
- return x > y ? x : y;
- }
- }
父类Base中定义了若干get方法,以及一个sum方法,sum方法是对一组数字的求和。请在子类 Sub 中重写 getX() 方法,使得 sum 方法返回结果为 x*10+y
整数
整数的和
输入:
1 2
输出:
12
题解
- import java.util.Scanner;
-
- public class Main {
-
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- while (scanner.hasNextInt()) {
- int x = scanner.nextInt();
- int y = scanner.nextInt();
- Sub sub = new Sub(x, y);
- System.out.println(sub.sum());
- }
- }
-
- }
-
- class Base {
-
- private int x;
- private int y;
-
- public Base(int x, int y) {
- this.x = x;
- this.y = y;
- }
-
- public int getX() {
- return x;
- }
-
- public final int getY() { //无法被子类修改
- return y;
- }
-
- public final int sum() { //无法被子类修改
- return getX() + getY();
- }
-
- }
-
- class Sub extends Base {
-
- public Sub(int x, int y) {
- super(x, y);
- }
-
- public int getX(){ //子类重写getX
- return super.getX() * 10;
- }
-
- }
Singleton类是单例的,每次调用该类的getInstance()方法都将得到相同的实例,目前该类中这个方法尚未完成,请将其补充完整,使得main()函数中的判断返回真(不考虑线程安全)。
无
true
题解
- public class Main {
-
- public static void main(String[] args) {
- Singleton s1 = Singleton.getInstance();
- Singleton s2 = Singleton.getInstance();
- System.out.println(s1 == s2);
- }
-
- }
-
- class Singleton {
-
- private static Singleton instance;
-
- private Singleton() {
-
- }
- public static Singleton getInstance() {
- if (instance == null) {
- instance = new Singleton();
- }
- return instance;
- }
- //write your code here......
-
-
- }
将一个由英文字母组成的字符串转换成从末尾开始每三个字母用逗号分隔的形式。
一个字符串
修改后的字符串
输入:
hellonowcoder
输出:
h,ell,ono,wco,der
题解
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String str = scanner.next();
- StringBuilder newstr = new StringBuilder(str); //用原字符串创建可改变的字符串
- for(int i = str.length() - 3; i >= 0; i -= 3){ //从末尾开始,三个三个遍历
- newstr.insert(i, ','); //插入逗号
- }
- System.out.println(newstr.toString()); //转变成String类输出
- }
- }
给定一个字符串,随机输入一个字母,判断该字母在这个字符串中出现的次数
任意一个字母
字母在字符串中出现次数
输入:
o
输出:
3
输入:
a
输出:
0
题解
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- String string = "H e l l o ! n o w c o d e r";
- Scanner scanner= new Scanner(System.in);
- String word = scanner.next();
- scanner.close();
- System.out.println(check(string, word));
- }
- public static int check(String str,String word){
- return str.length() - str.replace(word,"").length();
- }
- }
点击链接 进行跳转注册,开始你的保姆级刷题之路吧!
另外这里不仅仅可以刷题,你想要的这里都会有,十分适合小白和初学者入门学习~
1、算法篇(398题):面试必刷100题、算法入门、面试高频榜单
2、数据结构篇(300题):都是非常经典的链表、树、堆、栈、队列、动态规划等
3、语言篇(500题):C/C++、java、python入门算法练习
4、SQL篇(82题):快速入门、SQL必知必会、SQL进阶挑战、面试真题
5、大厂笔试真题:字节跳动、美团、百度、腾讯…掌握经验不在惧怕面试!
