Cracking the coding interview--Q5.7

Google面试之快乐智力题(Cracking the Coding Interview) 问:有20瓶药,其中19瓶装的都是1.0克的药片,只有1瓶装了1.1克的药。给你一个能称出具体克数的电子秤,只允许你称一次,怎么找出那瓶不一样的? 答:如果药片足够,从第i(1<i<20)个瓶子里取出数量为i的药片称重为Xg,[X-(21*20/2)]/0.1即为装了1.1g药的瓶子的编号。 Q:A bunch of men are on an island. A genie ... 阅读详情

原文:

An array A[1…n] contains all the integers from 0 to n except for one number which is missing. In this problem, we cannot access an entire integer in A with a single operation. The elements of A are represented in binary, and the only operation we can use to access them is “fetch the jth bit of A[i]”, which takes constant time. Write code to find the missing integer. Can you do it in O(n) time?

译文:

数组A[1…n]包含0到n的所有整数,但有一个整数丢失了。在这个问题中, 我们不能直接通过A[i]取得数组中的第i个数。数组A被表示成二进制, 也就是一串的0/1字符,而我们唯一能使用的操作只有“取得A[i]中的第j位”, 这个操作只需要花费常数时间。写程序找出丢失的整数,你能使程序的时间复杂度是O(n)吗?


题目中所述数组A被表示成二进制,也就是说数组A所有每个元素都是32位表示,不能直接通过A【i】来直接获取元素的值,

而唯一能使用的操作只有“取得A[i]中的第j位”,这是已知条件,可以利用这个条件来获取A【i】的32位每一位的数,这样也

就可以计算出数组A【i】的值。


package chapter_5_BitManipulation;

import java.util.Scanner;

/**
 * 
 * 数组A[1…n]包含0到n的所有整数,但有一个整数丢失了。在这个问题中, 我们不能直接通过A[i]取得数组中的第i个数。
 * 数组A被表示成二进制,
 * 也就是一串的0/1字符,而我们唯一能使用的操作只有“取得A[i]中的第j位”, 
 * 这个操作只需要花费常数时间。写程序找出丢失的整数,你能使程序的时间复杂度是O(n)吗?
 *
 */
public class Question_5_7 {
	
	/**
	 * @param a
	 * @param k
	 * @param i
	 * @return
	 * 
	 * 负责通过位运算获取数组k的二进制具体位上的值
	 * 
	 */
	public static int fetch(int a[], int k, int i) {
		int m = a[k];
		for(int j=0; j<i; j++) {
			m >>=1;
		}
		return m & 1;
	}
	
	/**
	 * @param a
	 * @param k
	 * @return
	 * 
	 * 负责获取数组A中第k下标的数
	 * 
	 */
	public static int getValue(int a[], int k){
		int sum = 0;
		int p = 1;
		int m = a[k];
		for(int i=31; i>=0; i--) {
			sum = (sum << 1) | fetch(a, k,  i);
		}
		return sum;
	}
	
	/**
	 * @param a
	 * @return
	 * 
	 * 找出缺失的元素值
	 * 
	 */
	public static int find_missing(int a[]) {
		boolean bool[] = new boolean[a.length + 1];
		for(int i=0; i<bool.length; i++) {
			bool[i] = false;
		}
		for(int i=0; i<a.length; i++) {
			bool[getValue(a, i)] = true;
		}
		int j = 0;
		for(; j<bool.length; j++) {
			if(bool[j] == false) {
				break;
			}
		}
		return j;
	}
	
	public static void main(String args[]) {
		int a[] = {0,1,2,3,5};
		int result = find_missing(a);
		System.out.println("缺少的是:" + result);
	}
}


FreeRTOS --(15)信号量之概述 FreeRTOS 中使用信号量来做同步,信号量可以在任务中使用作为任务与任务间的同步,也可以在中断中使用(带 FromISR 的版本)中断与任务间的同步; 针对不同的应用场景,信号量分为两种: 1、二值信号量; 2、计数信号量; 1、二值信号量 1.1、Usage 顾名思义,二值信号量只有两个值:0 和 1;它用于简单场景下的任务与任务、中断与任务之间的同步,比如:一个任务,在等待某个资源到位后,才能够继续执行,在得到这个资源之前,它处于阻塞状态,假如一个中断来了,给出了这个资源,那么. 阅读详情

相关推荐

Android P HAL层添加HIDL实例(详细实现步骤)

Android P HAL层添加HIDL实例 本文将介绍如何在P OS上添加HIDL实例,简单增加seLinux策略使得可以在system_service调用测试,并用模拟器emulator验证。 调用过程为 APP->TestManager->TestService->ITast.hal 文章目录Android P HAL层添加HIDL实例实现过程一 、hardware部分1...

sinat_18179367的博客 1万+

Cracking the coding interview--Q4

Chapter 4 | Trees and Graphs 4.1 Implement a function to check if a tree is balanced. For the purposes of this question, a balanced tree is defined to be a tree such that no two leaf nodes differ

763

Halcon实战:5分钟搞定线序颜色检测(附完整代码解析)

本文详细介绍了如何使用Halcon在5分钟内实现工业级线序颜色检测的高效方案。通过机器视觉技术,结合实战项目经验,提供完整的代码解析和优化技巧,帮助开发者快速构建鲁棒性强、准确率高的检测系统,适用于电子制造和汽车线束等行业。

weixin_29098367的博客 440

cracking-the-coding-interview的核心秘密:如何用单元测试“定义”面试题?C++ Catch+Python unittest的TDD完整教程

cracking-the-coding-interview 是一个为《Cracking the Coding Interview》(CTCI)第 6 版提供 **C++ 与 Python 双语言解法** 的开源项目,其最大特色是:**每一道面试题的解法都配有完整的自动化单元测试**(C++ 端用 Catch 框架,Python 端用 unittest 框架),自动化测试覆盖率超过 90%。更妙的是

gitblog_00501的博客 317

从USB相机标定到Aruco码精准定位:基于aruco-ros的机器人视觉实践

本文详细介绍了基于aruco-ros的机器人视觉实践,从USB相机标定到Aruco码精准定位的全流程。通过实战案例和优化技巧,展示了Aruco码在机器人导航中的高精度位姿识别能力,帮助开发者快速实现毫米级视觉定位系统。

weixin_27310417的博客 487

一文详解本地知识库RAG(检索增强):LangChain + Hugging Face 三件套 + 本地大模型 + MCP 协议 + Chroma + Agent 全流程实战

模型上下文协议(MCP)是 Anthropic 推出的开放标准,基于 JSON-RPC 2.0,为 AI 应用与外部工具/数据源之间提供统一的双向通信协议,被称为“AI 应用的 USB-C”。LangChain 是一个围绕 LLM、提示模板(Prompts)、记忆(Memory)、链(Chains)、工具(Tools)和代理(Agents)等组件打造的开源框架,旨在简化复杂自然语言应用的开发。:任意可调用的功能或服务(API、数据库查询、本地函数等),由 Agent 或 Chain 在推理过程中调用。

weixin_41645791的博客 1424

Cracking the coding interview--Q9

Chapter 9 | Sorting and Searching 9.1 You are given two sorted arrays, A and B, and A has a large enough buffer at the end to hold B. Write a method to merge B into A in sorted order. 插入排序,需要调整数

1020

Windows Pychram部署Unet++过程

windows系统 在pycharm平台上配置并跑通Unet网络 这里我用的是B站课程里的资源《唐宇迪Unet图像分割语义分割实战》

ArcGis_Niu的博客 803

Cracking the coding interview--Q17

Chapter 17 | Networking 17.1 Explain what happens, step by step, after you type a URL into a browser. Use as much detail as possible.  先进行DNS的namequery,得到对方的cname或者a记录(当然也可能是IPV6),反正就最后得到对方的IP

630

ArcGIS JS 态势标绘教程:直箭头(Straight Arrow)

ArcGIS JS 态势标绘教程:直箭头(Straight Arrow)。

非科班Java出身GISer的博客 700

G120变频器抱闸逻辑在提升机上应用

一、前言: 对于提升机、起重机、堆垛机、升降机等提升机构,负载为位能性负载,在设备启动和停止阶段,由于重物作用,可能会出现“溜车”现象、控制变频器与电机抱闸时许可以解决此问题 使用具有矢量控制功能的变频器 二、参数设置 抱闸控制时序: 启动: 1、变频器接收到启动命令时-----输出电流对电机进行励磁,经过P346电机励磁时间(==丹佛斯 设置启动电流延时0.5S)------达到一定的转矩 2、变频器输出开抱闸命令,经过P1216延时打开抱闸时间(==PLC延时控制打开抱闸) 注意:抱闸

