Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bd6a3b6

Browse files
nextcommit
1 parent 4fc6f8c commit bd6a3b6

File tree

6 files changed

+324
-1
lines changed

6 files changed

+324
-1
lines changed

‎src/revision/FindPairs.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package revision;
2+
3+
class ListN<t> {
4+
5+
public t data;
6+
public ListN<t> forw;
7+
public ListN<t> backw;
8+
9+
public ListN(t data) {
10+
this.data = data;
11+
}
12+
13+
14+
}
15+
16+
17+
public class FindPairs {
18+
19+
public static void findPairs(ListN<Integer> head, int n) {
20+
// Write your code here
21+
22+
23+
ListN<Integer> start = head;
24+
ListN<Integer> end = head;
25+
26+
while (end.forw != null) {
27+
end = end.forw;
28+
}
29+
30+
// System.out.println(start.data+" "+end.data);
31+
while (start.data < end.data) {
32+
33+
if (start.data + end.data == n) {
34+
System.out.println(start.data + " " + end.data);
35+
start = start.forw;
36+
end = end.backw;
37+
}
38+
39+
else if (start.data + end.data > n) {
40+
end = end.backw;
41+
}
42+
43+
else if (start.data + end.data < n) {
44+
start = start.forw;
45+
}
46+
47+
48+
}
49+
50+
51+
52+
}
53+
54+
public static void main(String[] args) {
55+
56+
}
57+
58+
}

‎src/revision/FloorValue.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package revision;
2+
3+
public class FloorValue {
4+
5+
public static int helper(int array[], int element, int in, int mx) {
6+
7+
if (in == array.length) {
8+
return mx;
9+
}
10+
11+
if (array[in] <= element && array[in] > mx) {
12+
mx = array[in];
13+
}
14+
15+
return helper(array, element, in + 1, mx);
16+
17+
}
18+
19+
20+
public static int findFloor(int[] array, int element) {
21+
22+
23+
int ans = helper(array, element, 0, Integer.MIN_VALUE);
24+
return ans;
25+
26+
27+
}
28+
29+
public static void main(String[] args) {
30+
31+
}
32+
33+
}

‎src/revision/GenerateParanthesis.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package revision;
2+
3+
public class GenerateParanthesis {
4+
5+
public static void generate(String str, int n, int i, int open, int close) {
6+
7+
if (i == 2 * n) {
8+
System.out.println(str);
9+
}
10+
11+
if (open < n) {
12+
generate(str + "(", n, i + 1, open + 1, close);
13+
}
14+
15+
if (open > close) {
16+
generate(str + ")", n, i + 1, open, close + 1);
17+
}
18+
19+
20+
}
21+
22+
public static void generateImp(String str, int n, int open, int close) {
23+
24+
if (close == n) {
25+
System.out.println(str);
26+
27+
}
28+
29+
if (open < n) {
30+
generateImp(str + "(", n, open + 1, close);
31+
}
32+
if (open > close) {
33+
generateImp(str + ")", n, open, close + 1);
34+
}
35+
36+
37+
38+
}
39+
40+
public static void main(String[] args) {
41+
42+
generate("", 5, 0, 0, 0);
43+
generateImp("", 5, 0, 0);
44+
}
45+
46+
}

