新浪微博筆試題

一、第一題

題目描述

假設有一個已經排好序的整型數組,要求實現一個使用二分查找的函數,判斷某個數字是否出如今該數組中。java

題目分析

這道題目相對來講很簡單,二分查找可用遞歸或者迭代實現,主要是中間值進行比較。數組

解決方法:

輸入一個已經排好序的整型數組以及待查的數組,返回一個布爾值,表示查詢的值是否存在。
代碼以下:ide

package sina;
/** 
 * @author smile_tina
 * 
 */
public class Problem1 {
    public boolean findNum(int[] nums, int num) {
        if (null == nums)
            return false;
        return findNumSub(nums, num, 0, nums.length - 1);
    }
    public boolean findNumSub(int[] nums, int num, int left, int right) {
        if (left > right)
            return false;
        int mid = left + (right - left) / 2;
        if (nums[mid] == num) {
            return true;
        } else if (nums[mid] > num)
            return findNumSub(nums, num, left, mid - 1);
        else
            return findNumSub(nums, num, mid + 1, right);
    }
    public static void main(String[] args) {
        int[] a = { 1, 2, 3, 4, 5, 6, 7 };
        Problem1 po = new Problem1();
        System.out.println(po.findNum(a, 8));
    }
}

二、第二題

題目描述

假設有一個整型數組,裏面有若干數字,要求統計出一共有多少種不一樣的數字,並將這些數字保存到一個新的數組中,且根據數字出現的頻率從少到多排列,若是頻率相同則按數字值從小到大排列。函數

題目分析

使用Hashmap存儲對應數字出現的頻數,key是數字,value是該數字出現的次數。而後根據value值進行排序。code

解決方法:

輸入數組,map存儲數字以及出現的次數,而後重寫Collections的sort函數,按value值進行排序,輸出不一樣數字的個數,result爲所求新數組。排序

package sina;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * 
 * @author smile_tina
 * 
 */
public class Problem2 {

    /**
     * 
     * @param nums 輸入數組
     * @return 按要求輸出新數組
     */
    public int[] distinctNum(int[] nums) {
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        // fill map
        for (int i = 0; i < nums.length; i++) {
            if(map.containsKey(nums[i])){
                map.put(nums[i], map.get(nums[i])+1);
            }else
                map.put(nums[i], 1);
        }
        //根據value值進行排序
        List<Map.Entry<Integer, Integer>> info = new ArrayList<Map.Entry<Integer,Integer>>(map.entrySet());
        Collections.sort(info,new Comparator<Map.Entry<Integer, Integer>>() {
            @Override
            public int compare(Entry<Integer, Integer> o1,
                    Entry<Integer, Integer> o2) {               
                return o1.getValue()-o2.getValue();
            }
            
        });
        System.out.println("一共有"+info.size()+"種不一樣的數字");
        int[] result = new int[info.size()];
        for(int i = 0;i<info.size();i++){
            result[i] = info.get(i).getKey();
        }
        return result;
        
    }

    public static void main(String[] args) {
        Problem2 po = new Problem2();
        int[] a = {1,2,6,4,2,1,8,9,3,6,7};
        System.out.println(Arrays.toString(a));
        System.out.println(Arrays.toString(po.distinctNum(a)));
    }

}

三、第三題

題目描述

假設有一個字符串數組,每個字符都是一個數字(1-9),找到其中的最大遞增數,遞增數是指相鄰的數位從小到大排列的數字,如:28953456323,遞增數有:289,3456,23,那麼最大的遞增數爲3456。遞歸

題目分析

該題是個動態規劃問題。
若是嚴格按照動態規劃的步驟去解,在字符串長度爲n的狀況下須要存儲n個字符串。可是咱們能夠發現以下規律,若是str[m:n]是嚴格遞增的,則他的子集str[p:q] (p大於等於m 而且小於等於n) 必定是嚴格遞增的,並且若是str[m] < str[m-1] 而且 str[n] > str[n+1] (未考慮邊界條件),則str[m:n]必定是字符串m到n索引中值最大的字符串,有了這個規律,咱們只須要從頭日後遍歷一次:用一個索引start表示當前正在遍歷的遞增字符串的初始位置,用一個變量res來存儲以前最大的字符串,每當發現一個局部遞增的字符串就和res比較一次(比較的規則是:若是str1的長度大於res的長度,則str1大,若是長度相等則比較字符串第一個位置),同時索引tmpIndex增長這個遞增字符串的長度,不然一旦不連續,更新初始位置也就是start的值。這樣當tmpIndex到達字符串末尾的時候,就獲得了最大字符串。索引

解決方法

代碼以下:字符串

package sina;

/**
 * 
 * @author smile_tina
 * 
 */
public class Problem3 {

    public String findMaxNum(String str) {
        if (null == str || str.length() == 0)
            return "";
        String res = "";
        int start = 0;
        int tmpIndex = start;
        for (int i = 1; i < str.length(); i++) {
            if (str.charAt(i) - str.charAt(tmpIndex) == 1) {
                tmpIndex++;
                if ((tmpIndex - start+1) > res.length())
                    res = str.substring(start, tmpIndex + 1);
                if((tmpIndex - start + 1== res.length()) && str.charAt(start)>res.charAt(0))
                    res = str.substring(start, tmpIndex + 1);
            }
            else{
                start = i;
                tmpIndex = start;
            }
        }
        return res;
    }

    public static void main(String[] args) {
        Problem3 po = new Problem3();
        System.out.println(po.findMaxNum("209876543210243423243242382648327493274832784123456789"));
    }
}
相關文章
相關標籤/搜索