mikezebin
BAN USER
//linear time O(n), traverse from left to right
//initialize variables
int i = 0;
int maxSum = 0; // sum of current maximum array
int LastSum = 0; // sum of current extending array
int maxBegin = 0; //start position of current maximum array
int LastBegin = 0; //start position of current extending array
int maxLength = 1; // length of current maximum array
int LastLength = 1; // length of current extending array
for(i=0; i<arr.length(); i++)
{
//if LastSum and the current integer are both non-negative or LastSum >= |arr[i]|, merge them
if(LastSum >= 0 && (arr[i] >= 0 || LastSum >= -arr[i]))
{
LastSum += arr[i];
LastLength ++;
}
//if arr[i] < 0, and |arr[i]| > LastSum , restart the calculation
else if arr[i] < -LastSum //note that LastSum is maintained non-negative
{
LastLength = 0;
LastSum = 0;
if(i< arr.length)
LastBegin = i+1;
}
///compare LastSum and maxSum, if LastSum is greater, update the array which has maximum sum.
if( LastSum >= maxSum )
{
maxSum = LastSum;
maxBegin = LastBegin;
maxLength = LastLength;
}
}
ArrayList<int[]> FindSumOfThreeUnique(int[] arr)
{
//sort the array
Arrays.sort(arr);
//unique value in order
Set<Integer> set = new LinkedHashSet<Integer>(Arrays.asList(arr));
//FindSumOfThree
return FindSumOfThree(set.toArray(),0);
}
ArrayList<int[]> FindSumOfThree(int[] arr, int sum)
{
ArrayList<int[]> results = new ArrayList<int[]>();
int i,j,k;
for(i = 0; i<arr.length(); i++)
for(j = 0; j<arr.length(); j++)
for(k = 0; k<arr.length(); k++)
if(arr[i]+arr[j]+arr[k] == sum)
{
results.add(new int[3]);
results.get(result.size())[0] = arr[i];
results.get(result.size())[1] = arr[j];
results.get(result.size())[2] = arr[k];
}
return results;
}
- mikezebin April 24, 2014Java > StringBuilder
String WordReverse(String string1)
{
StringBuilder string2 = new StringBuilder(); //output string
StringBuilder strTemp = new StringBuilder(); //buffer
isPreLetter = False; // if the previous letter is alphabetical, suppose there is non-alphabetical ahead of the string
string1.concat(" "); // sovle problem of the daggling last word
for(int i = 0; i<string1.length(); i++)
{
if (int)string[i]>=65 AND string[i]<=122 AND (string[i]<=90 OR string[i]>=97) //IsAlabet(string[i])
{
if isPreLetter //previous alphabetical, current alphabetical
strTemp.Append(string1[i]);
else //previous non-alphabetical, current alphabetical
strTemp.Delete();
strTemp.Append(string1[i]);
isPreLetter = True;
}else{
if isPreLetter //previous alphabetical, current non-alphabetical
string2.Append(strTemp.ToString());
isPreLetter = False;
else //previous non-alphabetical, current non-alphabetical
string2.Append(string1[i]);
}
}
string2.Delete(i); //cut off the last character
return string2.ToString(); //convert from StringBuilder to String
}
bool OneEditApart(string1, string2)
{
char str1[] = string1.toCharArray();
char str2[] = string2.toCharArray();
if( str1.len > str2.len)
return IsInsert( str2, str1);
else if(str1.len < str2.len)
return IsInsert(str1, str2);
else
return IsReplace(str1, str2);
}
bool IsReplace(char a[], char b[]) //array b is the result of one char replacement on array a
{
if( a.len <> b.len)
return false;
int i = 0;
int j = 0;
int ndiff = 0;
while(ndiff<2)
{
if(a[i++] <> b[j++])
ndiff++;
}
return ndiff == 1;
}
bool IsInsert(char a[], char b[]) //array b is the result of one char insert on array a
{
if(b.len - a.len <> 1)
return false;
int i = 0;
int j = 0;
int ndiff = 0;
while(ndiff<2)
{
if(a[i] == b[j])
{
i++;
j++;
}else{
ndiff++;
j++;
}
}
return ndiff == 1;
}
- mikezebin April 23, 2014
Repjacobghester2, Accountant at ASAPInfosystemsPvtLtd
I'm an artist at heart. I went from print design to web development and lots of server administration professionally ...
RepJanie Margreta, Android Engineer at Achieve Internet
JanieMargreta works as a plant operator, an employee who supervises the operation of an industrial plant. Where I have to ...
Repcharlesgwitt47, Animator at ASU
By Profession, I am a Automotive service technician in Kennewick USA. My strong interest is in yoga, My yogic journey ...
- mikezebin April 30, 2014