Skip to content

Commit 35b4322

Browse files
committed
init
0 parents  commit 35b4322

File tree

7 files changed

+546
-0
lines changed

7 files changed

+546
-0
lines changed

BitManipulation.java

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import java.io.*;
2+
import java.math.*;
3+
import java.security.*;
4+
import java.text.*;
5+
import java.util.*;
6+
import java.util.concurrent.*;
7+
import java.util.regex.*;
8+
9+
public class Solution {
10+
11+
12+
public static double log(int x, int base) {
13+
return (Math.log(x) / Math.log(base));
14+
}
15+
16+
17+
public static int reverseBits(int n,int len){
18+
int an=0;
19+
for(int i=0;i<len;i++){
20+
if((n & (1<<i)) != 0){
21+
an = an | (1<<len-i-1);
22+
}
23+
}
24+
return an;
25+
}
26+
27+
public static int reverseBitsI(int n,int len)
28+
{
29+
int leftbit,rightbit;
30+
for(int i=0;i<len/2;i++){
31+
if((n & (1<<i)) != 0){ rightbit=1; }else{ rightbit=0; }
32+
if((n & (1<<(len-i-1))) != 0){ leftbit=1; }else{ leftbit=0; }
33+
if(leftbit!=rightbit){
34+
if(leftbit==0){
35+
n = n | (1<<(len-i-1));
36+
n = n ^ (1<<i);
37+
}else{
38+
n = n ^ (1<<(len-i-1));
39+
n = n | (1<<i);
40+
}
41+
}
42+
}
43+
return n;
44+
}
45+
public static int invertBits(int n,int len)
46+
{
47+
for(int i=0;i<len;i++){
48+
if((n & (1<<i)) == 0){
49+
n = n | (1<<i);
50+
}else{
51+
n = n ^ (1<<i);
52+
}
53+
}
54+
return n;
55+
}
56+
57+
public static void main(String[] args)
58+
{
59+
Scanner scan = new Scanner(System.in);
60+
int n=scan.nextInt();
61+
int output = reverseBits(n,Integer.toBinaryString(n).length());
62+
int output1 = reverseBitsI(n,Integer.toBinaryString(n).length());
63+
int output2 = invertBits(n,Integer.toBinaryString(n).length());
64+
System.out.println(n + "'s binary :" + Integer.toBinaryString(n));
65+
System.out.println(n + "'s reverse's binary :" + Integer.toBinaryString(output));
66+
System.out.println(n + "'s reverse's binary :" + Integer.toBinaryString(output1));
67+
System.out.println(n + "'s Inversion binary :" + Integer.toBinaryString(output2));
68+
69+
}
70+
71+
}
72+
73+
74+
75+
76+
77+
78+
79+
80+
int convertBit(int num){
81+
int ans=1;
82+
while(ans<=num){
83+
ans=ans<<1;
84+
}
85+
ans--;
86+
return ans^num;
87+
}
88+
89+
90+
91+
92+
93+
int convertBit(int num){
94+
int ans=0,cnt=0;
95+
while(num>0){
96+
int r=num%2;
97+
ans+=(1-r)*pow(2,cnt);
98+
cnt++;
99+
num=num/2;
100+
}
101+
return ans;
102+
}
103+
104+
105+
106+
107+
108+

JSSample.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
process.stdin.resume();
2+
process.stdin.setEncoding('utf8');
3+
let ind=0;
4+
process.stdin.on('data', function(chunk){
5+
arr = chunk.split('\n');
6+
});
7+
8+
process.stdin.on('end', function() {
9+
let T=Number(arr[ind++]);
10+
for(i=0;i<T;i++){
11+
let N=Number(arr[ind].split(' ')[0]);
12+
let K=Number(arr[ind++].split(' ')[1]);
13+
let arr1=arr[ind++].split(' ');
14+
arr1=arr1.map((v)=>{return Number(v)});
15+
let min=arr1[0],max=arr1[0],minInd=0,maxInd=0;
16+
arr1.forEach((val,ind)=>{
17+
if(val>max){max=val;maxInd=ind;}
18+
if(val<min){min=val;minInd=ind}}
19+
);
20+
if(minInd<maxInd){
21+
console.log(min+' '+max);}else{console.log(max+' '+min);}
22+
}
23+
});

Multiset.java

