给一个超级大的排好序的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. 美团小程序框架mpvue蹲坑指南

    美团小程序框架mpvue(花名:没朋友)蹲坑指南 第一次接触小程序大概是17年初,当时小程序刚刚内侧,当时就被各种限制折腾的死去活来的,单向绑定, 没有promise,请求数限制,包大小限制,各种反人 ...

  2. Hibernate使用sql语句实现多表关联查询

    /** * <查找list> * * @return 返回页面需要显示的数据 */ @SuppressWarnings("unchecked") public List ...

  3. Beta(1/7)

    鐵鍋燉腯鱻 项目:小鱼记账 团队成员 项目燃尽图 冲刺情况描述 站立式会议照片 各成员情况 团队成员 学号 姓名 git地址 博客地址 031602240 许郁杨 (组长) https://githu ...

  4. 手把手教你安装nmon

    一.nmon简介 nmon是由IBM 提供.免费监控 AIX 系统与 Linux 系统资源的工具.该工具可帮助在一个屏幕上显示服务器系统资源耗用情况,并动态地对其进行更新.此外,他还可以利用 exce ...

  5. (80)Wangdao.com第十六天_JavaScript Object 对象的相关方法

    Object 对象的相关方法 Object.getPrototypeOf() 返回参数对象的原型. 这是获取某对象的原型对象的标准方法. var F = function () {}; var f = ...

  6. (84)Wangdao.com第十八天_JavaScript Promise 对象

    Promise 对象 是 JavaScript 的异步操作解决方案,为异步操作提供统一接口. 目前 JavaScript 原生支持 Promise 对象 它起到代理作用(proxy),充当异步操作与回 ...

  7. linux命令技巧

    printf "%-5s %-10s %-4s\n" No Name Mark printf "%-5s %-10s %-4.2f\n" 1 Sarath 80 ...

  8. 【ASP.NET】Validation 服务器控件

    Validation 服务器控件 Validation 服务器控件用于验证输入控件的数据.如果数据未通过验证,则向用户显示错误消息. 创建 Validation 服务器控件的语法是: <asp: ...

  9. 构建Dubbo-2.0.7源码

    一. 下载源码: git clone --branch dubbo-2.0.7 https://github.com/apache/incubator-dubbo.git 二. 下载依赖 git cl ...

  10. mysql 日志类型

    mysql有四种日志: 所有日志:general log 慢查询日志:slow log 二进制日志:binary log 错误日志:error log windows下在my.ini里配置,linux ...