FB面经prepare: Count the number of Vector】的更多相关文章

给一个超级大的排好序的vector [abbcccdddeeee]比如,要求返回[{,a}, {,b}, {,c}, {,d}, {,e}......]复杂度要优于O(N) 分析: 如果是binary search找每个char的上下界,worst case要找n次,时间复杂度O(nlogn) 所以考虑每次比较start point和start point + 2^n位置上的数,假如一样就continue,不一样就在区间里面binary search找上界,这样worst case O(N) p…
数unique island, 比如 110000 110001 001101 101100 100000 总共两个unique岛,不是四个 方法可以是记录每次新的岛屿搜索的路径,left,right,up,down, 作为标志是否相同的key,存hashset package fbOnsite; import java.util.*; public class UniqueIsland { public int countIsland(int[][] grid) { HashSet<Strin…
Count the number of occurrences in a sorted array Given a sorted array arr[] and a number x, write a function that counts the occurrences of x in arr[]. Expected time complexity is O(Logn) Examples: Input: arr[] = {1, 1, 2, 2, 2, 2, 3,}, x = 2 Output…
If you want to see the number of threads per process in Linux environments, there are several ways to do it. Method One: /proc The proc pseudo filesystem, which resides in /proc directory, is the easiest way to see the thread count of any active proc…
Description Given n numbers, your task is to insert '+' or '-' in front of each number to construct expressions. Note that the position of numbers can be also changed. You can calculate a result for each expression. Please count the number of distinc…
From: http://www.geeksforgeeks.org/find-number-of-triangles-possible/ Given an unsorted array of positive integers. Find the number of triangles that can be formed with three different array elements as three sides of triangles. For a triangle to be…
有时候在项目中我们需要检测当前连接在机子上的摄像头的数量,可以通过下面的代码实现,其中连接摄像头的最大数量maxCamNum可以任意修改: /** * Count current camera number */ int countCamera() { ; ; ; device < maxCamNum; device++) { CvCapture* capture; if (_capture[device]) { ++count; } else { capture = cvCaptureFrom…
有个getFriend() API, 让你推荐你的朋友的朋友做你的朋友,当然这个新朋友不能是你原来的老朋友 package fb; import java.util.*; public class ReferFriends { public List<String> recommendation(String name) { List<String> res = new ArrayList<String>(); if (name==null || name.length…
You are given a tree (a simple connected graph with no cycles). The tree has nodes numbered from to and is rooted at node . Find the maximum number of edges you can remove from the tree to get a forest such that each connected component of the forest…
青蛙跳跳: package com.code; public class Test03_1 { public int solution(int X, int Y, int D) { int res = (Y-X)/D+((Y-X)%D==0?0:1); return res; } public static void main(String[] args) { Test03_1 t03 = new Test03_1(); System.out.println(t03.solution(10, 8…