+159
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
class Multiset<E> {
2+
3+
private List<E> values;
4+
private List<Integer> frequency;
5+
private final String ERROR_MSG = "Count cannot be negative: ";
6+
7+
public Multiset() {
8+
values = new ArrayList<>();
9+
frequency = new ArrayList<>();
10+
}
11+
12+
public Multiset(Multiset<E> ms) {
13+
values = new ArrayList<>();
14+
frequency = new ArrayList<>();
15+
ms.forEach((val)->{this.add(val);});
16+
}
17+
18+
public int add(E element, int count) {
19+
if (count < 0) {
20+
throw new IllegalArgumentException(ERROR_MSG + count);
21+
}
22+
23+
int index = values.indexOf(element);
24+
int prevCount = 0;
25+
26+
if (index != -1) {
27+
prevCount = frequency.get(index);
28+
frequency.set(index, prevCount + count);
29+
}
30+
else if (count != 0) {
31+
values.add(element);
32+
frequency.add(count);
33+
}
34+
35+
return prevCount;
36+
}
37+
38+
public boolean add(E element) {
39+
return add(element, 1) >= 0;
40+
}
41+
42+
boolean addAll(Collection<? extends E> c){
43+
for (E element: c)
44+
add(element, 1);
45+
46+
return true;
47+
}
48+
49+
public void addAll(E... arr) {
50+
51+
for (E element: arr)
52+
add(element, 1);
53+
}
54+
55+
public void forEach(Consumer<? super E> action)
56+
{
57+
List<E> all = new ArrayList<>();
58+
59+
for (int i = 0; i < values.size(); i++)
60+
for (int j = 0; j < frequency.get(i); j++)
61+
all.add(values.get(i));
62+
63+
all.forEach(action);
64+
}
65+
66+
public boolean remove(Object element)
67+
{
68+
return remove(element, 1) > 0;
69+
}
70+
71+
public int remove(Object element, int count)
72+
{
73+
if (count < 0) {
74+
throw new IllegalArgumentException(ERROR_MSG + count);
75+
}
76+
77+
int index = values.indexOf(element);
78+
if (index == -1)
79+
return 0;
80+
81+
int prevCount = frequency.get(index);
82+
83+
if (prevCount > count) {
84+
frequency.set(index, prevCount - count);
85+
}
86+
else {
87+
values.remove(index);
88+
frequency.remove(index);
89+
}
90+
91+
return prevCount;
92+
}
93+
94+
public boolean contains(Object element) {
95+
return values.contains(element);
96+
}
97+
98+
public boolean containsAll(Collection<?> c) {
99+
return values.containsAll(c);
100+
}
101+
102+
public int setCount(E element, int count)
103+
{
104+
if (count < 0) {
105+
throw new IllegalArgumentException(ERROR_MSG + count);
106+
}
107+
108+
if (count == 0)
109+
remove(element);
110+
111+
int index = values.indexOf(element);
112+
if (index == -1)
113+
return add(element, count);
114+
115+
int prevCount = frequency.get(index);
116+
frequency.set(index, count);
117+
118+
return prevCount;
119+
}
120+
121+
public int count(Object element) {
122+
int index = values.indexOf(element);
123+
124+
return (index == -1) ? 0 : frequency.get(index);
125+
}
126+
127+
public Set<E> elementSet() {
128+
return values.stream().collect(Collectors.toSet());
129+
}
130+
131+
public boolean isEmpty() {
132+
return values.size() == 0;
133+
}
134+
135+
public int size() {
136+
int size = 0;
137+
for (Integer i: frequency){
138+
size += i;
139+
}
140+
return size;
141+
}
142+
143+
@Override
144+
public String toString() {
145+
StringBuilder sb = new StringBuilder("[");
146+
for (int i = 0; i < values.size(); i++) {
147+
sb.append(values.get(i));
148+
149+
if (frequency.get(i) > 1)
150+
sb.append(" x ").append(frequency.get(i));
151+
152+
if (i != values.size() - 1)
153+
sb.append(", ");
154+
}
155+
156+
return sb.append("]").toString();
157+
}
158+
}
159+

Trie.java

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
public class Main{
2+
public static void main(String []args){
3+
Node[] root=new Node[27];
4+
String[] input ={"ball","bounce","red","bear","talks","bells"};
5+
System.out.println(new Trie().build(input));
6+
}
7+
}
8+
9+
class Node{
10+
Node[] list=new Node[27];
11+
boolean endingFlag=false;
12+
int count=0;
13+
@Override
14+
public String toString() {
15+
StringBuilder sb = new StringBuilder();
16+
sb.append("Node[] : ").append(this.list).append("\n");
17+
sb.append("count : ").append(this.count).append("\n");
18+
sb.append("endingFlag : ").append(this.endingFlag).append("\n");
19+
return sb.toString();
20+
}
21+
}
22+
23+
class Trie{
24+
Node[] build(String[] input){
25+
Node[] root=new Node[27];
26+
for(String a:input){
27+
int count=1;Node newNode;
28+
if(root[a.charAt(0)-'a']==null){
29+
newNode=new Node();
30+
root[a.charAt(0)-'a'] = newNode;
31+
}else{
32+
newNode=root[a.charAt(0)-'a'];
33+
}
34+
newNode.count+=1;newNode.endingFlag=true;
35+
for(char c:a.toCharArray()){
36+
if(count==1){count=0;continue;}
37+
38+
newNode.endingFlag=false;
39+
newNode.count-=1;
40+
Node Node1;
41+
if(newNode.list[c-'a']==null){
42+
Node1=new Node();
43+
}else{
44+
Node1=newNode.list[c-'a'];
45+
}
46+
47+
Node1.endingFlag=true;
48+
Node1.count+=1;
49+
50+
newNode=Node1;
51+
}System.out.println(newNode);
52+
}
53+
return root;
54+
}
55+
56+
}
57+

0 commit comments

Comments
 (0)