682. Baseball Game

题目大意是给出我们要作为一个记分器,根据输入的字符串做出相应的加减分动作。其中普通的数字字符串给总分加上对应的分数,C代表上一个加分无效,D代表获得上一次加分的两倍分数,+代表获得上两次加分的和的分数。

思路是用一个栈来存储每一轮的分数,当普通加分时将分数加到总分里面后压入栈中;遇到+时弹出栈中的两个分数,相加后得到本轮分数,总分加上,然后依次把第二个弹出的分数、第一个弹出的分数、本轮分数压入栈中;遇到D时,弹出一个分数*2得到本轮分数,然后把上轮分数、本轮分数压入;遇到C时将上一轮分数弹出,总分减去上轮分数。代码如下:

class Solution {
    public int calPoints(String[] ops) {
		int sum = 0;
		Stack<Integer> recoder = new Stack<Integer>();
		for (int i = 0; i < ops.length; i++) {
			if (ops[i].equals("+")) {
				int t1 = recoder.pop();
				int t2 = recoder.pop();
				int tsum = t1 + t2;
				sum += tsum;
				recoder.push(t2);
				recoder.push(t1);
				recoder.push(tsum);
			} else if (ops[i].equals("D")) {
				int t = recoder.pop();
				int t1 = 2 * t;
				sum += t1;
				recoder.push(t);
				recoder.push(t1);
			} else if (ops[i].equals("C")) {
				int t = recoder.pop();
				sum -= t;
			} else {
				int t = Integer.parseInt(ops[i]);
				sum += t;
				recoder.push(t);
			}
		}
		return sum;
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以按照以下步骤使用`cv2.dnn.readNet`加载yolov5模型: 1. 下载yolov5模型权重文件和配置文件。 2. 加载权重文件和配置文件: ```python model_weights = "yolov5s.pt" model_config = "yolov5s.yaml" net = cv2.dnn.readNet(model_weights, model_config) ``` 3. 设置预测输入尺寸: ```python net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA) net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA) net.setPreferableLayout(cv2.dnn.DNN_TARGET_CUDA_FP16) net.setInputSize(640, 640) ``` 4. 设置类别标签: ```python class_labels = ["person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat", "traffic light", "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball", "kite", "baseball bat", "baseball glove", "skateboard", "surfboard", "tennis racket", "bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", "banana", "apple", "sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake", "chair", "couch", "potted plant", "bed", "dining table", "toilet", "tv", "laptop", "mouse", "remote", "keyboard", "cell phone", "microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors", "teddy bear", "hair drier", "toothbrush"] ``` 5. 进行预测: ```python image = cv2.imread("image.jpg") blob = cv2.dnn.blobFromImage(image, 1/255.0, (640, 640), swapRB=True, crop=False) net.setInput(blob) outputs = net.forward(net.getUnconnectedOutLayersNames()) ``` 6. 处理预测结果: ```python confidence_threshold = 0.5 nms_threshold = 0.4 for output in outputs: for detection in output: scores = detection[5:] class_id = np.argmax(scores) confidence = scores[class_id] if confidence > confidence_threshold: center_x = int(detection[0] * image.shape[1]) center_y = int(detection[1] * image.shape[0]) width = int(detection[2] * image.shape[1]) height = int(detection[3] * image.shape[0]) left = int(center_x - width / 2) top = int(center_y - height / 2) cv2.rectangle(image, (left, top), (left + width, top + height), (0, 255, 0), 2) cv2.putText(image, class_labels[class_id], (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) ``` 以上代码仅为示例,具体实现可能需要根据自己的需求进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值