fenglingxicao的博客 1万+

渣基础:比照Hawstein学Cracking the coding interview(1)

《C++ Primer 第五版》书实在是太长,太厚了。总是看了十几页就看累了,坚持不了多久,想了想还是别勉强自己,jued

低端玩家的记录地 949

手把手教你学simulink实例--基于Simulink的电动汽车动力总成控制策略验证与标定仿真平台

电动汽车的动力总成(Powertrain)是车辆的核心系统,包括动力电池、电机控制器、逆变器和驱动电机等关键部件。动力总成的控制策略直接影响车辆的动力性能、能耗效率和驾驶体验。为了验证和优化这些控制策略,基于MATLAB/Simulink的仿真平台成为一种高效且经济的解决方案。本项目旨在构建一个完整的基于Simulink的电动汽车动力总成控制策略验证与标定仿真平台,涵盖控制策略建模、性能评估以及参数标定等功能模块,并通过仿真验证系统的有效性。

MHD0815的博客 297

Cracking the coding interview--Q4.7

题目 原文: You have two very large binary trees: T1, with millions of nodes, and T2, with hundreds of nodes. Create an algorithm to decide if T2 is a subtree of T1. 译文: 有两个非常大的二叉树:T1,有上百万个结点,T2,有上百个

cfanr 1452

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

Cracking the coding interview--Q12.5

题目 原文: If you were designing a web crawler, how would you avoid getting into infinite loops? 译文: 如果你正在设计一个网络爬虫,何如避免进入无限循环? 解答

cfanr 1630

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

Cracking the coding interview--Q3.5

题目 原文: Implement a MyQueue class which implements a queue using two stacks. 译文: 用两个栈实现一个队列MyQueue。 解答 方法

cfanr 1448

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

Cracking the coding interview--Q2.5

Cracking the coding interview--Q2.5 December 17, 2012 作者:Hawstein 出处:http://hawstein.com/posts/2.5.html 声明:本文采用以下协议进行授权: 自由转载-非商用-非衍生-保持署名|Creative Commons BY-NC-ND 3.0 ,转载请注明作者及出处。 题目

pi9nc的专栏 817

Cracking the coding interview--Q9.3

题目 原文: Given a sorted array of n integers that has been rotated an unknown number of times, give an O(log n) algorithm that finds an element in the array. You may assume that the array was originall

cfanr 1620

Cracking the coding interview--Q19.10

题目 原文: Write a method to generate a random number between 1 and 7, given a method that generates a random number between 1 and 5 (i.e., implement rand7() using rand5()). 译文: 写一个方法

cfanr 1255

Cracking the coding interview--Q3.6

原文: Write a program to sort a stack in ascending order. You should not make any assumptions about how the stack is implemented. The following are the only functions that should be used to write thi

执子之手 560

程序员面试题目:Cracking the coding interview 分析与解答

Cracking the coding interview–Q11.2Cracking the coding interview–Q11.1Cracking the coding interview–Q10.6Cracking the coding interview–Q10.4Cracking the coding interview–Q10.2Cracking the

i 綠茶 1793

Cracking the coding interview--Q5

Chapter 5 | Bit Manipulation 5.1 You are given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set all bits between i and j in N equal to M (e.g., M becomes a subst

951

Cracking the coding interview--Q1.5

Cracking the coding interview--Q1.5 原文 Implement a method to perform basic string compression using the countsof repeated characters. For example, the string aabcccccaaa w...

weixin_34378045的博客 115

Cracking the coding interview--Q9.6

原文: Given a matrix in which each row and each column is sorted, write a method to find an element in it. 译文: 给出一个矩阵,其中每一行和每一列都是有序的,写一个函数在矩阵中找出指定的数。 矩阵是有序的,因此要利用这一点,当矩阵中元素和要查找的元素对比后,

执子之手 604

Cracking the coding interview--Q5.5

题目 原文: Write a function to determine the number of bits required to convert integer A to integer B. Input: 31, 14 Output: 2 译文: 写一个函数计算出将一个整数A转换到整数B需要修改多少个二进制位。 解答 首先要理解题目要求的是计算出整数A转换到整数B需要修

cfanr 1554
上一篇: Cracking the coding interview--Q5.6
下一篇: Cracking the coding interview--Q8.1
java_wliang
博客等级 码龄15年 19粉丝 82原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值