crazyMe
BAN USERpublic class SubMtrix {
private int m =3;
private int n =4;
private int mark_i = 0,mark_another=0;
private int part_row = 0;
private int row_1 = 0,row_2 =0;;
private int end_row =0,end_another=0;
public static void main(String args[]) {
int [][] a ={ { 0, 1,1,0}, { 0,0,0,0}, { 1, 1, 0, 0 } };
SubMtrix sm = new SubMtrix();
sm.subMat(a);
}
public void subMat(int a[][]) {
for(int i =0 ;i < a.length;i++){
part_row = 0;
for(int j =0;j < a[0].length;j++) {
if(a[i][j]==1){
if(row_1 == 0)
mark_i = i;
part_row++;
row_1++;
end_row =i+1;
}
}
if(part_row ==0)
{
row_2=row_1;
mark_another = mark_i;
end_another = end_row;
row_1 =0;
}
if(part_row !=0 & row_2 < row_1){
row_2 =row_1;
mark_another = mark_i;
end_another = end_row;
}
}
for(int i = mark_another;i < end_another;i++){
for(int j =0;j < n;j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}
I think to iterate over all elements in array it is of O(n) where n is the number of elements in array.
Also,it is O(n) in linked list.
But I think array is faster due to contiguous memory allocation and also due to less memory access as you dont need to access next pointer.
public class TransitionPoints {
//private int count;
//private int mid;
private ArrayList<Integer> al = new ArrayList<Integer>();
public static void main(String[] args) {
String s ="001112233444567";
TransitionPoints tp = new TransitionPoints();
tp.getPoints(s);
}
public void getPoints(String n) {
char [] arr = n.toCharArray();
getTransPoints(arr,0,arr.length-1);
for(Integer i:al)
System.out.println(i);
}
public void getTransPoints(char[] arr,int low, int high)
{
if(low == high)
return;
if(arr[low] == arr[high])
return;
int mid = (low+high)/2;
/* if(arr[low] < arr[high] & low+1 == high){
al.add(low);
}*/
getTransPoints(arr,low,mid);
getTransPoints(arr,mid+1,high);
if(arr[mid] != arr[mid+1])
al.add(mid);
}
}
code without recursion.Hope it helps
public static void main(String args[]) {
int num =12345;
SortLinkedList.replace(num);
}
public static void replace(int number) {
int z=0;
String s = ((Integer)number).toString();
char [] arr = s.toCharArray();
for(int i = 0; i < arr.length; i++) {
z=i;
System.out.println("outer");
while( z <= arr.length - 1){
char c = arr[z];
arr[z] = 'a';
System.out.print(Arrays.toString(arr));
System.out.println();
System.out.println("heya");
z=z+2;
}
arr =s.toCharArray();
}
}
time complexity O(mn)
- crazyMe July 14, 2014