Java每日一练(20230313)

news/2025/2/14 6:10:46/

目录

1. 字符串统计  ★

2. 单词反转  ★★

3. 俄罗斯套娃信封问题  ★★★

🌟 每日一练刷题专栏

C/C++ 每日一练 ​专栏

Python 每日一练 专栏

Java 每日一练 专栏


1. 字符串统计

编写一个程序,对于输入的一段英语文本,可以统计:

1、该文本中有多少英语单词;
2、该文本中有多少不同的英语单词。

如,输入 I am a good student. I am in Zhengzhou.
则可以统计出有9个英语单词、7个不同的英语单词。

代码:

import java.util.HashMap;
import java.util.Map;
public class Tee {public static String formatInput(String input) {if (input == null) {return null;}return input.replaceAll("[.|;|\\?]", " ");}public static Map<String, Integer> countWords(String input) {Map<String, Integer> result = new HashMap<String, Integer>();if (input == null || input.length() == 0) {return result;}String[] split = input.split(" ");if (split == null || split.length == 0) {return result;}for (String value : split) {if (result.containsKey(value)) {result.put(value, result.get(value) + 1);} else {result.put(value, 1);}}return result;}public static void main(String[] args) {String value = "I am a good student.I am in Zhengzhou.Ha?";String format = formatInput(value);System.out.println(format);Map<String, Integer> r = countWords(format);System.out.println(r.toString());}
}

原题中用了Map,HashMap,以下代码d只用数组即可: 

public class main {
    public static String formatInput(String input) {
        if (input == null) {
            return null;
        }
        return input.replaceAll("[.|;|\\?]", " ");
    }
    
    public static void countWords(String input) {
        if (input == null || input.length() == 0) {
            return;
        }
        String[] split = input.split(" ");
        if (split == null || split.length == 0) {
            return;
        }
        String[] words = new String[split.length];
        int[] counts = new int[split.length];
        int index = 0;
        for (String value : split) {
            boolean found = false;
            for (int i = 0; i < index; i++) {
                if (words[i].equals(value)) {
                    counts[i]++;
                    found = true;
                    break;
                }
            }
            if (!found) {
                words[index] = value;
                counts[index] = 1;
                index++;
            }
        }
        for (int i = 0; i < index; i++) {
            System.out.println(words[i] + ": " + counts[i]);
        }
    }
    
    public static void main(String[] args) {
        String value = "I am a good student.I am in Zhengzhou.Ha?";
        String format = formatInput(value);
        System.out.println(format);
        countWords(format);
    }


2. 单词反转

随便输出一个字符串 String str ="45abc,+de==fg"; 里面含有 abc,de,fg 三个单词

怎么处理能让单词反转,其他顺序不变呢 输出 “45cba,+ed==gf”;

代码:

public class HelloWorld {public static String revstr(String s) {char[] ch = s.toCharArray();for (int i = 0; i < ch.length; i++) {if ((ch[i] >= 'A' && ch[i] <= 'Z') || (ch[i] >= 'a' && ch[i] <= 'z')) {int j = i + 1;while (j < ch.length && ((ch[j] >= 'A' && ch[j] <= 'Z') || (ch[j] >= 'a' && ch[j] <= 'z')))j++;j--;if (i != j) {for (int k = i; k <= (j - i) / 2 + i; k++) {char temp = ch[k];ch[k] = ch[j - k + i];ch[j - k + i] = temp;}}i = j;}}return new String(ch);}public static void main(String[] args) {System.out.println(revstr("45abc,+de==fg"));}
}

3. 俄罗斯套娃信封问题

给你一个二维整数数组 envelopes ,其中 envelopes[i] = [wi, hi] ,表示第 i 个信封的宽度和高度。

当另一个信封的宽度和高度都比这个信封大的时候,这个信封就可以放进另一个信封里,如同俄罗斯套娃一样。

请计算 最多能有多少个 信封能组成一组“俄罗斯套娃”信封(即可以把一个信封放到另一个信封里面)。

注意:不允许旋转信封。

示例 1:

输入:envelopes = [[5,4],[6,4],[6,7],[2,3]]
输出:3
解释:最多信封的个数为 3, 组合为: [2,3] => [5,4] => [6,7]。

示例 2:

输入:envelopes = [[1,1],[1,1],[1,1]]
输出:1

提示:

  • 1 <= envelopes.length <= 5000
  • envelopes[i].length == 2
  • 1 <= wi, hi <= 10^4

代码:

class Solution {public int maxEnvelopes(int[][] envelopes) {int n = envelopes.length;if (n == 0)return 0;Arrays.sort(envelopes, new Comparator<int[]>() {public int compare(int[] a, int[] b) {if (a[0] != b[0])return a[0] - b[0];return b[1] - a[1];}});List<Integer> lastHeight = new ArrayList<>();lastHeight.add(envelopes[0][1]);for (int i = 1; i < n; i++) {int h = envelopes[i][1];if (h > lastHeight.get(lastHeight.size() - 1))lastHeight.add(h);else {int l = 0, r = lastHeight.size() - 1;while (r > l) {int m = (l + r) >> 1;if (lastHeight.get(m) < h)l = m + 1;elser = m;}lastHeight.set(l, h);}}return lastHeight.size();}
}

🌟 每日一练刷题专栏 🌟

 持续,努力奋斗做强刷题搬运工!

👍 点赞,你的认可是我坚持的动力! 

🌟 收藏,你的青睐是我努力的方向! 

 评论,你的意见是我进步的财富!  

​​

C/C++ 每日一练 ​专栏

​​​

Python 每日一练 专栏

Java 每日一练 专栏


http://www.ppmy.cn/news/32128.html

相关文章

【C++笔试强训】第三十一天

&#x1f387;C笔试强训 博客主页&#xff1a;一起去看日落吗分享博主的C刷题日常&#xff0c;大家一起学习博主的能力有限&#xff0c;出现错误希望大家不吝赐教分享给大家一句我很喜欢的话&#xff1a;夜色难免微凉&#xff0c;前方必有曙光 &#x1f31e;。 选择题 &#x…

如何保证Redis缓存和数据库一致性?

想要保证缓存与数据库的双写一致&#xff0c;一共有4种方式&#xff1a; 先更新缓存&#xff0c;再更新数据库&#xff1b; 先更新数据库&#xff0c;再更新缓存&#xff1b; 先删除缓存&#xff0c;再更新数据库&#xff1b; 先更新数据库&#xff0c;再删除缓存。 我们需要做…

进程间通信IPC

进程间通信IPC (InterProcess Communication) 一、进程间通信的概念 每个进程各自有不同的用户地址空间&#xff0c;任何一个进程的全局变量在另一个进程中都看不到&#xff0c;所以进程之间要交换数据必须通过内核&#xff0c;在内核中开辟一块缓冲区&#xff0c;进程1把数据…

电子工程师必须掌握的硬件测试仪器,你确定你都掌握了?

目录示波器示例1&#xff1a;测量示波器自带的标准方波信号输出表笔认识屏幕刻度认识波形上下/左右移动上下/左右刻度参数调整通道1的功能界面捕获信号设置Menu菜单触发方式触发电平Cursor按钮捕捉波形HLEP按钮参考资料频谱分析仪器信号发生器示波器 示例1&#xff1a;测量示波…

ssh创建秘钥对

1. 使用ssh-keygen 生成秘钥对 [root6zix89b87qmvuv ~]# ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): 按回车键或设置密钥的存储路径 Enter passphrase (empty for no passphrase): 按回车键或设置密钥的存…

到底什么是跨域,如何解决跨域(常见的几种跨域解决方案)?

文章目录1、什么是跨域2、解决跨域的几种方案2.1、JSONP 方式解决跨域2.2、CORS 方式解决跨域&#xff08;常见&#xff0c;通常仅需服务端修改即可&#xff09;2.3、Nginx 反向代理解决跨域&#xff08;推荐使用&#xff0c;配置简单&#xff09;2.4、WebSocket 解决跨域2.5、…

Elasticsearch使用——高级篇

1.数据聚合**聚合&#xff08;aggregations&#xff09;**可以让我们极其方便的实现对数据的统计、分析、运算。例如&#xff1a;什么品牌的手机最受欢迎&#xff1f;这些手机的平均价格、最高价格、最低价格&#xff1f;这些手机每月的销售情况如何&#xff1f;实现这些统计功…

人体姿态识别

自留记录论文阅读,希望能了解我方向的邻域前沿吧 粗读,持续更新 第一篇 ATTEND TO WHO YOU ARE: SUPERVISING SELF-ATTENTION FOR KEYPOINT DETECTION AND INSTANCE-AWARE ASSOCIATION 翻译:https://editor.csdn.net/md?not_checkout=1&spm=1001.2014.3001.5352&…