topics

全部话题 - 话题: maxlength
1 (共1页)
f*******t
发帖数: 7549
1
来自主题: JobHunting版 - T第二轮面经
这题还好吧,代码很简单:
class TreeNodeStatus {
int maxLength;
int maxDepth;
}

private TreeNodeStatus getTreeNodeStatus(TreeNode node) {
TreeNodeStatus status = new TreeNodeStatus();
if (node == null) {
status.maxLength = 0;
status.maxDepth = 0;
} else if (node.left == null && node.right == null) {
status.maxLength = 1;
status.maxDepth = 1;
} else {
TreeNodeStatus leftStatus = getTreeN... 阅读全帖
f*******t
发帖数: 7549
2
来自主题: JobHunting版 - T第二轮面经
这题还好吧,代码很简单:
class TreeNodeStatus {
int maxLength;
int maxDepth;
}

private TreeNodeStatus getTreeNodeStatus(TreeNode node) {
TreeNodeStatus status = new TreeNodeStatus();
if (node == null) {
status.maxLength = 0;
status.maxDepth = 0;
} else if (node.left == null && node.right == null) {
status.maxLength = 1;
status.maxDepth = 1;
} else {
TreeNodeStatus leftStatus = getTreeN... 阅读全帖
n*******n
发帖数: 45
3
木有用DP....
public int lengthOfLongestSubstring(String s) {
if (s == null) {
return 0;
}
if (s.length() < 2) {
return s.length();
}
int maxLength = 0;
int totalLen = s.length();
int currentLen = 0;
int cStart = 0;
Map indexMap = new HashMap<>();
while((totalLen - cStart) > maxLength) {
for(int i = cStart; i < totalLen; i++) {
Character cur = s.char... 阅读全帖
g**t
发帖数: 3
4
来自主题: JobHunting版 - one amazon interview problem
硬想出来的,没什么值得总结的思路,应该work,也满足条件。大家测测吧
public class Longest10Substring {
public static void main(String[] args) {
calc(new int[] { 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0 });
calc(new int[] { 0 });
}
public static void calc(int[] a) {
int n = a.length;
int minority = 0, cnt = 0;
for (int i = 0; i < n; i++) {
if (a[i] == 1) {
cnt++;
}
}
if (cnt == 0 || cnt == n) {
print(a, 0, 0... 阅读全帖
a*******8
发帖数: 110
5
来自主题: JobHunting版 - 问道string match的题
O(k^2)的brute force算法附上:
String getCommon(String s1, String s2) {
int m = s1.length();
int n = s2.length();

int maxLength = Math.min(m, n);
while (maxLength > 0) {
if (s1.substring(m - maxLength).compareTo(s2.substring(0,
maxLength)) == 0) {
return s1.substring(m - maxLength);
}
--maxLength;
}
return "";
}
n****r
发帖数: 471
6
用的方法就是最简单的逆序+ longest common substring (DP)
judge small能过
judge large就提示说memory limit exceeded。
我DP的table是用vector存的, code如下,请大侠们给点儿建议。 提前谢谢啦!
string longestPalindrome(string s) {
string ret;
if(s.empty())
return ret;
string s_copy = s;
reverse(s_copy.begin(), s_copy.end());
return findLongestCommonSubsequence(s, s_copy);
}


string findLongestCommonSubsequence(string s1, string s2){
int m = (int) s1.length();
int n = (int)... 阅读全帖
r**h
发帖数: 1288
7
连续递增子串的话,从左往右扫一轮不就可以了吗
int i=0, j, maxLength=0;
while(i < A.length-1){
while(A[i]> A[i+1] && i i++;
j = i+1;
while(jA[j-1])
j++;
if(maxLength < j-i)
maxLength = j-i;
i = j;
}
if(maxLength <= 1)
return 0;
return maxLength;
s********u
发帖数: 1109
8
来自主题: JobHunting版 - 这段LIS为啥崩溃?
if(nums[i]>nums[j] && sizes[i] < sizes[j]+1)
{
sizes[i] = sizes[j]+1;
paths[i] = paths[j] + nums[i] + " ";
if(maxLength < sizes[i])
maxLength = sizes[i];
}
中间这一段写的不太好,尤其是paths[i]的处理,应该用一个变量比如local_prev来记
录j的值,等j全部遍历完了,这时候再处理paths。包括maxlength也是。
应该是
for( int j = 0; j < i; j++){
if(nums[i]>nums[j] && sizes[i] < sizes[j]+1)
{
sizes[i] = sizes[j]+1;
local_prev = j;
}
}
paths[i] = path[j] + num[i] + " ";
if(ma... 阅读全帖
i******d
发帖数: 7
9
来自主题: JobHunting版 - Google onsite 题目求助
Space O(1), Time O(n)
public int maxLength(String s){
if (s.isEmpty()) return 0;
int diffCharIndex = -1, currentLength = 1, maxLength = 1;
for (int i = 1; i < s.length(); i++)
{
if (s.charAt(i) != s.charAt(i-1))
{
if (diffCharIndex == -1 || s.charAt(i) == s.charAt(
diffCharIndex)) {
currentLength++; diffCharIndex = i - 1; }
else { currentLength = i - diffCharIndex; diffCharIndex = i-
1; ... 阅读全帖
i******d
发帖数: 7
10
来自主题: JobHunting版 - Google onsite 题目求助
Space O(1), Time O(n)
public int maxLength(String s){
if (s.isEmpty()) return 0;
int diffCharIndex = -1, currentLength = 1, maxLength = 1;
for (int i = 1; i < s.length(); i++)
{
if (s.charAt(i) != s.charAt(i-1))
{
if (diffCharIndex == -1 || s.charAt(i) == s.charAt(
diffCharIndex)) {
currentLength++; diffCharIndex = i - 1; }
else { currentLength = i - diffCharIndex; diffCharIndex = i-
1; ... 阅读全帖
j*****u
发帖数: 1133
11
这个短些,用你的思路
static int LongestUniqueSubstring(string str)
{
if (string.IsNullOrEmpty(str)) return 0;
var lastPosition = new Dictionary(); // or use array with len
gth of #unicode_char
int maxLength = 0;
for (int head = 0, tail = 0; tail < str.Length; tail++)
{
int last;
if (lastPosition.TryGetValue(str[tail], out last))
head = last + 1;
else
maxLength = Math.Max(maxLength, tail - head + 1);
lastPosition[str[tai... 阅读全帖
s****a
发帖数: 528
12
来自主题: JobHunting版 - google电面杯具,贡献题目
1. build hash table for words in : wordsSet
2. sort the words from shortest to longest: wordsArray
while(!wordsArray.empty())
{
string word = wordsArray.back();
wordsArray.pop_back();
int maxLength = word.length();
if (maxLength == 1) return 1;
if (wordsSet.find(word) == wordsSet.end())
continue; // this word is already tested
wordsSet.erase(word);
WordsStack subWordsStack;
subWordsStack.push(word);
// check if the word can be build up from 1 to maxle... 阅读全帖
s****a
发帖数: 528
13
来自主题: JobHunting版 - google电面杯具,贡献题目
1. build hash table for words in : wordsSet
2. sort the words from shortest to longest: wordsArray
while(!wordsArray.empty())
{
string word = wordsArray.back();
wordsArray.pop_back();
int maxLength = word.length();
if (maxLength == 1) return 1;
if (wordsSet.find(word) == wordsSet.end())
continue; // this word is already tested
wordsSet.erase(word);
WordsStack subWordsStack;
subWordsStack.push(word);
// check if the word can be build up from 1 to maxle... 阅读全帖
r*******t
发帖数: 99
14
来自主题: JobHunting版 - 一道G家店面题
这个题相当于一个Jump Game:一排格子,每一个可以往后面的某些格子里跳,每一跳
的cost是0。同时每一个可以往紧邻的下一个格子跳,每一跳的cost是1。把格子看成
vertex,跳看成edge的话就是一个directed graph的shortest path问题,而且这个图已
经是topologically ordered,只用从前往后scan一遍所有vertices,同时update当前
vertex前方与之相连的vertices就可以了。
每一个vertex有哪些outgoing的edges可以查用anagram索引过的dictionary来确定。索
引dictionary的时候记下最大词长可以减少look up的次数。
string minSkip(vector& dictionary, string& str) {
multimap ana2word;
size_t maxLength = 0;
for (size_t i = 0; i < dictionary.size(); ++i) {
... 阅读全帖
s**x
发帖数: 7506
15
来自主题: JobHunting版 - 问一道题(7)
found this
http://www.dsalgo.com/2013/03/longest-subarray-with-equal-numbe
Longest subarray with equal number of ones and zeros
Problem
You are given an array of 1's and 0's only. Find the longest subarray which
contains equal number of 1's and 0's.
Solution
We will keep a running sum of "no of ones minus no of zeros" for each index
of the array. For any two indices, if the running sum is equal, that means
there are equal number of ones and zeros between these two indices. We will
store the runn... 阅读全帖
l********n
发帖数: 1038
16
来自主题: JobHunting版 - 这段LIS为啥崩溃?
longest Increasing Subsequence问题,java
public class LIS {
public static void main(String[] args)
{
int[] nums = {2,6,4,5,1,3};
printLIS(nums);
}

public static void printLIS(int[] nums)
{

String[] paths = new String[nums.length];
int[] sizes = new int[nums.length];

for(int i=0; i {
sizes[i] = 1;
... 阅读全帖
z***e
发帖数: 58
17
来自主题: JobHunting版 - 好挫的F家面经
public int maxSizedSubSequence(int [] num, int target){
Map map = new HashMap();
map.put(0, -1);
int sum = 0;
int maxLength = 0;
for(int i = 0 ;i < num.length; ++i){
sum += num[i];
if(!map.containsKey(sum)){
map.put(sum, i);
}
if(map.containsKey(sum - target)){
int prevIdx = map.get(sum - target);
maxLength = Math.max(maxLength... 阅读全帖
l***r
发帖数: 27
18
来自主题: Piebridge版 - [通知]斑马站点试运行开始
地址:
http://24.59.197.54/Game/login.aspx
或者
http://24.59.197.54:8080/Game/login.aspx
当一个连不上时,就试另一个
给我发过信的同学们可以登陆了
注意
1。鉴于时间有限,本站只实现基本功能,不要挑剔,
2。不要故意破坏,不要用超出正常范围的输入,避免用特殊字符
3。有bug或建议发到我的信箱
手册:
基本上一目了然,可同时开6条线(thread),每人最多开3条,
线主出题,并设最大线长(MaxLength);
在thread list的页面可以看到线列表,包括线主(owner),标题(title),
最大线长(MaxLength),完成的线长(length),正在创作者(workingUser);
如果length跟maxLength相等,表示线已结束,可点开看结果
如果length比maxLength小,表示未结束,这种情况下
如果没有workingUser, 点开后可点reply 接线
最后,完成的线请线主把结果贴到这里来,然后从站点上删除
a*******8
发帖数: 110
19
来自主题: JobHunting版 - 问道string match的题
面试时的String match题,如果要求比brute force更好的解的话,都可以考虑这个。
看了半天Wiki,才弄明白。。。
//KMP algorithm
//Reference WIKI: //http://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm
String getCommon(String s1, String s2) {
int maxLength = Math.min(s1.length(), s2.length());

int[] table = buildFailureTable(s2, maxLength);
int m = s1.length() - maxLength;
int i = 0;
while (m + i < s1.length()) {
if (s1.charAt... 阅读全帖
P********l
发帖数: 452
20
done利用已知的最大长度来跳过不必要的比较,觉得应该是最优的解法了。
但是代码上还可以改进:
int maxLength(int[] arr) {
if (arr.length < 2)
return arr.length;
// at least, the length contiguous incremental sequence
is 1.
int maxLen = 1;
// checking using the known maximum length
for (int i = maxLen; i < arr.length; i += maxLen) {
// i-maxLen is the starting position of the
sequence, so,
// invariant: arr[i-maxLen-1] >= arr[i-maxLen]
if (arr[i - 1] >... 阅读全帖
e********2
发帖数: 495
21
This should be the optimal:
Map[] maps = new Hashmap[26];
for(String s : strings){
// aabbcd then n = 4
int n = distinct characters in s;
// aabbcd then binary representation is 0b111100000000000000
int m = s's bit pattern;
// maps[n-1].get(m) stores the longest string with the same set
// of distinct chars
Integer maxlength = maps[n-1].get(m);
maps[n - 1].put(m, maxlength == null ? s.size() : Math.max(s.size(), max
length));
}... 阅读全帖
w*******w
发帖数: 18
22
来自主题: BuildingWeb版 - 关于php的问题
大家帮忙看看,为什么submit之后调出的是processorder.php的源代码?是不是环境建的
有问题?谢谢。















Item Quantity
Tires maxlength = "3">
Oil maxlength = "3">
P****y
发帖数: 707
23
来自主题: BuildingWeb版 - 关于php的问题
跟你code没关系。你的php没有安装好

大家帮忙看看,为什么submit之后调出的是processorder.php的源代码?是不是环境建的
有问题?谢谢。















Item Quantity
Tires maxlength = "3">
Oil maxlength = "3">
K****n
发帖数: 5970
24
看,这就是google这个网站全部的源代码:
大哥大嫂过年好!
content="text/html; charset=UTF-8">Google



阅读全帖
s****a
发帖数: 6521
48
来自主题: BuildingWeb版 - 问个jquery的简单问题
想用jquery.autocomplete.js做个简单的自动完成
结果无论如何都没反应
请问有可能是什么回事?
主要代码如下:



阅读全帖
H*********e
发帖数: 276
49
来自主题: Database版 - 给一堆table,怎样能自动生成ERD
在任何一个database 下, 用这个code 找, 我以前工作里面一个大牛给我的
select
pt.[name] as [ParentTable],
pc.[name] as [ParentColumn],
ct.[name] as [ChildTable],
cc.[name] as [ChildColumn]
from sys.foreign_key_columns fk
JOIN sys.columns pc on pc.column_id = fk.parent_column_id and pc.object_id =
fk.parent_object_id
JOIN sys.columns cc on cc.column_id = fk.referenced_column_id and cc.object
_id = fk.referenced_object_id
JOIN sys.objects pt on pt.object_id = pc.objec... 阅读全帖
m******u
发帖数: 12400
50
来自主题: Database版 - 给一堆table,怎样能自动生成ERD
thanks for sharing.
发信人: HoneyCoffee (贝贝), 信区: Database
标 题: Re: 给一堆table,怎样能自动生成ERD
发信站: BBS 未名空间站 (Sun Oct 11 22:24:08 2015, 美东)
在任何一个database 下, 用这个code 找, 我以前工作里面一个大牛给我的
select
pt.[name] as [ParentTable],
pc.[name] as [ParentColumn],
ct.[name] as [ChildTable],
cc.[name] as [ChildColumn]
from sys.foreign_key_columns fk
JOIN sys.columns pc on pc.column_id = fk.parent_column_id and pc.object_id =
fk.parent_object_id
JOIN sys.columns cc on... 阅读全帖
1 (共1页)