给出一个数组arr,我们希望它通过增加arr中的所有重复元素使其唯一,从而使arr unique的元素之和最小。换句话说,如果arr中的两个或多个元素不是唯一的,则我们必须将重复元素的值增加到其他一些数字,以使arr包含的唯一元素的和等于一个小数字尽可能。
例如,如果arr = [3,2,1,2,7],则arr unique = [3,2,1,4,7],其元素的总和为3 + 2 + 1的最小值+ 4 + 7 = 17。
我的方法。没有执行
[
{
"Id": "868b7935cc371a0eef47e84a7ffbddb99b03cfc93e735af31e5b5754680c1f98",
"Created": "2018-11-15T20:11:44.9362404Z",
"Path": "docker-entrypoint.sh",
"Args": [
"mysqld"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 4819,
"ExitCode": 0,
"Error": "",
"StartedAt": "2018-11-23T23:15:52.5735445Z",
"FinishedAt": "2018-11-21T01:52:31.3103165Z"
},
"Image": "sha256:583a6e3a3c98793a6c8a3b09d291b574da66f7e1fba6ebfebe3e93c88c3b443a",
"ResolvConfPath": "/var/lib/docker/containers/868b7935cc371a0eef47e84a7ffbddb99b03cfc93e735af31e5b5754680c1f98/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/868b7935cc371a0eef47e84a7ffbddb99b03cfc93e735af31e5b5754680c1f98/hostname",
"HostsPath": "/var/lib/docker/containers/868b7935cc371a0eef47e84a7ffbddb99b03cfc93e735af31e5b5754680c1f98/hosts",
"LogPath": "/var/lib/docker/containers/868b7935cc371a0eef47e84a7ffbddb99b03cfc93e735af31e5b5754680c1f98/868b7935cc371a0eef47e84a7ffbddb99b03cfc93e735af31e5b5754680c1f98-json.log",
"Name": "/dmr_mysql_1",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": [
"dmr_local_mysql_data:/var/lib/mysql:rw",
"dmr_local_mysql_data_backups:/backups:rw"
],
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "dmr_default",
"PortBindings": {
"3306/tcp": [
{
"HostIp": "",
"HostPort": "3306"
}
]
},
"RestartPolicy": {
"Name": "",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": [],
"CapAdd": null,
"CapDrop": null,
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "shareable",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": null,
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": null,
"DeviceCgroupRules": null,
"DiskQuota": 0,
"KernelMemory": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": 0,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0,
"MaskedPaths": [
"/proc/acpi",
"/proc/kcore",
"/proc/keys",
"/proc/latency_stats",
"/proc/timer_list",
"/proc/timer_stats",
"/proc/sched_debug",
"/proc/scsi",
"/sys/firmware"
],
"ReadonlyPaths": [
"/proc/asound",
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/830398a5558d1451a520a7219971cfb6f869cfc7aa149373eab77287c2924ee4-init/diff:/var/lib/docker/overlay2/d6fbcced29e35b61a9bb5a8db9cec8c561fdcba5b52a61c62af886de180aa93a/diff:/var/lib/docker/overlay2/289d826020070599fe59d4171f40bfcfc41de1bbefa29bcc4cfd0bc0ab5ebb3c/diff:/var/lib/docker/overlay2/05572289cc7498d3d29d09d0b9745c0387c56ef06919ef27517c9131a585a895/diff:/var/lib/docker/overlay2/eed1357572b7a67729f776846e8109fa9493e0083d88bb3edeb2c95410bfa2b4/diff:/var/lib/docker/overlay2/8163d89b53f562d5476ffc8ccabdcb6a935ee932b2544f0d42ada9650b67eb46/diff:/var/lib/docker/overlay2/8ea425a1f09814f6e6f3f9d8f887c0829b2151e359425ea985792a75e65acd90/diff:/var/lib/docker/overlay2/ae06aa0cbb069d340970beb76ad8b278ac4b4f97eaceb1f3b36cb4ba15a2128c/diff:/var/lib/docker/overlay2/16350f1b36b1eb496286e5ad4cdea02f9931d33a6869a6105da766e40793d81a/diff:/var/lib/docker/overlay2/305da8336df57edf64806244981141bd6a05b168653a48f97223e7da0a3ac477/diff:/var/lib/docker/overlay2/2265f0da439e923b98007d292dda922f3a90298bb879c07f2f41afa66c971c7b/diff:/var/lib/docker/overlay2/b5e59e46468f95a1d243b6c99b7421b41715f7ad11bda4095901244a6552bbb9/diff:/var/lib/docker/overlay2/76fdb756320d579aed7713e27b4760a5266fcfde5358903d9e4351d9c77a4b9d/diff:/var/lib/docker/overlay2/58952f226dee428fecc6cf23f45e39b4084f10c6214f3ded03ebd87a250318bd/diff:/var/lib/docker/overlay2/7f03ca1e222e9ee48d8332e6ec830cb0a2a7a27167d2698847d41d3f18c47bd3/diff",
"MergedDir": "/var/lib/docker/overlay2/830398a5558d1451a520a7219971cfb6f869cfc7aa149373eab77287c2924ee4/merged",
"UpperDir": "/var/lib/docker/overlay2/830398a5558d1451a520a7219971cfb6f869cfc7aa149373eab77287c2924ee4/diff",
"WorkDir": "/var/lib/docker/overlay2/830398a5558d1451a520a7219971cfb6f869cfc7aa149373eab77287c2924ee4/work"
},
"Name": "overlay2"
},
"Mounts": [
{
"Type": "volume",
"Name": "dmr_local_mysql_data_backups",
"Source": "/var/lib/docker/volumes/dmr_local_mysql_data_backups/_data",
"Destination": "/backups",
"Driver": "local",
"Mode": "rw",
"RW": true,
"Propagation": ""
},
{
"Type": "volume",
"Name": "dmr_local_mysql_data",
"Source": "/var/lib/docker/volumes/dmr_local_mysql_data/_data",
"Destination": "/var/lib/mysql",
"Driver": "local",
"Mode": "rw",
"RW": true,
"Propagation": ""
}
],
"Config": {
"Hostname": "868b7935cc37",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"3306/tcp": {},
"33060/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"MYSQL_DATABASE=dmr",
"MYSQL_USER=dmr",
"MYSQL_PASSWORD=dmr",
"MYSQL_ROOT_PASSWORD=dmr",
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"GOSU_VERSION=1.7",
"MYSQL_MAJOR=5.7",
"MYSQL_VERSION=5.7.24-1debian9"
],
"Cmd": [
"mysqld"
],
"ArgsEscaped": true,
"Image": "dmr_mysql",
"Volumes": {
"/backups": {},
"/var/lib/mysql": {}
},
"WorkingDir": "",
"Entrypoint": [
"docker-entrypoint.sh"
],
"OnBuild": null,
"Labels": {
"com.docker.compose.config-hash": "ffc27388c47a8468694fe5412bb06e3dda7a7b083d378fba1ab57eace2b3628e",
"com.docker.compose.container-number": "1",
"com.docker.compose.oneoff": "False",
"com.docker.compose.project": "dmr",
"com.docker.compose.service": "mysql",
"com.docker.compose.version": "1.22.0"
}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "cbccae535b05d954c1592710bb808814a87bcfbee6617fd1fb0a8f44561faec7",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"3306/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "3306"
}
],
"33060/tcp": null
},
"SandboxKey": "/var/run/docker/netns/cbccae535b05",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks": {
"dmr_default": {
"IPAMConfig": null,
"Links": null,
"Aliases": [
"868b7935cc37",
"mysql"
],
"NetworkID": "eaf16cd4854d6bcb607ca7598c5337d42b917164404de82f873b9567ab480df7",
"EndpointID": "c98e525c7c4f22e5bfb7b6041a2f94fbf81561f518d2a6b550768ef6c32e57d5",
"Gateway": "172.18.0.1",
"IPAddress": "172.18.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:12:00:02",
"DriverOpts": null
}
}
}
}
答案 0 :(得分:1)
您可以遍历列表,跟踪涵盖的项目以查找任何重复项,并将其替换为可能的最小整数:
import sys
lst = [3, 2, 1, 2, 7]
visited = []
for i, x in enumerate(lst):
if x in visited:
lst[i] = next(a for a in range(1, sys.maxsize) if a not in visited)
visited.append(lst[i])
else:
visited.append(x)
print(lst)
# [3, 2, 1, 4, 7]
答案 1 :(得分:0)
不如上述优雅,但发布是因为我很乐意尝试解决此问题
def incDup(list):
dict = {}
j = 0
while j < len(list):
if list[j] in dict: # if value has been seen, we keep increasing it
list[j] += 1
else: # first time seeing value
dict[list[j]] = 1
j += 1
print(list)
incDup([3, 2, 1, 2, 7]) #[3, 2, 1, 4, 7]
incDup([3, 2, 2, 7, 7]) #[3, 2, 4, 7, 8]