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)
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的更多相关文章
- FB面经 Prepare: Count Unique Island
数unique island, 比如 110000 110001 001101 101100 100000 总共两个unique岛,不是四个 方法可以是记录每次新的岛屿搜索的路径,left,right ...
- [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 ...
- 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 ...
- 2017年上海金马五校程序设计竞赛:Problem C : Count the Number (模拟)
Description Given n numbers, your task is to insert '+' or '-' in front of each number to construct ...
- Count the number of possible triangles
From: http://www.geeksforgeeks.org/find-number-of-triangles-possible/ Given an unsorted array of pos ...
- OpenCV count the number of connected camera 检测连接的摄像头的数量
有时候在项目中我们需要检测当前连接在机子上的摄像头的数量,可以通过下面的代码实现,其中连接摄像头的最大数量maxCamNum可以任意修改: /** * Count current camera num ...
- FB面经Prepare: Friends Recommendation
有个getFriend() API, 让你推荐你的朋友的朋友做你的朋友,当然这个新朋友不能是你原来的老朋友 package fb; import java.util.*; public class R ...
- FB面经 Prepare: Even Tree
You are given a tree (a simple connected graph with no cycles). The tree has nodes numbered from to ...
- 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 = ...
随机推荐
- Node.js API 学习笔记
常用 API 学习笔记 url 函数 url.parse: 解析 url 地址 url.resolve: 向 url 地址添加或替换字段 url.format: 生成 url 地址 querystri ...
- Centos系统中彻底删除Mysql数据库
步骤: 1.输入命令查询系统中已安装的mysql. rpm -qa |grep -i mysql 2.逐个卸载mysql. yum remove 系统显示已安装的mysql 比如:yum remove ...
- maya cmds pymel undoInfo chunk 撤销束
maya cmds pymel undoInfo chunk 撤销束 cmds.undoInfo(openChunk = 1) # your code cmds.undoInfo(closeChunk ...
- 【mybatis】-- springboot整合mybatis
1.添加依赖 <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>m ...
- Django跨域、cookie、session
前后台分离开发 1.前台页面运行在前台服务器上,负责页面的渲染(静态文件的加载)与跳转 2.后台代码运行在后台服务器上,负责数据的处理(提供数据请求的接口) 跨域 什么是跨域? 通常情况下,A网页访问 ...
- YII2 console中引用其他模块(子项目)的model时出现model找不到命名空间的问题解决
YII2 console中写定时任务, 想使用其他模块的model, 在 console的yii.php 入口文件中引入其他模块的配置文件, 否者会出现model等命名空间找不到的问题. 还有, 命名 ...
- Django——微信消息推送
前言 微信公众号的分类 微信消息推送 公众号 已认证公众号 服务号 已认证服务号 企业号 基于:微信认证服务号 主动推送微信消息. 前提:关注服务号 环境:沙箱环境 沙箱环境地址: https://m ...
- mongodb 遇到问题-查询单个需要包装id
mongodb,get字符查询需要传入特定的包装id才能识别 const ObjectID = require('mongodb').ObjectID exports.queryOne = (req, ...
- vue_条件渲染_v-if_v-else_v-show
data: { ok: true flag: false } 1. 成对出现的 v-if 和 v-else 原理是: 标签的删除与重新创建 ,有些情况必须用 v-if <p v-if=" ...
- [LeetCode] Implement Rand10() Using Rand7() 使用Rand7()来实现Rand10()
Given a function rand7 which generates a uniform random integer in the range 1 to 7, write a functio ...