685 - First Unique Number In Stream

本文介绍了一种使用LinkedHashMap来解决寻找数组中第一个非唯一出现的数字的问题的方法。该方法适用于需要保持元素插入顺序的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2017.9.29

这个题目是要求输出第一个非唯一的元素。如果直接用HashMap的话会失去顺序,所以需要使用LinkedHashMap。

public class Solution {
    /*
     * @param : a continuous stream of numbers
     * @param : a number
     * @return: returns the first unique number
     */
  	public int firstUniqueNumber(int[] nums, int number) {
        // Write your code here
		if(nums == null){
			return -1;
		}
		int res = -1;
		LinkedHashMap<Integer,Integer> map = new LinkedHashMap<>();
		boolean find = false;
		for(int i = 0; i < nums.length; i++){
			if(nums[i] == number){
				find = true;
				break;
			}
			if(!map.containsKey(nums[i])){
				map.put(nums[i], 1);
			}
			else{
				map.put(nums[i], map.get(nums[i]) + 1);
			}
		}
		if(find == false){
			return res;
		}
		Iterator ite = map.keySet().iterator();
		while(ite.hasNext()){
			int tmp = (int)ite.next();
			if(map.get(tmp) == 1){
				res = tmp;
				break;
			}
		}
		return res;
    }
};


bool ExternalCameraOfflineSession::OutputThread::threadLoop() { auto parent = mParent.promote(); if (parent == nullptr) { ALOGE("%s: session has been disconnected!", __FUNCTION__); return false; } if (mOfflineReqs.empty()) { ALOGI("%s: all offline requests are processed. Stopping.", __FUNCTION__); return false; } std::shared_ptr<HalRequest> req = mOfflineReqs.front(); mOfflineReqs.pop_front(); auto onDeviceError = [&](auto... args) { ALOGE(args...); parent->notifyError( req->frameNumber, /*stream*/-1, ErrorCode::ERROR_DEVICE); signalRequestDone(); return false; }; if (req->frameIn->mFourcc != V4L2_PIX_FMT_MJPEG && req->frameIn->mFourcc != V4L2_PIX_FMT_Z16) { return onDeviceError("%s: do not support V4L2 format %c%c%c%c", __FUNCTION__, req->frameIn->mFourcc & 0xFF, (req->frameIn->mFourcc >> 8) & 0xFF, (req->frameIn->mFourcc >> 16) & 0xFF, (req->frameIn->mFourcc >> 24) & 0xFF); } int res = requestBufferStart(req->buffers); if (res != 0) { ALOGE("%s: send BufferRequest failed! res %d", __FUNCTION__, res); return onDeviceError("%s: failed to send buffer request!", __FUNCTION__); } std::unique_lock<std::mutex> lk(mBufferLock); // Convert input V4L2 frame to YU12 of the same size // TODO: see if we can save some computation by converting to YV12 here uint8_t* inData; size_t inDataSize; if (req->frameIn->getData(&inData, &inDataSize) != 0) { lk.unlock(); return onDeviceError("%s: V4L2 buffer map failed", __FUNCTION__); } // TODO: in some special case maybe we can decode jpg directly to gralloc output? if (req->frameIn->mFourcc == V4L2_PIX_FMT_MJPEG) { ATRACE_BEGIN("MJPGtoI420"); int res = libyuv::MJPGToI420( inData, inDataSize, static_cast<uint8_t*>(mYu12FrameLayout.y), mYu12FrameLayout.yStride, static_cast<uint8_t*>(mYu12FrameLayout.cb), mYu12FrameLayout.cStride, static_cast<uint8_t*>(mYu12FrameLayout.cr), mYu12FrameLayout.cStride, mYu12Frame->mWidth, mYu12Frame->mHeight, mYu12Frame->mWidth, mYu12Frame->mHeight); ATRACE_END(); if (res != 0) { // For some webcam, the first few V4L2 frames might be malformed... ALOGE("%s: Convert V4L2 frame to YU12 failed! res %d", __FUNCTION__, res); lk.unlock(); Status st = parent->processCaptureRequestError(req); if (st != Status::OK) { return onDeviceError("%s: failed to process capture request error!", __FUNCTION__); } signalRequestDone(); return true; } } ATRACE_BEGIN("Wait for BufferRequest done"); res = waitForBufferRequestDone(&req->buffers); ATRACE_END(); if (res != 0) { ALOGE("%s: wait for BufferRequest done failed! res %d", __FUNCTION__, res); lk.unlock(); return onDeviceError("%s: failed to process buffer request error!", __FUNCTION__); } ALOGV("%s processing new request", __FUNCTION__); const int kSyncWaitTimeoutMs = 500; for (auto& halBuf : req->buffers) { if (*(halBuf.bufPtr) == nullptr) { ALOGW("%s: buffer for stream %d missing", __FUNCTION__, halBuf.streamId); halBuf.fenceTimeout = true; } else if (halBuf.acquireFence >= 0) { int ret = sync_wait(halBuf.acquireFence, kSyncWaitTimeoutMs); if (ret) { halBuf.fenceTimeout = true; } else { ::close(halBuf.acquireFence); halBuf.acquireFence = -1; } } if (halBuf.fenceTimeout) { continue; } // Gralloc lockYCbCr the buffer switch (halBuf.format) { case PixelFormat::BLOB: { int ret = createJpegLocked(halBuf, req->setting); if(ret != 0) { lk.unlock(); return onDeviceError("%s: createJpegLocked failed with %d", __FUNCTION__, ret); } } break; case PixelFormat::Y16: { void* outLayout = sHandleImporter.lock(*(halBuf.bufPtr), halBuf.usage, inDataSize); CID 146246: (#1 of 1): 解引用 null 返回值 (NULL_RETURNS) 23. dereference: Dereferencing a pointer that might be nullptr outLayout when calling memcpy.[注意:函数的源代码实现已被内置模型覆盖。] std::memcpy(outLayout, inData, inDataSize); int relFence = sHandleImporter.unlock(*(halBuf.bufPtr)); if (relFence >= 0) { halBuf.acquireFence = relFence; } } break;
07-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值