当前位置: 首页> 科技> 能源 > 企业宣传片视频模板_中国合同网官网_会计培训机构排名前十_今天国际新闻最新消息

企业宣传片视频模板_中国合同网官网_会计培训机构排名前十_今天国际新闻最新消息

时间:2025/7/12 6:13:03来源:https://blog.csdn.net/qq_46103963/article/details/146262760 浏览次数:1次
企业宣传片视频模板_中国合同网官网_会计培训机构排名前十_今天国际新闻最新消息

题目描述:

根据输入的字符串,按照一定的规则将字母分配给数字字符,并生成一个新的字符串1. 如果字符串只包含一个数字字符,则所有的字母按顺序分配给该数字字符2. 如果字符串中包含01,则a分配给1,其余的字母按照顺序分配给03. 输出字符串是根据输入字符串中的数字字符,依次从对应的字母列表中取出字母拼接而成

在这里插入图片描述
输入:01字符串,这里使用String
输出:字符串
思路

  1. 先将字符串转换为字符数组
  2. 存储到Set集合中,判断只有一种字符还是有两种
  3. 只有一种的话,把所有的小写字母分配给唯一的数字,有两种的话把‘a’分配给1,其他的‘b’-‘z’分配给0
  4. 然后遍历字符串,用小写字符替换
//然后遍历字符串数组//使用StringBuilder做拼接StringBuilder sb = new StringBuilder();//定义0和1的当前索引int[] indexs = new int[2];for(char c : s.toCharArray()){List<Character> list = map.get(c);int index = indexs[c - '0'] % list.size();sb.append(list.get(index));indexs[c - '0']++;}

关于字符串拼接:
使用StringBuilder来操作StringBuilder sb = new StringBuilder();,然后定义一个数组来存储0、1的当前索引int[] indexs = new int[2];,遍历数组for(char c : s.toCharArray()),取出当前数字字符对应的小写字母链表List<Character> list = map.get(c),然后得到链表中的小写字符int index = indexs[c - '0'] % list.size();,进行StringBuilder拼接,sb.append(list.get(index));然后将当前字符指向下一个小写字符indexs[c - '0']++;


import java.util.*;/*** @Title: test_Ant_1* @Author Vacant Seat* @Date 2025/3/14 17:46*/
public class test_Ant_1 {public static void main(String[] args) {//输入Scanner sc = new Scanner(System.in);String s = sc.next();//调用方法String s1 = numToChar(s);System.out.println(s1);}private static String numToChar(String s){Set<Character> set = new HashSet<>();for(char c : s.toCharArray()){set.add(c);}//定义一个HashMap,用来存储对应的关系Map<Character,List<Character>> map = new HashMap<>();//定义两个字符链表,存储小写字母List<Character> zeroList = new ArrayList<>();List<Character> oneList = new ArrayList<>();if(set.size() == 1){//将所有的字母分配给唯一的数字char num = set.iterator().next();//说明此时只有一个数字字符for(char c = 'a'; c <= 'z'; c++){zeroList.add(c);}//将num和zeroList存入mapmap.put(num, zeroList);}else {oneList.add('a');for(char c = 'b'; c <= 'z'; c++){zeroList.add(c);}map.put('1',oneList);map.put('0',zeroList);}//然后遍历字符串数组//使用StringBuilder做拼接StringBuilder sb = new StringBuilder();//定义0和1的当前索引int[] indexs = new int[2];for(char c : s.toCharArray()){List<Character> list = map.get(c);int index = indexs[c - '0'] % list.size();sb.append(list.get(index));indexs[c - '0']++;}return sb.toString();}}
关键字:企业宣传片视频模板_中国合同网官网_会计培训机构排名前十_今天国际新闻最新消息

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: