木有用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... 阅读全帖
这个短些,用你的思路
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... 阅读全帖
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... 阅读全帖
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... 阅读全帖
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... 阅读全帖
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... 阅读全帖
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] >... 阅读全帖
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));
}... 阅读全帖
在任何一个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... 阅读全帖
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... 阅读全帖