-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1171 from codimiracle/master
307-Week 07
- Loading branch information
Showing
11 changed files
with
330 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class Solution { | ||
public int[] relativeSortArray(int[] arr1, int[] arr2) { | ||
int[] bucket = new int[1001]; | ||
for (int i = 0; i < arr1.length; i++) { | ||
bucket[arr1[i]]++; | ||
} | ||
int pos = 0; | ||
for (int i = 0; i < arr2.length; i++) { | ||
int times = bucket[arr2[i]]; | ||
for (int j = 0; j < times; j++) { | ||
arr1[pos++] = arr2[i]; | ||
} | ||
bucket[arr2[i]] = 0; | ||
} | ||
for (int i = 0; i < bucket.length; i++) { | ||
for (int j = 0; j < bucket[i]; j++) { | ||
arr1[pos++] = i; | ||
} | ||
bucket[i] = 0; | ||
} | ||
return arr1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
class LRUCache { | ||
private Map<Integer, Node> map; | ||
private Node head; | ||
private Node tail; | ||
private int size; | ||
private int capacity; | ||
|
||
public LRUCache(int capacity) { | ||
this.map = new HashMap<>(); | ||
this.capacity = capacity; | ||
this.size = 0; | ||
this.head = this.tail = null; | ||
} | ||
private void addFirst(int key, int value) { | ||
Node node = new Node(); | ||
node.key = key; | ||
node.value = value; | ||
map.put(key, node); | ||
moveToFirst(node); | ||
} | ||
|
||
private void concat(Node a, Node b) { | ||
if (a != null && b != null) { | ||
a.next = b; | ||
b.previous = a; | ||
} | ||
} | ||
private void removeLast() { | ||
Node temp = tail; | ||
//头尾一样 | ||
if (tail == head) { | ||
tail = head = null; | ||
} else { | ||
tail = tail.previous; | ||
tail.next = temp.previous = temp.next = null; | ||
} | ||
map.remove(temp.key); | ||
} | ||
private void moveToFirst(Node node) { | ||
//头节点 | ||
if (node == head) | ||
return; | ||
if (tail == null) { | ||
head = tail = node; | ||
return; | ||
} | ||
//尾节点 | ||
if (tail == node) { | ||
tail = node.previous; | ||
tail.next = null; | ||
node.previous = null; | ||
// 中间节点 | ||
} else { | ||
concat(node.previous, node.next); | ||
node.previous = null; | ||
} | ||
concat(node, head); | ||
head = node; | ||
} | ||
|
||
public int get(int key) { | ||
if (map.containsKey(key)) { | ||
Node target = map.get(key); | ||
moveToFirst(target); | ||
return target.value; | ||
} | ||
return -1; | ||
} | ||
|
||
public void put(int key, int value) { | ||
if (map.containsKey(key)) { | ||
Node target = map.get(key); | ||
target.value = value; | ||
get(key); | ||
} else { | ||
if (size + 1 > capacity) { | ||
removeLast(); | ||
size--; | ||
} | ||
addFirst(key, value); | ||
size++; | ||
} | ||
} | ||
|
||
class Node { | ||
int key; | ||
int value; | ||
Node previous; | ||
Node next; | ||
} | ||
} | ||
|
||
/** | ||
* Your LRUCache object will be instantiated and called as such: | ||
* LRUCache obj = new LRUCache(capacity); | ||
* int param_1 = obj.get(key); | ||
* obj.put(key,value); | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
public class Solution { | ||
// you need treat n as an unsigned value | ||
public int reverseBits(int n) { | ||
int reversedN = 0; | ||
int i = 0; | ||
while (i < 32) { | ||
reversedN |= ((n >> i) & 1) << (31 - i); | ||
i++; | ||
} | ||
return reversedN; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
public class Solution { | ||
// you need to treat n as an unsigned value | ||
public int hammingWeight(int n) { | ||
int count = 0; | ||
while (n != 0) { | ||
count++; | ||
n &= n - 1; | ||
} | ||
return count; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class Solution { | ||
public boolean isPowerOfTwo(int n) { | ||
//溢出 | ||
if (n == -n) return false; | ||
return n != 0 && (n & (n - 1)) == 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class Solution { | ||
public boolean isAnagram(String s, String t) { | ||
int[] table1 = new int[26]; | ||
int[] table2 = new int[26]; | ||
for (int i = 0; i < s.length(); i++) { | ||
table1[s.charAt(i) - 'a']++; | ||
} | ||
for (int i = 0; i < t.length(); i++) { | ||
table2[t.charAt(i) - 'a']++; | ||
} | ||
for (int i = 0; i < table1.length; i++) { | ||
if (table1[i] != table2[i]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class Solution { | ||
public int[] countBits(int num) { | ||
int[] results = new int[num + 1]; | ||
for (int i = 1; i <= num; i++) { | ||
results[i] = results[i & (i - 1)] + 1; | ||
} | ||
return results; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
class Solution { | ||
public int reversePairs(int[] nums) { | ||
return mergeSort(nums, 0, nums.length - 1); | ||
} | ||
private void merge(int[] nums, int begin, int mid, int end) { | ||
int[] temp = new int[end - begin + 1]; | ||
int i = begin, j = mid + 1, pos = 0; | ||
while (i <= mid && j <= end) { | ||
if (nums[i] < nums[j]) { | ||
temp[pos++] = nums[i++]; | ||
} else { | ||
temp[pos++] = nums[j++]; | ||
} | ||
} | ||
while (i <= mid) { | ||
temp[pos++] = nums[i++]; | ||
} | ||
while (j <= end) { | ||
temp[pos++] = nums[j++]; | ||
} | ||
System.arraycopy(temp, 0, nums, begin, end - begin + 1); | ||
} | ||
private int mergeSort(int[] nums, int begin, int end) { | ||
if (begin >= end) { | ||
return 0; | ||
} | ||
int mid = begin + ((end - begin) >> 2); | ||
int result = mergeSort(nums, begin, mid) + mergeSort(nums, mid + 1, end); | ||
for (int i = begin, j = mid + 1; i <= mid; i++) { | ||
while (j <= end && (nums[i] > nums[j] * 2l)) j++; | ||
result += j - (mid + 1); | ||
} | ||
merge(nums, begin, mid, end); | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
class Solution { | ||
private int N; | ||
private int[] rows; | ||
private int[] hills; | ||
private int[] delas; | ||
private int[] queens; | ||
private List<List<String>> results = new ArrayList<>(); | ||
|
||
public boolean isOccupied(int row, int col) { | ||
int constant = rows[col] + hills[row - col + 2 * N] + delas[row + col]; | ||
return constant > 0; | ||
} | ||
|
||
public void placeQueen(int row, int col) { | ||
rows[col] = 1; //该行已放置 | ||
hills[row - col + 2 * N] = 1; //左斜 | ||
delas[row + col] = 1; //右斜 | ||
queens[row] = col; //记录放置位置 | ||
} | ||
|
||
public void removeQueen(int row, int col) { | ||
rows[col] = 0; | ||
hills[row - col + 2 * N] = 0; | ||
delas[row + col] = 0; | ||
queens[row] = 0; | ||
} | ||
|
||
public void nQueensByDfs(int n, int limit) { | ||
if (n == limit) { | ||
List<String> rect = new ArrayList<>(); | ||
for (int i = 0; i < limit; i++) { | ||
StringBuilder builder = new StringBuilder(); | ||
int col = queens[i]; | ||
for (int j = 0; j < col; j++) { | ||
builder.append('.'); | ||
} | ||
builder.append('Q'); | ||
for (int j = col + 1; j < limit; j++) { | ||
builder.append('.'); | ||
} | ||
rect.add(builder.toString()); | ||
} | ||
results.add(rect); | ||
return; | ||
} | ||
|
||
for (int j = 0; j < limit; j++) { | ||
if (!isOccupied(n, j)) { | ||
placeQueen(n, j); | ||
nQueensByDfs(n + 1, limit); | ||
removeQueen(n, j); | ||
} | ||
} | ||
} | ||
|
||
public List<List<String>> solveNQueens(int n) { | ||
rows = new int[n]; | ||
hills = new int[4 * n]; | ||
delas = new int[2 * n]; | ||
queens = new int[n]; | ||
N = n; | ||
nQueensByDfs(0, n); | ||
return results; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
class Solution { | ||
private int count; | ||
private void totalNQueensByDfsWithBin(int n, int limit, int rows, int hills, int dales) { | ||
if (n >= limit) { | ||
count++; | ||
return; | ||
} | ||
// int pos = rows[n] + hills[row - col + 2 * n] + dales[row + col]; | ||
// 下面都使用 1字节有符号整型来设例。 | ||
// 这里直接整体上运算一次,最后看 n 的位置是否为 1 (已经求反) | ||
int free_cols = ~(rows | hills | dales) & ((1 << limit) - 1); | ||
// 有位置可以使用(0000(2) => 0(10):没有位置,1111 => 15(10):所有位置可用) | ||
while (free_cols != 0) { | ||
// 取得放置后的位置 00001110 & 10001110(反码加1后为 00000010) :1110 & 0010 => 0010 | ||
int cur_cols = free_cols & -free_cols; | ||
// 通过函数参数固定放置结果 | ||
totalNQueensByDfsWithBin(n + 1, limit, rows | cur_cols, (hills | cur_cols) << 1, (dales | cur_cols) >> 1); | ||
free_cols = free_cols & (free_cols - 1); | ||
} | ||
} | ||
public int totalNQueens(int n) { | ||
count = 0; | ||
totalNQueensByDfsWithBin(0, n, 0, 0, 0); | ||
return count; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
class Solution { | ||
class IntervalComparator implements Comparator<int[]> { | ||
@Override | ||
public int compare(int[] a, int[] b) { | ||
return a[0] < b[0] ? -1 : a[0] == b[0] ? 0 : 1; | ||
} | ||
} | ||
public int[][] merge(int[][] intervals) { | ||
List<int[]> results = new ArrayList<>(); | ||
Map<Integer, Boolean> deleted = new HashMap<>(); | ||
Arrays.sort(intervals, new IntervalComparator()); | ||
for (int i = 1; i < intervals.length; i++) { | ||
if (intervals[i - 1][0] <= intervals[i][0] && intervals[i][0] <= intervals[i - 1][1]) { | ||
intervals[i][0] = intervals[i - 1][0]; | ||
intervals[i][1] = Math.max(intervals[i - 1][1], intervals[i][1]); | ||
deleted.put(i - 1, false); | ||
} | ||
} | ||
for (int i = 0; i < intervals.length; i++) { | ||
if (!deleted.containsKey(i)) | ||
results.add(intervals[i]); | ||
} | ||
return results.toArray(new int[results.size()][2]); | ||
} | ||
} |