面试题总结~~(google level)
題目一
Trapping Rain Water
Given?n?non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.
For example,?
Given?[0,1,0,2,1,0,1,3,2,1,2,1], return?6.
The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped.?Thanks Marcos?for contributing this image!
這里計算面積不用一般幾何書的方法,這里是兩邊往中間遍歷,記錄當前第二高點secHight,然后利用這個第二高點減去當前歷經的柱子,剩下就裝水容量了。
為什么是第二高點?因為兩邊比較,最高的點不用動,只移動第二高點。
第一個思路,按照上圖的思想就能寫出非常簡潔的程序了,時間復雜度為O(n):
int trap(int A[], int n) {int secHight = 0;int left = 0;int right = n-1;int area = 0;while (left < right){if (A[left] < A[right]){secHight = max(A[left], secHight);area += secHight-A[left];//計算當前格的能裝雨水的容量left++;} else {secHight = max(A[right], secHight);area += secHight-A[right];right--;}}return area;}題目二
Given a number, can you remove k digits from the number so that the new?formatted number is smallest possible.
input: n = 1432219, k = 3
output: 1219
public class Solution {public static void main(String[] args){System.out.print(get_smallest("63534",2));}public static String get_smallest(String n, int k){int len=n.length()-k;//最后的序列長int index=0;int temp=0;String result="";//輸出結果for(int i=0;i<len;i++){int min=9;for(int j=index;j<=n.length()-len+i;j++){if(Integer.parseInt(String.valueOf(n.charAt(j)))<min){index=j+1;min=Integer.parseInt(String.valueOf(n.charAt(j)));}}result+=min+"";}return result;} }
題目三
第一題:Median of Two Sorted Arrayspublic class Solution {public static void main(String[] args){int[] list1={1,5,8,8,9};int[] list2={2,7,7};System.out.print(get_median(list1,list2));}public static int get_median(int[] list1,int[] list2){int len1=list1.length;int len2=list2.length;if(len1==0 && len2==0) return 0;if(len1==0) return list2[len2/2];if(len2==0) return list1[len1/2];int median=(len1+len2)/2-2;int index_1=0;int index_2=0;int result=Integer.MIN_VALUE;while(index_1+index_2<=median && index_1<len1 && index_2<len2){if(list1[index_1]<=list2[index_2]){result=list2[index_2];index_1++;}else{result=list1[index_1];index_2++;} }if(index_1+index_2-1==median){return result;}else{if(index_1==len1){return list2[median-len1];}else{return list1[median-len2];}}} }
/********************************
* 本文來自博客 ?“李博Garvin“
* 轉載請標明出處:http://blog.csdn.net/buptgshengod
******************************************/
總結
以上是生活随笔為你收集整理的面试题总结~~(google level)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【LeetCode从零单排】No198.
- 下一篇: 【分布式计算】DFS BigTable