给一个超级大的排好序的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)

 package fb;

 import java.util.*;

 public class ReorganizeVector {

     public List<List<Character>> reorganize(String str) {
List<List<Character>> res = new ArrayList<List<Character>>();
if (str==null || str.length()==0) return res;
int starter = 0;
int n = 0;
while (starter < str.length()) {
char cur = str.charAt(starter);
int index = starter + (int)Math.pow(2, n);
while (index < str.length() && str.charAt(index) == cur) {
n++;
index = starter + (int)Math.pow(2, n);
}
if (index >= str.length())
index = str.length() - 1;
int rightEdge = findRight(str, starter, index, cur);
List<Character> newItem = new ArrayList<Character>();
newItem.add((char)('0'+ rightEdge-starter+1));
newItem.add(cur);
res.add(newItem); starter = rightEdge + 1;
n = 0;
}
return res;
} public int findRight(String str, int starter, int end, char target) {
int l = starter;
int r = end;
while (l <= r) {
int m = (l + r)/2;
if (str.charAt(m) == target)
l = m + 1;
else r = m - 1;
}
return r;
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ReorganizeVector sol = new ReorganizeVector();
List<List<Character>> res = sol.reorganize("abbcccddddeeeee");
for (List<Character> line : res) {
for (Character c : line) System.out.print(c);
System.out.println("");
}
} }

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

  1. FB面经 Prepare: Count Unique Island

    数unique island, 比如 110000 110001 001101 101100 100000 总共两个unique岛,不是四个 方法可以是记录每次新的岛屿搜索的路径,left,right ...

  2. [geeksforgeeks] Count the number of occurrences in a sorted array

    Count the number of occurrences in a sorted array Given a sorted array arr[] and a number x, write a ...

  3. How to count the number of threads in a process on Linux

    If you want to see the number of threads per process in Linux environments, there are several ways t ...

  4. 2017年上海金马五校程序设计竞赛:Problem C : Count the Number (模拟)

    Description Given n numbers, your task is to insert '+' or '-' in front of each number to construct ...

  5. Count the number of possible triangles

    From: http://www.geeksforgeeks.org/find-number-of-triangles-possible/ Given an unsorted array of pos ...

  6. OpenCV count the number of connected camera 检测连接的摄像头的数量

    有时候在项目中我们需要检测当前连接在机子上的摄像头的数量,可以通过下面的代码实现,其中连接摄像头的最大数量maxCamNum可以任意修改: /** * Count current camera num ...

  7. FB面经Prepare: Friends Recommendation

    有个getFriend() API, 让你推荐你的朋友的朋友做你的朋友,当然这个新朋友不能是你原来的老朋友 package fb; import java.util.*; public class R ...

  8. FB面经 Prepare: Even Tree

    You are given a tree (a simple connected graph with no cycles). The tree has nodes numbered from to ...

  9. 1. 青蛙跳跳FrogJmp Count minimal number of jumps from position X to Y.

    青蛙跳跳: package com.code; public class Test03_1 { public int solution(int X, int Y, int D) { int res = ...

随机推荐

  1. GA:GA优化BP神经网络的初始权值、阈值,从而增强BP神经网络的鲁棒性—Jason niu

    global p global t global R % 输入神经元个数,此处是6个 global S1 % 隐层神经元个数,此处是10个 global S2 % 输出神经元个数,此处是4个 glob ...

  2. 网页布局之flex

    Flex是Flexible Box的缩写,意为“弹性布局”,用来为盒状模型提供最大的灵活性.设为Flex布局以后,子元素的float.clear和vertical-align属性将失效.使用flex ...

  3. wrk 性能测试工具安装与使用

    程序这玩意,性能是很关键的点,之前我一直以为自己写的程序能承载很多很多并发量之类的,然后,被一个搞搞安全的前辈来了个当头一棒,为什么?因为他给我测试了一下我程序的并发量,然后,我想死的心都有了,至于数 ...

  4. NEO-Karl-Python1

    第一个程序: print("NEO-Karl welcome to the new world")

  5. vue入门1(搭建项目)

    安装node.js 安装cnpm npm install -g cnpm --registry=http://registry.npm.taobao.org 安装vue-cli脚手架构建工具 npm ...

  6. 2017-11-4—稳态和暂态/瞬态(对运放积分电路的思考)[待仿真]

    先直接截图了,暂态或者说瞬态都是暂时的状态,是从一个稳定态到另一个稳定态的过程. 之所以要了解这个概念是因为对于使用运放搭建的模拟PID有很多的疑惑,比如负反馈没有电阻满不满足"虚短&quo ...

  7. js 原生_拖动页面元素,松开释放

    嗯哼.不多说,直接上代码了. // 为元素 绑定拖动事件 function bindDragEvent(obj){ obj.onmousedown = function(e){ e = e || wi ...

  8. PrintService类打印

    系统打印服务框架代码位于android.printservice包中.系统并没有实现具体打印功能,需要打印机厂商制作插件接入系统打印服务之后,自行实现 主要类: PrintDocument:表示待打印 ...

  9. shell变量的使用及输入输出

    1.shell 中变量名可以由字母,数字,下划线组成,但数字不能作为变量名的第一个数字 2.通过赋值符合“=” 来定义一个变量的值 如 myname='zhangjunjie'  #字符串类型,不解析 ...

  10. 剑指offer——python【第60题】把二叉树打印成多行

    题目描述 从上到下按层打印二叉树,同一层结点从左至右输出.每一层输出一行.#类似于二维列表[[1,2],[4,5]] 解题思路 其实这倒题和其他类似的题有所区别,这里是分层打印,把每层的节点值放在同一 ...