当前位置:网站首页>【 Leetcode string, the string transform/hexadecimal conversion 】 HJ1. The length of the string last word HJ2. Calculation of a certain number of characters appear HJ30. String merging processing
【 Leetcode string, the string transform/hexadecimal conversion 】 HJ1. The length of the string last word HJ2. Calculation of a certain number of characters appear HJ30. String merging processing
2022-08-02 17:02:00 【alone_yue】
HJ1.字符串最后一个单词的长度
1.问题描述

2.解决方案
简单的JavaJust enter a string
import java.io.InputStream;
import java.util.*;
public class Main{
public static void main(String [] args) throws Exception{
List<String> list = new ArrayList<>();
Scanner in = new Scanner(System.in);
while(in.hasNext()){
String str = in.next();
list.add(str);
}
System.out.println(list.get(list.size()-1).length());
}
}
HJ2.计算某字符出现次数
1.问题描述

2.解决方案
Directly delete the characters given later,Then compare the length before and after deletion to know,出现次数了!
使用API:str1.replaceAll(str2, “”)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
String str1 = in.nextLine().toUpperCase();
String str2 = in.next().toUpperCase();
String str3 = str1.replaceAll(str2, "");
System.out.println(str1.length()-str3.length());
}
}
HJ30.字符串合并处理
1.问题描述

2.解决方案
Just convert it according to the title,But there are a lot of base conversion relatedAPI的使用,可以回来reviewon a regular basis
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.*;
public class Main{
public static void main(String[] args) throws Exception{
//Enter and splice
Scanner in = new Scanner(System.in);
String str = in.next() + in.next();
//Parity is sorted and returned
List<Character> list1 = new ArrayList<>();
List<Character> list2 = new ArrayList<>();
for(int i=0;i<str.length();i++){
if(i%2==0) list1.add(str.charAt(i));
else list2.add(str.charAt(i));
}
Collections.sort(list1);
Collections.sort(list2);
StringBuilder sb = new StringBuilder();
int p1 = 0;
int p2 = 0;
for(int i=0;i<str.length();i++){
Character c = i%2==0 ? list1.get(p1++) : list2.get(p2++);
sb.append(c);
}
//转换操作
for(int i=0;i<sb.length();i++){
//判断是否是'0'~'9'、'A'~'F'和'a'~'f'字符
if(!( ('0'<=sb.charAt(i)&&sb.charAt(i)<='9') || ('a'<=sb.charAt(i)&&sb.charAt(i)<='f') || ('A'<=sb.charAt(i)&&sb.charAt(i)<='F'))) continue;
//a->10
int a = Integer.parseInt(String.valueOf(sb.charAt(i)), 16);
//二进制串
String str0 = Integer.toBinaryString(a);
//Requires leading zeros
while(str0.length()!=4){
str0 = "0"+str0;
}
//Reverse binary string
String str1 = (new StringBuilder(str0)).reverse().toString();
//Binary strings to numbers Number to hexadecimal string
int b = Integer.parseInt(str1, 2);
String str3 = Integer.toHexString(b).toUpperCase();
sb.replace(i,i+1,str3);
}
//输出
System.out.println(sb.toString());
}
}
边栏推荐
- 已解决ModuleNotFoundError: No module named‘ pip‘(重新安装pip的两种方式)
- 机械键盘失灵
- Impulse response invariant method and bilinear transformation method for IIR filter design
- 两分钟录音就可秒变语言通!火山语音音色复刻技术如何修炼而成?
- 单例模式(singleton pattern)
- Principles of permutation entropy, fuzzy entropy, approximate entropy, sample entropy and approximate entropy implemented by MATLAB
- 2022/7/15,我的人生中第一篇博客,不忘初心,砥砺前行!
- ShardingSphere基本介绍及核心概念
- 【go-zero】go-zero 框架踩坑指南 Q&A (持续更新中)
- 2022-07-27 第六小组 瞒春 学习笔记
猜你喜欢
随机推荐
如何查看微信小程序服务器域名并且修改
【js手风琴效果案例】
基于mobileNet实现狗的品种分类(迁移学习)
XML和注解(Annotation)
2022-02-14 第五小组 瞒春 学习笔记
电设3----脉冲信号测试仪
Mechanical keyboard failure
【QMT】给QMT量化交易软件安装和调用第三方库(举例通达信pytdx,MyTT,含代码)
MATLAB file operations
(数学基础)第三章-3.2-标准记号和常用函数
机械键盘失灵
第六章-6.1-堆-6.2-维护堆的性质-6.3-建堆
数据库性能优化的误区!
PAT甲级 1137 期终成绩
告别手摇织布机的AI时代
ShardingSphere基本介绍及核心概念
职工管理系统(SSM整合)
已解决ModuleNotFoundError: No module named‘ pip‘(重新安装pip的两种方式)
2022-0801 第六小组 瞒春 学习笔记
阅读,是最便宜的高贵









