经典题目刷一刷

目录

1.另类加法

2.密码强度等级

3.最近公共祖先

4.求最大的bit 数


1.另类加法

另类加法_牛客题霸_牛客网【牛客题霸】收集各企业高频校招笔面试题目,配有官方题解,在线进行百度阿里腾讯网易等互联网名企笔试面试模拟考试练习,和牛人一起讨论经典试题,全面提升你的技术能力https://www.nowcoder.com/practice/e7e0d226f1e84ba7ab8b28efc6e1aebc?tpId=8&&tqId=11065&rp=1&ru=/activity/oj&qru=/ta/cracking-the-coding-interview/question-ranking

①题目及示例:

②方法解析:

我们由题意知道,我们要利用非运算符来完成运算,我们就很容易想到用位运算符来计算 。

而我们在计算前需要知道的是:

a.二进制位进行异或运算的结果是两个数对应相加的结果(不考虑进位情况)

b.二进制位按位与再左移一位的结果是两个数进位后的结果(只考虑进位情况)

所以基于上述结论,我们可以做如下的图来结合理解:

③代码如下:

import java.util.*;
public class UnusualAdd {
    public int addAB(int A, int B) {
        // write code here
        if(B==0){//要是B为0,则不需要进位,直接可以返回A的值
            return A;
        }
        int sum=0;
        int tmp=0;
        while(B!=0){//即需要进位的时候
            sum=A^B;//将两数相加(不判断进位)
            tmp=(A&B)<<1;//判断进几位
            A=sum;
            B=tmp;
        }//出循环后,然后B=0了,即不需要进位,此时的和就是A的值
        return A;
    }
}

2.密码强度等级

密码强度等级_牛客题霸_牛客网【牛客题霸】收集各企业高频校招笔面试题目,配有官方题解,在线进行百度阿里腾讯网易等互联网名企笔试面试模拟考试练习,和牛人一起讨论经典试题,全面提升你的技术能力https://www.nowcoder.com/practice/52d382c2a7164767bca2064c1c9d5361?tpId=37&&tqId=21310&rp=1&ru=/activity/oj&qru=/ta/huawei/question-ranking

①题目示例:

 代码如下:(这个题没有太多的难点,就是比较麻烦,直接附上代码)

import java.util.*;
 public class Main { 
public static void main(String[] args){ 
Scanner sc=new Scanner(System.in);
 while(sc.hasNextLine()){
 String str=sc.nextLine(); 
int sum1=getLen(str);
 int sum2=getChar(str);
 int sum3=getNum(str); 
int sum4=getSym(str); 
int sum=0;
 if(sum2==20&&sum3>=1&&sum4>=1){
 sum=sum1+sum2+sum3+sum4+5;
 }else if(sum2==10&&sum3>=1&&sum4>=1){
 sum=sum1+sum2+sum3+sum4+3; 
}else if(sum2==10&&sum3>=1&&sum4==0){ 
sum=sum1+sum2+sum3+sum4+2; 
}else{
sum=sum1+sum2+sum3+sum4; 
}if(sum>=90){
 System.out.println("VERY_SECURE");
 }else if(sum>=80){
 System.out.println("SECURE"); 
}else if(sum>=70){ 
System.out.println("VERY_STRONG"); 
}else if(sum>=60){ 
System.out.println("STRONG"); 
}else if(sum>=50){
System.out.println("AVERAGE");
 }else if(sum>=25){
 System.out.println("WEAK");
 }else if(sum>=0){
 System.out.println("VERY_WEAK");
 } 
} 
}public static int getLen(String str){ 
if(str.length()<=4){
 return 5; 
}else if(7>=str.length()&&str.length()>=5){ 
return 10;
 }else if(str.length()>=8){ 
return 25;
 }return 0;
 }public static int getChar(String str){ 
int small=0; int big=0; for(int i=0;i<str.length();i++){ if(str.charAt(i)>=65&&str.charAt(i)<=90){
 big++;
 }else if(str.charAt(i)>=97&&str.charAt(i)<=122){ 
small++;
 } 
}if(small>0&&big>0){ 
return 20; 
}else if(small>0||big>0){
 return 10; 
}else{
return 0;
 }
 }public static int getNum(String str){ 
int num=0; 
for(int i=0;i<str.length();i++){ 
if(str.charAt(i)-'0'>=0 && str.charAt(i)-'0'<=9){
 num++;
 } 
}if(num>1){ 
return 20;
 }else if(num==1){
 return 10; 
}else{
return 0;
 } 
}public static int getSym(String str){
 int num=0; 
for(int i=0;i<str.length();i++){
 if(!(str.charAt(i)>=65&&str.charAt(i)<=90)&& !(str.charAt(i)>=97&&str.charAt(i)<=122)&& !(str.charAt(i)-'0'>=0&&str.charAt(i)-'0'<=9)){
num++; 
}
 }if(num>1){
 return 25;
 }else if(num==1){
 return 10; 
}else{
return 0; 
}
 } 
}

3.最近公共祖先

最近公共祖先_牛客题霸_牛客网【牛客题霸】收集各企业高频校招笔面试题目,配有官方题解,在线进行百度阿里腾讯网易等互联网名企笔试面试模拟考试练习,和牛人一起讨论经典试题,全面提升你的技术能力https://www.nowcoder.com/practice/70e00e490b454006976c1fdf47f155d9?tpId=8&&tqId=11017&rp=1&ru=/activity/oj&qru=/ta/cracking-the-coding-interview/question-ranking

①题目及示例:

 ②方法解析: 

由题意,同时我们能够很清楚地知道子节点与其父亲节点的对应关系。

其父节点=左子树节点/2=右子树节点/2;

我们也就可以知道,通过这种方式,我们可以一次次找到该子节点的父节点,等最后,两子树相等时,我们就认为它是最近的公共祖先。下面进行图例说明:

③代码如下:

import java.util.*;
public class LCA {
    public int getLCA(int a, int b) {
        // write code here
        while(a!=b){
            if(a>b){
                a/=2;
            }else{
                b/=2;
            }
        }
        return a;
    }
}

4.求最大的bit 数

求最大连续bit数_牛客题霸_牛客网【牛客题霸】收集各企业高频校招笔面试题目,配有官方题解,在线进行百度阿里腾讯网易等互联网名企笔试面试模拟考试练习,和牛人一起讨论经典试题,全面提升你的技术能力https://www.nowcoder.com/practice/4b1658fd8ffb4217bc3b7e85a38cfaf2?tpId=37&&tqId=21309&rp=1&ru=/activity/oj&qru=/ta/huawei/question-ranking

①题目及示例:

②方法解析:

我们很容易理解题意,但是我们要知道,每次出现1的个数都可能是不同的,而多少也是不确定的,所以我们定义两个变量,通过来比较他俩间的最大值来获取到要求的。与此同时还有一个问题,如何来判断1的个数呢?我们这里采用&上1来判断最后一位,是1那么count++,不是1的话就将count=0;每成功判断一次循环后,让该值>>1右移一位,更替掉它的最后一位,以便我们进行下一步的判断。 

 ③代码如下:

import java.util.*;
public class Main{
    public static void main(String[]args){
        Scanner scanner=new Scanner(System.in);
        int n=scanner.nextInt();
        int count=0;
        int tmp=0;
        while(n!=0){
        if((n&1)==1){
            count++;
            tmp=Math.max(count,tmp);
        }else{
            count=0;
        }
            n>>=1;
        }
        System.out.println(tmp);
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_58850105/article/details/124175985