2021 E3 算法题第二题(Rename Photo Names)

2024-04-21 06:52

本文主要是介绍2021 E3 算法题第二题(Rename Photo Names),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目内容

John likes to travel. He has visited a lot of cities over many years. Whenever he visits a city, he takes a few photos and saves them on his computer. Each photo has a name with an extension ("jpg", "png" or "jpeg’) and there is a record of the name of the city where the photo was taken and the time and date the photo; for example:
"photo.jpg, Warsaw, 2013-09-05 14:08:15".Note that, in some rare cases, photos from different locations may share the time and date, due to timezone differences.John notices that his way of filing photos on his computer has become a mess. He wants to reorganize the photos. First he decides to group the photos by city, then, within each such group, sort the photos by the time they were taken and finally assign consecutive natural numbers to the photos, starting from 1. Afterwards he intends to rename all the photos. The new name of each photo should begin with the name of the city followed by the number already assigned to that photo. The number of every photo in each group should have the same length (equal to the length of the
largest number in this group); thus, John needs to add some leading zeros to the numbers. The new name of the photo should end with the extension, which should remain the same.Your task is to help John by finding the new name of each photo.Each of John's photos has the format: "<<photoname>>.<<extension>>,<<city_name>>, yyyy-mm-dd hh:mm:ss", where "<<photoname>>", " <<extension>>" and "<<city_name>>" consist only of letters of the English alphabet and supply the name of the photo, the file name extension and the city name, respectively. Be aware that the names of the photos may not be unique.Write a function:class Solution { public String solution(String S); }that, given a string representing the list of M photos, returns the string representing
the list of the new names of all photos (the order of photos should stay the same).
For example, given a string:
photo.jpg, Warsaw, 2013-09-05 14:08:15
john.png, London, 2015-06-20 15:13:22
myFriends.png, Warsaw, 2013-09-05 14:07:13
Eiffel.jpg, Paris, 2015-07-23 08:03:02
pisatower.jpg, Paris, 2015-07-22 23:59:59
BOB.jpg, London, 2015-08-05 00:02:03
notredame.png, Paris, 2015-09-01 12:00:00
me.jpg, Warsaw, 2013-09-06 15:40:22
a.png, Warsaw, 2016-02-13 13:33:50
b.jpg, Warsaw, 2016-01-02 15:12:22
c.jpg, Warsaw, 2016-01-92 14:34:30
d.jpg, Warsaw, 2016-01-02 15:15:01
e.png, Warsaw, 2016-01-02 09:49:99
f.png, Warsaw, 2016-01-92 10:55:32
g.jpg, Warsaw, 2016-92-29 22:13:11your function should return:Warsaw02. jpg
London1. png
Warsaw01. png
Paris2.jpg
Paris1.jpg
London2. jpg
Paris3.png
Warsaw03. jpg
Warsaw09.png
Warsaw07. jpg
Warsaw6. jpg
Warsaw08. jpg
Warsawe4. png
Warsaw05. png
Warsaw10. jpgas there are ten Photos taken in Warsaw (numbered from 01 to 10),
London (numbered from two photos in 1 to 2) and three Photos in Paris (numbered from 1 to 3).The new names of the photos are returned in the same order as in the given String.

解法一

思路

创建一个Photo的对象,Photo对应的属性有photoName, extension, cityName, createDate还有新名字。然后把输入的字符串按换行符分割,分割后构造一个Photo的链表和Map,Map的key是城市名字,值是这个城市里所有的图片对象。然后对每个城市里的所有照片排序,然后再重命名照片的名字。最后把所有图片的新名字组成一个字符串输出。

java代码实现

public class Task2 {SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");class Photo{String photoName;String extention;String cityName;Date createdDate;String newPhotoName;Photo(String photoStr)  {String[] strs = photoStr.split(",");this.photoName = strs[0];this.extention = strs[0].split("\\.")[1];this.cityName = strs[1].strip();try {this.createdDate = format.parse(strs[2].strip());}catch (ParseException e){e.printStackTrace();;}}void rename(int index, int size){int zeroLength = String.valueOf(size).length() - String.valueOf(index).length();String zeroStr = getZeroStr(zeroLength);this.newPhotoName = cityName+zeroStr+index+"."+extention;}String getZeroStr(int zeroLength){String zeroStr = "";for (int i=0; i< zeroLength ;i++){zeroStr  = zeroStr + "0";}return zeroStr;}}public String solution(String text) {System.out.println(text);String[] phoneStrArray = text.split("\n");Map<String, List<Photo>> photoGroupByCity = new HashMap<>();List<Photo> photos = new ArrayList<>();for(int i=0; i< phoneStrArray.length; i++){Photo photo = new Photo(phoneStrArray[i]);photos.add(photo);if (photoGroupByCity.get(photo.cityName)==null){List<Photo> photoTemp = new ArrayList<>();photoTemp.add(photo);photoGroupByCity.put(photo.cityName, photoTemp);}else{photoGroupByCity.get(photo.cityName).add(photo);}}photoGroupByCity.forEach((cityName, photoGroup)->{photoGroup.sort(new Comparator<Photo>() {@Overridepublic int compare(Photo o1, Photo o2) {return o1.createdDate.compareTo(o2.createdDate);}});;});photoGroupByCity.forEach((cityName, photosGroup) ->{for (int i=0; i< photosGroup.size() ; i ++){Photo photo = photosGroup.get(i);photo.rename(i+1, photosGroup.size());}});String newPhoneNames = "";for (int i=0 ; i< photos.size(); i++){newPhoneNames = newPhoneNames+ photos.get(i).newPhotoName;if(i != photos.size()-1){newPhoneNames = newPhoneNames + "\n";}}return newPhoneNames;}public static void main(String[] args) {Task2 task = new Task2();String text = "photo.jpg, Warsaw, 2013-09-05 14:08:15\n" +"john.png, London, 2015-06-20 15:13:22\n" +"myFriends.png, Warsaw, 2013-09-05 14:07:13\n" +"Eiffel.jpg, Paris, 2015-07-23 08:03:02\n" +"pisatower.jpg, Paris, 2015-07-22 23:59:59\n" +"BOB.jpg, London, 2015-08-05 00:02:03\n" +"notredame.png, Paris, 2015-09-01 12:00:00\n" +"me.jpg, Warsaw, 2013-09-06 15:40:22\n" +"a.png, Warsaw, 2016-02-13 13:33:50\n" +"b.jpg, Warsaw, 2016-01-02 15:12:22\n" +"c.jpg, Warsaw, 2016-01-92 14:34:30\n" +"d.jpg, Warsaw, 2016-01-02 15:15:01\n" +"e.png, Warsaw, 2016-01-02 09:49:99\n" +"f.png, Warsaw, 2016-01-92 10:55:32\n" +"g.jpg, Warsaw, 2016-92-29 22:13:11";System.out.println(task.solution(text));}
}

这篇关于2021 E3 算法题第二题(Rename Photo Names)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/922424

相关文章

使用雪花算法产生id导致前端精度缺失问题解决方案

《使用雪花算法产生id导致前端精度缺失问题解决方案》雪花算法由Twitter提出,设计目的是生成唯一的、递增的ID,下面:本文主要介绍使用雪花算法产生id导致前端精度缺失问题的解决方案,文中通过代... 目录一、问题根源二、解决方案1. 全局配置Jackson序列化规则2. 实体类必须使用Long封装类3.

Springboot实现推荐系统的协同过滤算法

《Springboot实现推荐系统的协同过滤算法》协同过滤算法是一种在推荐系统中广泛使用的算法,用于预测用户对物品(如商品、电影、音乐等)的偏好,从而实现个性化推荐,下面给大家介绍Springboot... 目录前言基本原理 算法分类 计算方法应用场景 代码实现 前言协同过滤算法(Collaborativ

openCV中KNN算法的实现

《openCV中KNN算法的实现》KNN算法是一种简单且常用的分类算法,本文主要介绍了openCV中KNN算法的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录KNN算法流程使用OpenCV实现KNNOpenCV 是一个开源的跨平台计算机视觉库,它提供了各

springboot+dubbo实现时间轮算法

《springboot+dubbo实现时间轮算法》时间轮是一种高效利用线程资源进行批量化调度的算法,本文主要介绍了springboot+dubbo实现时间轮算法,文中通过示例代码介绍的非常详细,对大家... 目录前言一、参数说明二、具体实现1、HashedwheelTimer2、createWheel3、n

SpringBoot实现MD5加盐算法的示例代码

《SpringBoot实现MD5加盐算法的示例代码》加盐算法是一种用于增强密码安全性的技术,本文主要介绍了SpringBoot实现MD5加盐算法的示例代码,文中通过示例代码介绍的非常详细,对大家的学习... 目录一、什么是加盐算法二、如何实现加盐算法2.1 加盐算法代码实现2.2 注册页面中进行密码加盐2.

Java时间轮调度算法的代码实现

《Java时间轮调度算法的代码实现》时间轮是一种高效的定时调度算法,主要用于管理延时任务或周期性任务,它通过一个环形数组(时间轮)和指针来实现,将大量定时任务分摊到固定的时间槽中,极大地降低了时间复杂... 目录1、简述2、时间轮的原理3. 时间轮的实现步骤3.1 定义时间槽3.2 定义时间轮3.3 使用时

如何通过Golang的container/list实现LRU缓存算法

《如何通过Golang的container/list实现LRU缓存算法》文章介绍了Go语言中container/list包实现的双向链表,并探讨了如何使用链表实现LRU缓存,LRU缓存通过维护一个双向... 目录力扣:146. LRU 缓存主要结构 List 和 Element常用方法1. 初始化链表2.

golang字符串匹配算法解读

《golang字符串匹配算法解读》文章介绍了字符串匹配算法的原理,特别是Knuth-Morris-Pratt(KMP)算法,该算法通过构建模式串的前缀表来减少匹配时的不必要的字符比较,从而提高效率,在... 目录简介KMP实现代码总结简介字符串匹配算法主要用于在一个较长的文本串中查找一个较短的字符串(称为

通俗易懂的Java常见限流算法具体实现

《通俗易懂的Java常见限流算法具体实现》:本文主要介绍Java常见限流算法具体实现的相关资料,包括漏桶算法、令牌桶算法、Nginx限流和Redis+Lua限流的实现原理和具体步骤,并比较了它们的... 目录一、漏桶算法1.漏桶算法的思想和原理2.具体实现二、令牌桶算法1.令牌桶算法流程:2.具体实现2.1

Python中的随机森林算法与实战

《Python中的随机森林算法与实战》本文详细介绍了随机森林算法,包括其原理、实现步骤、分类和回归案例,并讨论了其优点和缺点,通过面向对象编程实现了一个简单的随机森林模型,并应用于鸢尾花分类和波士顿房... 目录1、随机森林算法概述2、随机森林的原理3、实现步骤4、分类案例:使用随机森林预测鸢尾花品种4.1