给一个超级大的排好序的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. pandas合并merge-【老鱼学pandas】

    本节讲述对于两个数据集按照相同列的值进行合并. 首先定义原始数据: import pandas as pd import numpy as np data0 = pd.DataFrame({'key' ...

  2. mysql运行报The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone的解决方法

    话不多说,从错误即可知道是时区的错误,因此只要将时区设置为你当前系统时区即可, 因此使用root用户登录mysql,按照如下图所示操作即可. 我电脑的系统为北京时区,因此在系统中设置后,再连接数据库运 ...

  3. SpringMVC-2-(Controller)

    一)参数类型 @RequestMapping("hello4") @ResponseBody public ModelAndView Hello4( // Servlet的三个参数 ...

  4. Stars HDU - 1541

    HDU - 1541 思路:二维偏序,一维排序,一维树状数组查询即可. #include<bits/stdc++.h> using namespace std; #define maxn ...

  5. Python WMI获取Windows系统信息

    #!/usr/bin/env python # -*- coding: utf-8 -*- #http://www.cnblogs.com/liu-ke/ import wmi import os i ...

  6. FJUT3260

    不是啊..不是说双栈嘛,,怎么是个**题啊. 链接:  http://120.78.128.11/Problem.jsp?pid=3260    从左到右扫一遍,把相交的区间扔到一起算,那么就变成了一 ...

  7. Tomcat报错invalid LOC header

    原因: 可能是jar包有问题. 解决方法: 1.找到加载不了的类对应的jar包. 2.在tomcat中webapps/INF/lib中找到对应的jar包,然后删除. 3.重新下载其它版本的jar包. ...

  8. 问题:CGI返回给前端的汉字数据是乱码(已解决)

    今天,我在写CGI程序,把后台数据返回到前台页面上,出现乱码, 不好看,所以,我在Content-type:application后面加了字符集的设置(字符集和前端一样), fprintf(stdou ...

  9. 怎样将PDF文件转换成Excel表格

    PDF文件怎样转换成Excel表格呢?因为很多的数据信息现在都是通过PDF文件进行传输的,所以很多时候,信息的接受者都需要将这些PDF文件所传输的数据信息转换成Excel表格来进行整理,但是我们应该怎 ...

  10. 对于EMC DAE、DPE、SPE、SPS的解释

    对于EMC DAE.DPE.SPE.SPS的解释: DAE: Disk Array Enclosure 单一的磁盘扩展单元 CX200/CX300/CX400/CX500CX600/CX700 DPE ...