2019屆華爲秋招筆試題【數字反轉相加】【消除重複數字】【仿LISP運算】

數字反轉相加

1.題目描述

請您寫一個 reversoAdd函數,該函數根據輸入的兩個正整數a和b, 字按照高位在右邊的方式反轉後求和。
例如,reverseAdd(123, 456) == 321 + 654 = 975
輸入描述:
函數原型: int reverseAdd (int a, int b);
輸入:
輸入的a, b參數均爲有效取值範圍[1, 70000]區間上的正整數。100和200反轉後的值爲1和2 (前導0被忽略)
輸出描述:
輸出:
通過函數返回值輸出結果。
若輸入的a或b參數超出了取值範圍(小於1或者大於70000),則應輸出-1:否則應按照要求輸出數字反轉後的和。
注意:最終交付的函數代碼中不要向控制檯打印輸出任何信息。
輸入樣例:
123, 456
輸出樣例:
975

2.解題思路

想法就是把它變成String類型,再利用String類型的reverse()函數進行反轉,再將其變成Integer類型進行運算

3.代碼

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sr = new Scanner(System.in);
        int a = sr.nextInt();
        int b = sr.nextInt();
        reverseAdd(a,b);
    }
    public static int  reverseAdd(int a,int b){
    		if((a<=1 && a>=70000) || (b<=1 && b>=70000))
    				return -1;
           String  str1 =new StringBuilder(a +"").reverse().toString();
           String str2 =new StringBuilder( b +"").reverse().toString();
           return Integer.parseInt(str1) +Integer.parseInt(str2);
    }
}

消除重複數字

1.題目描述

給定一個正整數,給出消除重複數字以後最大的整數
輸入描述:
正整數,注意考慮長整數
輸出描述:
消除重複數字以後的最大整數輸入樣例:423234輸出樣例:432

2.解題思路

方法1:(垃圾選手第一式,複雜度高)

  • 用list1存放出現一次的數字,用list2存放重複的數字。然後將他們放到優先隊列自動排序,因爲優先隊列從小到大排序的,所以重寫它的比較方法new Comparator()…,最後再依次將他們按順序加到res裏面

方法2:

  • 桶排序思想,因爲數字無疑就是0-9,所以創建一個長度爲10的數組,將每個數字依次入桶。桶中不爲空代表這個常整數有該值,然後將桶內的數依次拼湊成最大的數。count[0] ~count[9]依次存放0-9
  • 設置一個list存放一共出現了幾個不同的數了,當list.size == 10,代表所有數字都出現過了,後面再怎麼出現的數都是重複的。可以提前終止while語句。

3.代碼

方法1:

public static long solution(long a) {
        //存放出現一次的數
        List<Integer> list1 = new ArrayList<>();
        //存放重複的數
        List<Integer> list2 = new ArrayList<>();
        while (a != 0) {
            int temp = (int)a % 10;
            if (!list1.contains(temp) && !list2.contains(temp)) {
                list1.add(temp);
            } else {
                list1.remove(Integer.valueOf(temp));
                list2.add(temp);
            }
            a = a / 10;
        }
        //實現Comparator,將優先隊列從大到小排列
        PriorityQueue<Integer> queue = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2-o1;
            }
        });
        for(int i = 0;i<list1.size();i++){
            queue.add(list1.get(i));
        }
        for(int i = 0;i<list2.size();i++){
            queue.add(list2.get(i));
        }
        long res = 0;
        while (queue.size()!=0){
            res = res*10 + queue.poll();
        }
        return res;
    }

方法2:

public static long solution(long a){
        //利用桶計數方法,判斷哪個值出現過
        int count[] = new int[10];
        //用來計數,如果前面出現了0-9的所有數,則提前結束循環
        List<Integer> list = new ArrayList<>();
        while (a!=0){
            int temp =(int) a%10;
            count[temp]++;
            //list裏面存放沒出現的值
            if(!list.contains(temp)){
                list.add(temp);
            }
            //判斷是否提前結束循環
            if(list.size() == 10){
                break;
            }
            a = a / 10;
        }
        //結果的生成
        long res = 0;
        for(int i = count.length-1;i>=0;i--){
            if(count[i]!=0){
                res = res*10 + i;
            }
        }
        return res;
    }

仿LISP運算

1.題目描述

 LISP語言唯一的語法就是括號要配對。
 形如 (OP P1 P2 …),括號內元素由單個空格分割。
 其中第一個元素OP爲操作符,後續元素均爲其參數,參數個數取決於操作符類型
 注意:參數 P1, P2 也有可能是另外一個嵌套的 (OP P1 P2 …)
 當前OP類型爲add/sub/mul/div(全小寫),分別代表整數的加減乘除法。簡單起見,所以OP參數個數爲2
舉例
  輸入:(mul 3 -7)輸出:-21
  輸入:(add 1 2) 輸出:3
  輸入:(sub (mul 2 4) (div 9 3)) 輸出 :5
  輸入:(div 1 0) 輸出:error
輸入描述
  合法C字符串,字符串長度不超過512,用例保證了無語法錯誤
輸出描述
  合法C字符串,字符包括’0’-‘9’及負號’-‘或者’error’

2.解題思路

按照題意模擬語法運算規則

3.代碼

import java.util.Scanner;
import java.util.Stack;
public class Main_3 {
    public static void main(String[] args) {
        Scanner sr = new Scanner(System.in);
        String str = sr.nextLine();
        solution(str);
    }

    public static void solution(String str) {
        Stack<Integer> numStack = new Stack<>();
        Stack<String> operStack = new Stack<>();
        int mark = 0;
        int paramOne = 0;
        int paramTwo = 0;
        for(int i = 0;i<str.length();i++){
            char chas = str.charAt(i);
            if(chas == '('){
                //截取符號位
                operStack.push(str.substring(i+1,i+4));
                //這裏爲空格的索引位置
                i = i + 4;
                //符號位後第一個數字的索引座標
                mark = i+1;
            }else if(chas == ')'){
                if(mark < i){
                    //所有數字的截取
                    numStack.push(Integer.valueOf(str.substring(mark,i)));
                    i++;
                    mark = i+1;
                }
                //得到一次()的對應,就進行一次計算
                paramOne = numStack.pop();
                paramTwo = numStack.pop();
                calc(numStack,operStack,paramOne,paramTwo);
            }else{
                //空格位將數字進行區分
                if(chas == ' '){
                    if(mark < i ){
                        numStack.push(Integer.valueOf(str.substring(mark,i)));
                        //下一個數字的索引爲空格後面一位,故mark = i+1;
                        mark = i + 1;
                    }
                }
            }
        }
        //如果還有沒計算完的,就進行再次計算
        while (!operStack.isEmpty()){
            paramTwo = numStack.pop();
            paramOne = numStack.pop();
            calc(numStack,operStack,paramOne,paramTwo);
        }
    }

    private static void calc(Stack<Integer> numStack, Stack<String> operStack, int paramOne, int paramTwo) {
        switch(operStack.pop()){
            case "add":
                numStack.push(paramOne + paramTwo);
                break;
            case "sub":
                numStack.push(paramOne - paramTwo);
                break;
            case "mul":
                numStack.push(paramOne * paramTwo);
                break;
            case "div":
                if(paramTwo == 0)
                    System.out.println("error");
                else
                    numStack.push(paramOne / paramTwo);
                break;
        }
    }
}

題目及代碼整理自牛客技術資料
如有錯誤請指出!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章