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

相关文章

闲置电脑也能活出第二春?鲁大师AiNAS让你动动手指就能轻松部署

对于大多数人而言,在这个“数据爆炸”的时代或多或少都遇到过存储告急的情况,这使得“存储焦虑”不再是个别现象,而将会是随着软件的不断臃肿而越来越普遍的情况。从不少手机厂商都开始将存储上限提升至1TB可以见得,我们似乎正处在互联网信息飞速增长的阶段,对于存储的需求也将会不断扩大。对于苹果用户而言,这一问题愈发严峻,毕竟512GB和1TB版本的iPhone可不是人人都消费得起的,因此成熟的外置存储方案开

不懂推荐算法也能设计推荐系统

本文以商业化应用推荐为例,告诉我们不懂推荐算法的产品,也能从产品侧出发, 设计出一款不错的推荐系统。 相信很多新手产品,看到算法二字,多是懵圈的。 什么排序算法、最短路径等都是相对传统的算法(注:传统是指科班出身的产品都会接触过)。但对于推荐算法,多数产品对着网上搜到的资源,都会无从下手。特别当某些推荐算法 和 “AI”扯上关系后,更是加大了理解的难度。 但,不了解推荐算法,就无法做推荐系

康拓展开(hash算法中会用到)

康拓展开是一个全排列到一个自然数的双射(也就是某个全排列与某个自然数一一对应) 公式: X=a[n]*(n-1)!+a[n-1]*(n-2)!+...+a[i]*(i-1)!+...+a[1]*0! 其中,a[i]为整数,并且0<=a[i]<i,1<=i<=n。(a[i]在不同应用中的含义不同); 典型应用: 计算当前排列在所有由小到大全排列中的顺序,也就是说求当前排列是第

csu 1446 Problem J Modified LCS (扩展欧几里得算法的简单应用)

这是一道扩展欧几里得算法的简单应用题,这题是在湖南多校训练赛中队友ac的一道题,在比赛之后请教了队友,然后自己把它a掉 这也是自己独自做扩展欧几里得算法的题目 题意:把题意转变下就变成了:求d1*x - d2*y = f2 - f1的解,很明显用exgcd来解 下面介绍一下exgcd的一些知识点:求ax + by = c的解 一、首先求ax + by = gcd(a,b)的解 这个

综合安防管理平台LntonAIServer视频监控汇聚抖动检测算法优势

LntonAIServer视频质量诊断功能中的抖动检测是一个专门针对视频稳定性进行分析的功能。抖动通常是指视频帧之间的不必要运动,这种运动可能是由于摄像机的移动、传输中的错误或编解码问题导致的。抖动检测对于确保视频内容的平滑性和观看体验至关重要。 优势 1. 提高图像质量 - 清晰度提升:减少抖动,提高图像的清晰度和细节表现力,使得监控画面更加真实可信。 - 细节增强:在低光条件下,抖

【数据结构】——原来排序算法搞懂这些就行,轻松拿捏

前言:快速排序的实现最重要的是找基准值,下面让我们来了解如何实现找基准值 基准值的注释:在快排的过程中,每一次我们要取一个元素作为枢纽值,以这个数字来将序列划分为两部分。 在此我们采用三数取中法,也就是取左端、中间、右端三个数,然后进行排序,将中间数作为枢纽值。 快速排序实现主框架: //快速排序 void QuickSort(int* arr, int left, int rig

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

秋招最新大模型算法面试,熬夜都要肝完它

💥大家在面试大模型LLM这个板块的时候,不知道面试完会不会复盘、总结,做笔记的习惯,这份大模型算法岗面试八股笔记也帮助不少人拿到过offer ✨对于面试大模型算法工程师会有一定的帮助,都附有完整答案,熬夜也要看完,祝大家一臂之力 这份《大模型算法工程师面试题》已经上传CSDN,还有完整版的大模型 AI 学习资料,朋友们如果需要可以微信扫描下方CSDN官方认证二维码免费领取【保证100%免费

dp算法练习题【8】

不同二叉搜索树 96. 不同的二叉搜索树 给你一个整数 n ,求恰由 n 个节点组成且节点值从 1 到 n 互不相同的 二叉搜索树 有多少种?返回满足题意的二叉搜索树的种数。 示例 1: 输入:n = 3输出:5 示例 2: 输入:n = 1输出:1 class Solution {public int numTrees(int n) {int[] dp = new int

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯: