当前位置:网站首页>Daily question brushing record (II)
Daily question brushing record (II)
2022-06-23 19:43:00 【Unique Hami melon】
List of articles
The first question is : Binary number to string
LeetCode: Interview questions 05.02. Binary number to string
describe :
Binary number to string . Given a value between 0 and 1 The real number between ( Such as 0.72), The type is double, Print its binary expression . If the number cannot be accurately used 32 Binary representation within bits , Then print “ERROR”.

Their thinking :
It is preferred to know the method of converting decimal to binary ,
Decimal to binary , It's all the time*2, Then extract the integer part , Until 0
for example : 0.6250.625 * 2 = 1.25 => 1 0.25 * 2 = 0.5 => 0 0.5 * 2 = 1.0 => 1obtain 101
Notice the ERROR , When always *2, The data obtained exceeds 32 position , Namely ERROR
Code implementation :
class Solution {
public String printBin(double num) {
// Here is the initialization
StringBuilder sb = new StringBuilder("0.");
while(num != 0){
// *2 To get the results
num = num * 2;
// Get this integer number , To add to sb in
sb.append(num - 1 >=0 ? 1 : 0);
// Remove the integer part
num = num % 1;
// Determine whether the current exceeds 32 position
if(sb.length() > 32) return "ERROR";
}
return sb.toString();
}
}
The second question is : Pairing exchange
LeetCode: Interview questions 05.07. Pairing exchange
describe :
Pairing exchange . Programming , Exchange the odd and even digits of an integer , Use as few instructions as possible ( in other words , position 0 And bit 1 In exchange for , position 2 And bit 3 In exchange for , And so on ).
Their thinking :
send Odd bits shift left , Even bits shift right , then Both or operations Get results
Give WayOdd numberAnd0x55555555( Odd bits are 1, Even digits are 0) And operation Move left
Give WayEven digitAnd0xaaaaaaaa( Odd bits are 0, Even digits are 1) And operation Move right
Let the result of bothOr operations
Code implementation :
class Solution {
public int exchangeBits(int num) {
int x = (num & 0x55555555) << 1;
int y = (num & 0xaaaaaaaa) >> 1;
return x|y;
}
}
Third question : Integer conversion
LeetCode: Interview questions 05.06. Integer conversion
describe :
Integer conversion . Write a function , Determine how many bits need to be changed to change the integer A Converted to an integer B.
Their thinking :
Here is how many digits of these two numbers are different
- First let's A and B Xor operation Get the binary of the number 1 The number of is the number of different digits
- To judge this number 1 The number of .
Here you can useC = C & (C-1)until C=0, There are several cycles here , Namely 1 The number of
Code implementation :
class Solution {
public int convertInteger(int A, int B) {
// XOR gets this number
int tmp = A ^ B;
int count = 0;
// Cycle a few times It's a bit 1 The number of
while(tmp!=0){
tmp = tmp & (tmp-1);
count++;
}
return count;
}
}
Fourth question : Magic index
LeetCode : Interview questions 08.03. Magic index
describe :
Magic index . In the array A[0…n-1] in , There's a magic index , Meet the conditions A[i] = i. Given an ordered array of integers , Write a way to find magic index , If any , In the array A Find a magic index in , without , Then return to -1. If there are multiple magic indexes , Returns the smallest index value .

Their thinking :
Here you can directly traverse , See if the subscript matches the value , After traversal, it returns -1;
Optimize :
Because the title says that it is an ordered integer array , You can jump and traverse directly .
index = Math.max( index+1, nums[index])
Code implementation :
class Solution {
public int findMagicIndex(int[] nums) {
int index = 0;
while(index < nums.length){
if(index == nums[index]){
return index;
}
// Jump here
index = Math.max(index+1,nums[index]);
}
return -1;
}
}
Fifth question : Permutations of non repeating strings
LeetCode : Interview questions 08.07. Permutations of non repeating strings
describe :
Permutations of non repeating strings . Write a method , Calculates all permutations of a string , Each character of the string is different 
Their thinking :
Use backtracking solutions , bfs,
Pay attention to the pruning here , You cannot reuse the same , Use one boolean Array to prune
Because this question is a non repeating string , There is no need to consider having multiple identical characters
Code implementation :
class Solution {
private List<String> list = new ArrayList<>();
public String[] permutation(String S) {
boolean[] tmp = new boolean[S.length()];
StringBuilder sb = new StringBuilder();
backstrack(S,tmp,0,sb);
String[] res = new String[list.size()];
for (int i = 0; i < list.size(); i++) {
res[i] = list.get(i);
}
return res;
}
public void backstrack(String s, boolean[] tmp, int start,StringBuilder sb) {
if (start == s.length()) {
list.add(sb.toString());
return;
}
for(int i = 0; i < s.length(); i++) {
if(!tmp[i]){
tmp[i] = true;
sb.append(s.charAt(i));
backstrack(s,tmp,start+1,sb);
tmp[i] = false;
sb.deleteCharAt(start);
}
}
}
}
Sixth question : There are permutations and combinations of repeated strings
LeetCode : Interview questions 08.08. There are permutations and combinations of repeated strings
There are permutations and combinations of repeated strings . Write a method , Calculates all permutations of a string .

