File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ class LRUCache {
2+ private int capacity ;
3+ private Node head ;
4+ private Node tail ;
5+ private Map <Integer , Node > map ;
6+
7+ public LRUCache (int capacity ) {
8+ this .capacity = capacity ;
9+ head = null ;
10+ tail = null ;
11+ map = new HashMap <>();
12+ }
13+
14+ public int get (int key ) {
15+ Node node = map .get (key );
16+ if (node == null ) {
17+ return -1 ;
18+ }
19+ if (node != tail ) {
20+ if (node == head ) {
21+ head = head .next ;
22+ } else {
23+ node .next .prev = node .prev ;
24+ node .prev .next = node .next ;
25+ }
26+ tail .next = node ;
27+ node .prev = tail ;
28+ node .next = null ;
29+ tail = node ;
30+ }
31+ return node .value ;
32+ }
33+
34+ public void put (int key , int value ) {
35+ Node node = map .get (key );
36+ if (node == null ) {
37+ Node newNode = new Node (key , value );
38+
39+ if (capacity == 0 ) {
40+ Node temp = head ;
41+ head = head .next ;
42+ map .remove (temp .key );
43+ capacity ++;
44+ }
45+ if (head == null && tail == null ) {
46+ head = newNode ;
47+ } else {
48+ tail .next = newNode ;
49+ newNode .prev = tail ;
50+ newNode .next = null ;
51+ }
52+ tail = newNode ;
53+ map .put (key , newNode );
54+ capacity --;
55+ } else {
56+ // node already exit
57+ node .value = value ;
58+ if (node != tail ) {
59+ if (node == head ) {
60+ head = head .next ;
61+ } else {
62+ node .next .prev = node .prev ;
63+ node .prev .next = node .next ;
64+ }
65+ tail .next = node ;
66+ node .prev = tail ;
67+ node .next = null ;
68+ tail = node ;
69+ }
70+ }
71+ }
72+ }
73+
74+ class Node {
75+ int key ;
76+ int value ;
77+ Node prev ;
78+ Node next ;
79+
80+ public Node (int key , int value ) {
81+ this .key = key ;
82+ this .value = value ;
83+ prev = null ;
84+ next = null ;
85+ }
86+ }
Original file line number Diff line number Diff line change 1+ public class Solution {
2+ // need to treat n as an unsigned value
3+ public int reverseBits (int n ) {
4+ if (n == 0 ) {
5+ return 0 ;
6+ }
7+ int res = 0 ;
8+ for (int i = 0 ; i < 32 ; i ++) {
9+ res <<= 1 ;
10+ if ((n & 1 ) == 1 ) {
11+ res ++;
12+ }
13+ n >>= 1 ;
14+ }
15+ return res ;
16+ }
17+ }
Original file line number Diff line number Diff line change 1+ public class Solution {
2+ // need to treat n as an unsigned value
3+ public int hammingWeight (int n ) {
4+ int sum = 0 ;
5+ while (n != 0 ) {
6+ n &= n - 1 ;
7+ sum ++;
8+ }
9+ return sum ;
10+ }
11+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public boolean isPowerOfTwo (int n ) {
3+ if (n < 1 ) {
4+ return false ;
5+ }
6+ return (n & (n -1 )) == 0 ;
7+ }
8+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public boolean isAnagram (String s , String t ) {
3+ if (s .length () != t .length ()) {
4+ return false ;
5+ }
6+ int [] count = new int [26 ];
7+ for (int i = 0 ; i < s .length (); i ++) {
8+ count [s .charAt (i ) - 'a' ]++;
9+ count [t .charAt (i ) - 'a' ]--;
10+ }
11+ for (int num : count ) {
12+ if (num != 0 ) {
13+ return false ;
14+ }
15+ }
16+ return true ;
17+ }
18+ }
You can’t perform that action at this time.
0 commit comments