1089. Duplicate Zeros

24 篇文章 0 订阅

Given a fixed length array arr of integers, duplicate each occurrence of zero, shifting the remaining elements to the right.

Note that elements beyond the length of the original array are not written.

Do the above modifications to the input array in place, do not return anything from your function.

复制数组中的0,前提是不开辟新的数组

解题:用的是动态数组vector,主要考察这个数组的用法

class Solution {
public:
    void duplicateZeros(vector<int>& arr) {
        
        for(int i=0;i<arr.size()-1;i++){
            if(arr[i]!=0)
                continue;
            for(int j=arr.size()-1;j>i;j--){
                arr[j]=arr[j-1];
            }
            i++;

        }
    }
};

import numpy as np import pandas as pd import matplotlib.pyplot as plt import PIL import torch from torchvision import transforms import torchvision #调用已经训练好的FCN语义分割网络 model = torchvision.models.segmentation.fcn_resnet101(pretrained=True) model.eval() #读取照片 image=PIL.Image.open('1234.jpg') #照片进行预处理 image_transf=transforms.Compose([ transforms.ToTensor(), transforms.Normalize(mean=[0.485,0.456,0.406], std=[0.229,0.224,0.225]) ] ) image_tensor=image_transf(image).unsqueeze(0) output=model(image_tensor)['out'] output.shape #读取图片,进行分割,总共21个通道,因为在21个数据集上训练 #转化为2维图像 outputarg=torch.argmax(output.squeeze(),dim=0).numpy() outputarg def decode_seqmaps(image,label_colors,nc=21): r=np.zeros_like(image).astype(np.uint8) g=np.zeros_like(image).astype(np.uint8) b=np.zeros_like(image).astype(np.uint8) for cla in range(0,nc): idx = image == cla r[idx] = label_colors[cla,0] g[idx] = label_colors[cla,1] b[idx] = label_colors[cla,2] rgbimage= np.stack([r,g,b],axis=2) return rgbimage import os os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE" label_colors =np.array([(0,0,0), #0=background (128,0,0),(0,128,0),(128,128,0),(0,0,128), #1=airplane,2=bicycle,3=bird,4=boat (128,0,128),(0,128,128),(128,128,128),(64,0,0), #6=bus,7=car,8=cat,9=chair (192,0,0),(64,128,0),(192,128,0),(64,0,128), #10=cow,11=dining table,12=dog,13=horse (192,0,128),(64,128,128),(192,128,128),(0,64,0), #14=motorbike,15=person,16=potted plant,17=sheep (128,64,0),(0,192,0),(128,192,0),(0,64,128) #18=sofa,19=train,20=tv/monitor ]) outputrgb=decode_seqmaps(outputarg,label_colors) plt.figure(figsize=(20,8)) plt.subplot(1,2,1) plt.imshow(image) plt.axis('off') plt.subplot(1,2,2) plt.imshow(outputrgb) plt.axis('off') plt.subplots_adjust(wspace=0.05) plt.show()使用了哪些深度学习的模型和方法
04-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值