‎src/revision/LoopReconnect.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package revision;
2+
3+
public class LoopReconnect {
4+
5+
public static ListNode<Integer> method(ListNode<Integer> head) {
6+
if (head == null || head.next == null) {
7+
return head;
8+
}
9+
10+
ListNode<Integer> temp = head;
11+
ListNode<Integer> fast = head;
12+
ListNode<Integer> slow = head;
13+
14+
while (fast != null && fast.next != null) {
15+
16+
slow = slow.next;
17+
fast = fast.next.next;
18+
19+
if (slow == fast) {
20+
21+
break;
22+
}
23+
24+
25+
26+
}
27+
28+
if (slow == fast) {
29+
30+
slow = head;
31+
while (slow.next != fast.next) {
32+
slow = slow.next;
33+
fast = fast.next;
34+
}
35+
}
36+
37+
ListNode<Integer> val = fast.next;
38+
// System.out.println(fast.next.data);
39+
fast.next = null;
40+
41+
ListNode<Integer> mx = null;
42+
// ListNode<Integer>prev=null;
43+
44+
while (temp.next != null) {
45+
46+
if (temp.data > val.data) {
47+
48+
mx = (mx == null || mx.data > temp.data) ? temp : mx;
49+
50+
}
51+
52+
temp = temp.next;
53+
54+
}
55+
56+
fast.next = mx;
57+
58+
59+
60+
return head;
61+
62+
}
63+
64+
65+
66+
public static void main(String[] args) {
67+
68+
}
69+
70+
}

‎src/revision/Tribonacci.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ else if (n <= 2)
6565

6666
}
6767

68-
}
68+
6969

7070
public static void main(String[] args) {
7171

‎src/revision/matchNumberLL.java

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package revision;
2+
3+
public class matchNumberLL {
4+
5+
6+
public static boolean CheckRepresentaion(ListNode<Integer> head1, ListNode<Integer> head2) {
7+
8+
if (head1 == null && head2 == null) {
9+
return true;
10+
}
11+
12+
if (head1 == null || head2 == null) {
13+
return false;
14+
}
15+
16+
StringBuilder num1 = new StringBuilder();
17+
StringBuilder num2 = new StringBuilder();
18+
19+
20+
while (head1 != null && head2 != null) {
21+
22+
num1.append(head1.data);
23+
num2.append(head2.data);
24+
head1 = head1.next;
25+
head2 = head2.next;
26+
27+
28+
29+
}
30+
31+
while (head1 != null) {
32+
num1.append(head1.data);
33+
head1 = head1.next;
34+
}
35+
while (head2 != null) {
36+
num2.append(head2.data);
37+
head2 = head2.next;
38+
}
39+
while (num1.charAt(0) == '0') {
40+
num1.deleteCharAt(0);
41+
}
42+
while (num2.charAt(0) == '0') {
43+
num2.deleteCharAt(0);
44+
}
45+
46+
47+
48+
return (num1.toString().compareTo(num2.toString()) != 0) ? false : true;
49+
}
50+
51+
52+
public static String convert(int n) {
53+
54+
if (n == 0) {
55+
return "0";
56+
}
57+
String s = "";
58+
char t;
59+
while (n > 0) {
60+
t = (char) (n % 10);
61+
s = t + s;
62+
n /= 10;
63+
}
64+
65+
return s;
66+
}
67+
68+
// problem with this method...c++
69+
public static boolean method(ListNode<Integer> a, ListNode<Integer> b) {
70+
71+
String one = "", two = "";
72+
73+
boolean zero = false;
74+
75+
while (a.next != null) {
76+
77+
zero = true;
78+
if (a != null || one.length() != 0) {
79+
one += convert(a.data);
80+
}
81+
a = a.next;
82+
83+
}
84+
85+
if (zero == true && one.length() != 0) {
86+
one = "0";
87+
}
88+
zero = false;
89+
90+
while (b.next != null) {
91+
zero = true;
92+
if (b != null || two.length() != 0) {
93+
two += convert(b.data);
94+
}
95+
96+
b = b.next;
97+
}
98+
99+
if (zero == true && two.length() != 0) {
100+
two = "0";
101+
}
102+
103+
return one.equals(two);
104+
105+
}
106+
107+
public static void main(String[] args) {
108+
109+
ListNode<Integer> head = new ListNode<>(1);
110+
ListNode<Integer> head2 = new ListNode<Integer>(14);
111+
112+
System.out.println(CheckRepresentaion(head, head2));
113+
114+
}
115+
116+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /