本文主要是介绍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)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!