1. 程式人生 > >【ForOffer】【深信服--測試開發崗筆試題】求最少車費&求最低位&找馬甲&找字串子串

【ForOffer】【深信服--測試開發崗筆試題】求最少車費&求最低位&找馬甲&找字串子串

深信服的整體筆試題難度一般,有多刷題的經驗,能立馬找出了題意理解,在此,自己只是做個總結髮出,有更好的解法的童靴可以一起交流,其實這四道題都是常見題型的一種變形。程式碼照著搬都是沒有問題的哦!

第一題:求最低位

 關鍵點說明:主要是對二進位制轉換,並且擷取到了從右到左第一個1位置。思路簡單吧!

package com.Mukvin.ShenXinFu;

import java.util.ArrayList;
import java.util.Scanner;

public class 求最低位 {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		String string = "" ;
		ArrayList<String> arrayList = new ArrayList<>();
		while(!(string = scanner.nextLine()).equals("0")) {
			arrayList.add(string);
		}
		ArrayList<Integer> res = getResFormStr(arrayList);
		for (int i = 0; i < res.size(); i++) {
			System.out.println(res.get(i));
		}
		scanner.close();
	}

	private static ArrayList<Integer> getResFormStr(ArrayList<String> arrayList) {
		ArrayList<Integer> temp = new ArrayList<>();
		
		for (int i = 0; i < arrayList.size(); i++) {
			String erjinzhi = getErJ(Integer.parseInt(arrayList.get(i)));
			//System.out.println(erjinzhi);
			String subMin = getSubMinBinary(erjinzhi);
			//System.out.println(subMin);
			//int res = binToTen();
			//System.out.println(Integer.parseInt(subMin, 2));
			temp.add(Integer.parseInt(subMin, 2));
		}
		return temp;
	}

	private static String getSubMinBinary(String erjinzhi) {
		int len = erjinzhi.length();
		char[] lastIndex = erjinzhi.toCharArray();
		int index = 0;
		for (int i = len-1; i >=0; i--) {
			if (lastIndex[i] == '1') {
				index = i;
				break;
			}else {
				continue;
			}
		}
		return erjinzhi.substring(index);
	}

	private static String getErJ(int num) {
		String string= "";
		while(num!=0) {
			string = num%2+string;
			num=num/2;
		}
		return string;
	}

}

第二題:最少車費

如果做過題的同學肯定能一眼就看出了這個題目,其實出題者只是變了個表述,連的其中的基礎資料都是沒有變的,也就是常規的動態規劃的題目。

package com.Mukvin.ShenXinFu;

import java.util.Scanner;

public class 最小車費 {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int[][] matrix = new int[4][4];
		for (int i = 0; i < matrix.length; i++) {
			for (int j = 0; j < matrix[0].length; j++) {
				matrix[i][j] =scanner.nextInt();
			}
		}
		int minFee = getMinPath(matrix);
		System.out.println(minFee);
		scanner.close();
	}

	private static int getMinPath(int[][] matrix) {
		if (matrix == null || matrix.length == 0 || matrix[0] == null || matrix[0].length == 0) {
			return 0;
		}
		int row = matrix.length;
		int col = matrix[0].length;
		int[][] dp = new int[row][col];
		dp[0][0] = matrix[0][0];
		for (int i = 1; i < row; i++) {
			dp[i][0] = dp[i - 1][0] + matrix[i][0];
		}
		for (int j = 1; j < col; j++) {
			dp[0][j] = dp[0][j - 1] + matrix[0][j];
		}
		for (int i = 1; i < row; i++) {
			for (int j = 1; j < col; j++) {
				dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1]) + matrix[i][j];
			}
		}
		return dp[row - 1][col - 1];
	}
}

 第三題:字串子串

 

該題主要注意一個點就是,其中如果重複了的話,要去掉,注意看有兩個bb哦!

package com.Mukvin.ShenXinFu;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;

public class 字串子串 {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		String string = scanner.nextLine();
		ArrayList<String> reStrings = getSubStr(string);
		if (reStrings.isEmpty() || reStrings == null) {
			System.out.println(0);
			return;
		}
		HashMap<String, Integer> hashMap = new HashMap<>();
		for (int i = 0; i < reStrings.size(); i++) {
			if (hashMap.containsKey(reStrings.get(i))) {
				continue;
			}else {
				hashMap.put(reStrings.get(i), 1);
				System.out.print(reStrings.get(i)+" ");
			}
		}
		scanner.close();
	}

	private static ArrayList<String> getSubStr(String string) {
		ArrayList<String> arrayList = new ArrayList<>();
		char[] charArr = string.toCharArray();
		for (int i = 0; i < charArr.length; i++) {
			if (!(charArr[i]>='a' && charArr[i]<='z')) {
				return arrayList;
			}
		}
		int index = 1;
		for (int i = 0; i < charArr.length; i++) {
			char tempChar = charArr[i];
			if (i+1 < charArr.length && tempChar == charArr[i+1] ) {
				++index;
			}else {
				arrayList.add(String.valueOf(index+""+tempChar));
				index = 1;
			}
		}
		
		return arrayList;
	}

}

第四題:找馬甲

該題的主要思路就是對映問題,看到對映,那不得把hashmap好好用起來!

package com.Mukvin.ShenXinFu;

import java.util.*;
import java.util.Map.Entry;
public class 找馬甲 {

	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		int n=scanner.nextInt();
		while(n!=0){
			String str[][]=new String [n][2];
			for(int i=0;i<n;i++){
				for(int j=0;j<2;j++){
					str[i][j]=scanner.next();
				}
			}
			pup(str);
			n=scanner.nextInt();
		}
		scanner.close();
	}
	public static void pup(String str[][]){
		int n=str.length;
		String s[][]=new String [n][2];
		HashMap<String ,String> map=new HashMap<String ,String>();
		for(int i=0;i<n;i++){
			if(!map.containsKey(str[i][1])){
				map.put(str[i][1], str[i][0]);
			}else{
				s[i][0]=map.get(str[i][1]);
				s[i][1]=str[i][0];
			}
		}
		TreeMap<String,String> tmap=new TreeMap<String,String>();
		for(int i=0;i<s.length;i++){
			if(s[i][0]!=null){
				tmap.put(s[i][0], s[i][1]);
			}
		}
		
		Iterator<?> iterator=tmap.entrySet().iterator();
		while(iterator.hasNext()){
			@SuppressWarnings("unchecked")
			Map.Entry<String, String> entry=(Entry<String, String>) iterator.next();
			System.out.println(entry.getValue()+" is the MaJia of "+entry.getKey());
		}
		System.out.println("over");
	}
}

 以上,就是這四道題的總結和回答~時間和空間複雜度可能不是最優的,但是思路卻是最直接明瞭的!