当前位置:网站首页>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);
}
}
}
边栏推荐
- Principles of microcomputer Chapter VIII notes arrangement
- Real topic of the 13th National Competition of single chip microcomputer in the Blue Bridge Cup
- 测试的重要性及目的
- Programmable data plane (paper reading)
- 硬件开发笔记(六): 硬件开发基本流程,制作一个USB转RS232的模块(五):创建USB封装库并关联原理图元器件
- 力扣每日一练之字符串Day6
- Chaos engineering, learn about it
- Not only Lei Jun, iqoo product manager, praised Qualcomm Xiaolong 8+: a new look
- 墨天轮访谈 | IvorySQL王志斌—IvorySQL,一个基于PostgreSQL的兼容Oracle的开源数据库
- [one by one series] identityserver4 (III) user name and password
猜你喜欢

技术分享| WVP+ZLMediaKit实现摄像头GB28181推流播放

基于微信小程序的婚纱影楼小程序开发笔记

函数的定义和函数的参数

I came from a major, so I didn't want to outsource

每日刷题记录 (二)

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

How to use the low code platform of the Internet of things for process management?

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

35岁危机?内卷成程序员代名词了…

Interpreting the 2022 agile coaching industry status report
随机推荐
Game asset reuse: a new way to find required game assets faster
Elastricearch's fragmentation principle of the second bullet
SAP实施项目上的内部顾问与外部顾问,相互为难还是相互成就?
A review of comparative learning
区块哈希竞猜游戏系统开发(dapp)
函數的定義和函數的參數
Design of hardware switch with programmable full function rate limiter
[one by one series] identityserver4 (IV) authorization code process
String Day6 of Li Kou daily practice
FlagAI飞智:AI基础模型开源项目,支持一键调用OPT等模型
Kotlin jetpack compose Tab的渲染 AnimatedVisibility的使用
Are there conditions for making new bonds? Is it safe to make new bonds
Helix QAC更新至2022.1版本,将持续提供高标准合规覆盖率
测试的重要性及目的
Zabbix监控- Aruba AP运行数据
20 provinces and cities announce the road map of the meta universe
解读2022年度敏捷教练行业现状报告
SQL联合查询(内联、左联、右联、全联)的语法
CV convolution neural network
打新债有条件吗 打新债安全吗

