赞
踩
Java从0到1刷题记录
目录
我几乎每天都会刷题训练来使自己对各种算法随时保持一个清晰的状态。要知道眼过千遍不如手过一遍,想成为一名合格的开发工程师,更要逼迫自己养成动手的好习惯。
我们都知道,算法的训练对程序员来说及其重要,语言和开发平台不断变化,但是万变不离其宗的是那些算法和理论,刷算法最最最直白的原因就是找一个好的工作,那刷题一定是必不可少的。
现在算法刷题平台还是蛮多的,给大家介绍一个我认为与大厂关联最深的平台——牛客网
相较于其他平台,他们的题单更和工作,大厂靠拢,不光有面试必刷的101到题目,还有大量大厂真题,内容也全程免费,相较于其它会员费结算的来说 非常的友好。
牛客网还支持ACM模式,没有练习过的一定要提前适应!像某团、某为,都要求自己处理输入输出,如果不提前练习会很吃亏的!
牛客的题解更新迭代也很快,讨论区也有技巧的分享,能帮你把所有盲点扫清楚,整体来说还是非常推荐去练习的~
从键盘获取一串字符串,要求去除重复的字符。请使用HashSet解决这一问题
键盘输入的任意字符串
去重后的字符串(不要求顺序,预设代码中已经给出输出)
输入:
helloworld
输出:
rdewhlo
题解:
- import java.util.HashSet;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String str = scanner.nextLine();
- scanner.close();
- HashSet<Character> hs = new HashSet<>();
-
- //write your code here......
- // 新建HashSet,然后遍历字符串,将所有字符添加到Set。
- // 由于HashSet本身结构不允许有重复的项,所以会自动去除重复的元素
-
- for(int i=0;i<str.length();i++){
- hs.add(str.charAt(i));
- //str.charAt (i)的意思是第i个字符在字符串str中所占的位置
- //不能直接使用str[i]这种方法来取字符串的值,这种取法只适合数组根据索引下标取值
- }
- // //第二种写法
- // //因为增强for循环只能够对数组进行循环,故我们应该先对str字符串进行toCharArray()将其数组化,进而取得其值
- // for(char c:str.toCharArray(){
- // hs.add(c);
- // }
-
- for (char c:hs) {
- System.out.print(c);
- }
- }
- }
请使用三种方式遍历一个list集合
键盘任意输入的五个int类型变量
使用三种不同的方法遍历集合,遍历输出时不换行,数字之间用空格隔开
输入:
1 2 3 4 5
输出:
普通for循环:1 2 3 4 5 增强for循环:1 2 3 4 5 迭代器遍历:1 2 3 4 5
题解:
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List<Integer> list = new ArrayList<>();
- int num1 = scanner.nextInt();
- int num2 = scanner.nextInt();
- int num3 = scanner.nextInt();
- int num4 = scanner.nextInt();
- int num5 = scanner.nextInt();
- scanner.close();
- list.add(num1);
- list.add(num2);
- list.add(num3);
- list.add(num4);
- list.add(num5);
- System.out.print("普通for循环:");
-
- //write your code here......
- for(int i=0;i<list.size();i++) System.out.print(list.get(i) + " ");
-
- System.out.println();
- System.out.print("增强for循环:");
-
- //write your code here......
- for(Integer x : list) System.out.print(x + " ");
-
- System.out.println();
- System.out.print("迭代器遍历:");
-
- //write your code here......
- Iterator iterator = list.iterator();
- while(iterator.hasNext()) System.out.print(iterator.next() + " ");
-
- System.out.println();
- }
- }
请设计一个排队程序,用户有普通客人和VIP客人之分,VIP客人不排队(即VIP客人在队列头部),目前队列中已有两位客人小明和小军在排队,请将VIP客人小红新增至队列头部。
无
预设代码中已经指定输出
题解:
- import java.util.*;
-
- public class Main {
- public static void main(String[] args) {
- Guest guest1 = new Guest("小明",false);
- Guest guest2 = new Guest("小军",false);
- Guest vipGuest = new Guest("小红",true);
- Deque<Guest> deque = new ArrayDeque<>();
- deque.add(guest1);
- deque.add(guest2);
- deque.addFirst(vipGuest);
- System.out.println(deque);
- }
- }
- class Guest{
- String name;
- Boolean vip;
-
- @Override
- public String toString() {
- return name;
- }
-
- public Guest(String name, Boolean vip) {
- this.name = name;
- this.vip = vip;
-
- }
- }
现从一队学生中抽人上台演讲,为公平起见,队头队尾交替抽取,请通过程序实现这个出队的过程。
一组学生的名字
交替打印队头队尾学生的名字
输入:
Tom Jim Lily Lucy Mary
输出:
Tom Mary Jim Lucy Lily
题解:
- import java.util.ArrayDeque;
- import java.util.Scanner;
-
- public class Main {
-
- public static void main(String[] args) {
- ArrayDeque deque = new ArrayDeque();
-
- Scanner scanner = new Scanner(System.in);
- while (scanner.hasNext()) {
- String name = scanner.next();
- // 初始化队列中的数据
- deque.offerLast(name);
- }
-
- // write your code here......
- int l = deque.size();
- for (int i=0; i<l; i++) {
- if (i%2==0) {
- System.out.println(deque.pollFirst());
- }
- else {
- System.out.println(deque.pollLast());
- }
- }
-
- }
-
- }
统计控制台输入的一句话中不同字⺟字符出现的次数。例如:现有字符串"Hello World!",上述字符串中各个字符的出现的次数为:
H:1
e:1
l:3
o:2
W:1
r:1
d:1
(不考虑数字、空格和特殊字符的个数,按照字符在字符串中出现的顺序显示。相同字母的大小写算两个不同字符)
控制台任意输入一段话,可以有空格和特殊符号
输出字符和字符对应的出现字数(字符和出现次数之间用:隔开,输出逻辑已经给出)
输入:
Hello World
输出:
H:1 e:1 l:3 o:2 W:1 r:1 d:1
题解:
- import java.util.*;
-
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String line = scanner.nextLine();
- Map<Character, Integer> map = new LinkedHashMap<Character, Integer>();
- line = line.replaceAll("[^a-zA-Z]", "");
- for (int i = 0; i < line.length(); i++) {
- char c = line.charAt(i);
- if (map.containsKey(c)) {
- map.put(c, map.get(c) + 1);
- } else {
- map.put(c, 1);
- }
- }
- //write your code here......
-
-
- Set<Map.Entry<Character, Integer>> entrys = map.entrySet();
- for (Map.Entry<Character, Integer> entry : entrys) {
- System.out.println(entry.getKey() + ":" + entry.getValue());
- }
- }
- }
有三个客户对象,将三个客户对象存入集合中,并将其按照消费总额从高到低排序
三个整数,分别代表三个客户的消费值
根据消费额从高到低排序后的集合(输出部分预设代码已经给出,为保证输出格式请不要随意更改)
输入:
234 456 789
输出:
[Customer{name='小红', consumption=789}, Customer{name='小军', consumption=456}, Customer{name='小明', consumption=234}]
题解:
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- Customer customer1 = new Customer("小明",scanner.nextInt());
- Customer customer2 = new Customer("小军",scanner.nextInt());
- Customer customer3 = new Customer("小红",scanner.nextInt());
- List<Customer> customers = new ArrayList<>();
- customers.add(customer1);
- customers.add(customer2);
- customers.add(customer3);
-
- //调用排序接口
- Collections.sort(customers);
-
- System.out.println(customers);
-
- }
- }
-
- class Customer implements Comparable<Customer>{
- private String name;
- private int consumption;
-
- public Customer(String name, int consumption) {
- this.name = name;
- this.consumption = consumption;
- }
-
- @Override
- public String toString() {
- return "Customer{" +
- "name='" + name + '\'' +
- ", consumption=" + consumption +
- '}';
- }
-
- //重写compareTo()方法,按照消费额从高到低排序
- @Override
- public int compareTo(Customer c){
- return c.consumption-consumption;
- }
-
- }
-
点击链接 进行跳转注册,开始你的保姆级刷题之路吧!
另外这里不仅仅可以刷题,你想要的这里都会有,十分适合小白和初学者入门学习~
1、算法篇(398题):面试必刷100题、算法入门、面试高频榜单
2、数据结构篇(300题):都是非常经典的链表、树、堆、栈、队列、动态规划等
3、语言篇(500题):C/C++、java、python入门算法练习
4、SQL篇(82题):快速入门、SQL必知必会、SQL进阶挑战、面试真题
5、大厂笔试真题:字节跳动、美团、百度、腾讯…掌握经验不在惧怕面试!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。