-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedlistflattingquestion.java
144 lines (135 loc) · 3.49 KB
/
linkedlistflattingquestion.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Stack;
//class Node{
//
// int data;
// Node next;
// Node child;
// public Node(int data,Node next,Node child){
// this.data=data;
// this.child=child;
// this.next=next;
// }
//
// public Node(){
// this.data=0;
// this.next=null;
// this.child=null;
// }
//
// public Node (int data){
// this.data=data;
// this.next=null;
// this.child=null;
// }
//}
public class linkedlistflattingquestion {
// public static Node flattenLinkedList(Node head) {
// ArrayList<Integer> arr=new ArrayList<>();
//
// Node temp=head;
//
// while(temp.next!=null){
// Node children=temp;
// while(children!=null){
// arr.add(children.data);
// children=children.child;
// }
// temp=temp.next;
// }
//
// if(temp!=null && !arr.contains(temp.data)){
// arr.add(temp.data);
// }
//
// Node newhead=createll(arr);
// return newhead;
//
// }
//
// public static Node createll(ArrayList<Integer>arr){
// Node head=new Node(arr.get(0));
// Node temp=head;
// for(int i=1;i<arr.size();i++){
// Node newnode=new Node(arr.get(i));
// temp.next=newnode;
// temp=newnode;
// }
//
// return head;
// }
public static void main (String[]args){
System.out.println(divide(10,3));
}
public static int divide(int dividend, int divisor) {
return binary(0,dividend*divisor,dividend,divisor);
}
public static int binary(int low,int high,int dividened,int divisor){
int mid=(low+high)/2;
int ans=0;
while(low<=high){
if((mid*divisor) >=dividened){
ans=mid;
low=mid+1;
}else{
high=mid-1;
}
}
return ans;
}
static int setBit(int n){
int org=n;
int pos=0;
while(n>0){
if(n%2==1){
pos++;
}else{
break;
}
n=n>>1;
}
int bitmask=1<<pos;
return org|bitmask;
}
public static void quicksort(int low,int high,ArrayList<Integer>arr){
if(low<=high){
int partition=sort(low,high,arr);
quicksort(low,partition-1,arr);
quicksort(partition+1,high,arr);
}
}
public static int sort(int low,int high,ArrayList<Integer>arr){
int pivot=low;
int i=low;
int j=high;
while(i<j){
while(arr.get(i)<=arr.get(pivot) && i<=high-1)i++;
while(arr.get(j)>arr.get(pivot) && j>=low)j--;
if(i<j){
int temp=arr.get(i);
arr.set(i,arr.get(j));
arr.set(j,temp);
}
}
int temp=arr.get(j);
arr.set(j,arr.get(pivot));
arr.set(pivot,temp);
return j;
}
public static double myPow(double x, int n) {
if(n==0) return 1;
if(x==0) return 0;
double ans=1;
while(n!=0){
if(n%2==1){
n=n-1;
ans=x*ans;
}else{
x=x*x;
n=n/2;
}
}
return ans;
}
}