给一个超级大的排好序的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. es ik分词插件安装

    1.ik下载(下载es对应版本的ik分词包) https://github.com/medcl/elasticsearch-analysis-ik/releases 2.mac cd /usr/loc ...

  2. leetcode算法题整理

    一.线性表,如数组,单链表,双向链表 线性表.数组 U1.有序数组去重,返回新数组长度 A = [1,1,2] -> [1,2] 返回2   分析:其实一般数组的问题都可以用两个指针解决,一个指 ...

  3. 获取验证码倒计时60s

    倒计时函数: function time(btns) { if (wait == 0) { btns.css("background-color","#F84C02&qu ...

  4. Do-Now—团队冲刺博客四

    各个成员今日完成的任务 侯泽洋:完成奖励页面设计,完成奖励从云端拉取到本地:完成奖励从云端拉取到本地 周亚杰:冲刺博客,个人中心设计(未完成) 王志伟:积分页面设计:积分页面设计 仇夏:树苗成长过程设 ...

  5. Hibernate 映射一对一关联关系

    基于外键的方式: 附上代码: public class Manager { private Integer mgrId; private String mgrName; private Departm ...

  6. Installation of CarbonData 1.1.0 with Spark 1.6.2

    关键词:carbondata spark thrift 数据仓库 [Install thrift 0.9.3] 注意 要装thrift-java必须先装ant . 有人说要装boost,我在cento ...

  7. 6. 深度克隆_ES7**_arr.includes('孙悟空')

    1. 如何实现深度克隆 利用 JSON 方法 (没办法克隆函数数据) `JSON.parse(JSON.stringify(xxx))` 自定义方法 检查所有数据类型的方法 `Object.proto ...

  8. poj 3422 最小费用流

    如果不是从费用流区做这个题几乎不会想到用费用流 点有权值很容易想到拆点 问题是求最大sum ...  把权值取负 这样最小费用流的相反数就是最大sum 源点S汇点T k为移动次数 矩阵中的点拆成入点出 ...

  9. pthread_detach()与pthread_join的区别?

    简单来说: pthread_detach()即主线程与子线程分离,子线程结束后,资源自动回收.pthread_join()即是子线程合入主线程,主线程阻塞等待子线程结束,然后回收子线程资源. [转]在 ...

  10. 2019.4.1 JMeter中文乱码解决方案

    1)添加一个HTTP信息头管理器:添加Accept:application/xml;application/json;charset=utf-8 2)参数为中文:在HTTP请求时设置编码格式为utf- ...