Their thinking :
Same as the previous question , The pruning here is not just the one that reduces reuse
Still need pruning , Same characters .
for example :
- there q Minus Branches of the same character
- here qq and ee Minus Reusable branches
Code implementation :
class Solution {
private List<String> list = new ArrayList<>();
private boolean[] tmp = new boolean[10];
public String[] permutation(String S) {
StringBuilder sb = new StringBuilder();
char[] arr = S.toCharArray();
Arrays.sort(arr);
backstrack(arr,sb,0);
String[] res = new String[list.size()];
for (int i = 0; i < list.size(); i++) {
res[i] = list.get(i);
}
return res;
}
public void backstrack(char[] arr,StringBuilder sb, int start) {
if(start == arr.length){
list.add(sb.toString());
return;
}
for(int i = 0; i < arr.length; i++) {
if(i>0 && arr[i] == arr[i-1] && !tmp[i-1]){
continue;
}
if(tmp[i]) {
continue;
}
tmp[i] = true;
sb.append(arr[i]);
backstrack(arr,sb,start+1);
tmp[i] = false;
sb.deleteCharAt(start);
}
}
}
边栏推荐
- How to use the low code platform of the Internet of things for process management?
- How to avoid the "black swan" incident in the gene field: a security war behind a preventive "recall"
- 解读2022年度敏捷教练行业现状报告
- Netcf summary
- 直播分享| 腾讯云 MongoDB 智能诊断及性能优化实践
- 盘点四种WiFi加密标准:WEP、WPA、WPA2、WPA3
- Netseer: stream event telemetry notes for programmable data plane
- Interpreting the 2022 agile coaching industry status report
- RStudio 1.4软件安装包和安装教程
- JDBC 在性能测试中的应用
猜你喜欢

区块哈希竞猜游戏系统开发(dapp)

Helix QAC更新至2022.1版本,将持续提供高标准合规覆盖率

5 月最大的 GameFi 崩溃受害者能否在熊市中生存?| May Monthly Report

直播分享| 腾讯云 MongoDB 智能诊断及性能优化实践

Hardware development notes (6): basic process of hardware development, making a USB to RS232 module (5): creating USB package library and associating principle graphic devices

Development of block hash quiz game system (DAPP)

Shunted self attention | vit method for solving small target problems, which is derived from PVT and higher than PVT

Principles of microcomputer Chapter VIII notes arrangement

在线文本实体抽取能力,助力应用解析海量文本数据

Elastricearch's fragmentation principle of the second bullet
随机推荐
解读2022年度敏捷教练行业现状报告
活动报名 | MongoDB 5.0 时序存储特性介绍
Interview with Mo Tianlun | ivorysql wangzhibin - ivorysql, an Oracle compatible open source database based on PostgreSQL
Goldfish rhca memoirs: do447 managing user and team access -- effectively managing users with teams
直播回顾 | 云原生混部系统 Koordinator 架构详解(附完整PPT)
Deeply understand and grasp the basic characteristics of digital economy
I came from a major, so I didn't want to outsource
5 月最大的 GameFi 崩溃受害者能否在熊市中生存?| May Monthly Report
CV background introduction
如何在Microsoft Exchange 2010中安装SSL证书
墨天轮访谈 | IvorySQL王志斌—IvorySQL,一个基于PostgreSQL的兼容Oracle的开源数据库
@@脚本实现Ishell自动部署
每日刷题记录 (二)
Pisces: a programmable, protocol independent software switch (summary)
vs2022scanf函数的使用,使用scanf的报错-返回值被忽略:解决·方法
Naacl 2022 finds | byte proposes MTG: multilingual text generation data set
打新债有条件吗 打新债安全吗
Hardware development notes (6): basic process of hardware development, making a USB to RS232 module (5): creating USB package library and associating principle graphic devices
金鱼哥RHCA回忆录:DO447管理用户和团队的访问--用团队有效地管理用户
函數的定義和函數的參數

