Skip to content

Commit

Permalink
Merge pull request algorithm004-04#1026 from zenglinhui/master
Browse files Browse the repository at this point in the history
044-Week 07
  • Loading branch information
Daisy3485 authored Dec 4, 2019
2 parents 340e917 + b4b6ae3 commit b59e97b
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 0 deletions.
32 changes: 32 additions & 0 deletions Week 07/id_044/LeetCode_044_146.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package week_07.lesson17;

import java.util.LinkedHashMap;
import java.util.Map;

public class LRUCache extends LinkedHashMap<Integer, Integer> {

private int capacity;

public LRUCache(int capacity) {
super(capacity,0.75F,true);
this.capacity = capacity;
}

public int get(int key) {
return super.getOrDefault(key, -1);
}

public void put(int key, int value) {
super.put(key,value);
}

@Override
protected boolean removeEldestEntry(Map.Entry<Integer, Integer> eldest) {
return size() > capacity;
}

public static void main(String[] args) {

}

}
23 changes: 23 additions & 0 deletions Week 07/id_044/LeetCode_044_190.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package week_07.lesson16;

public class ReverseBits {

public int reverseBits(int n) {
int result = 0;
for (int i = 0; i < 32; i++) {
//右移i位
int tmp = n >> i;
//有效位是否为1
tmp = tmp & 1;
//反转位置
tmp = tmp << (31 - i);
//
result = result | tmp;
}
return result;
}

public static void main(String[] args) {

}
}
30 changes: 30 additions & 0 deletions Week 07/id_044/LeetCode_044_191.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package week_07.lesson16;

public class NumberOfOneBits {

public int hammingWeight(int n) {
/*int bits = 0;
//循环加标记法,每次把标记左移一位,然后&上n,是否为1
int mask = 1;
for (int i = 0; i < 32; i++) {
if (0 != (n & mask)) {
bits++;
}
mask = mask << 1;
}
return bits;*/

//用n&(n-1)来运算,在二进制表示中,总是会把最低位(也可理解成最后一位)的1变成0
int sum = 0;
while (n != 0) {
sum++;
n = n & (n - 1);
}
return sum;
}

public static void main(String[] args) {

}

}
26 changes: 26 additions & 0 deletions Week 07/id_044/LeetCode_044_231.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package week_07.lesson16;

public class PowerOfTwo {

/**
* 若 n = 2^x 且 x 为自然数(即 n 为 2 的幂),则一定满足以下条件:
* 1.恒有 n & (n - 1) == 0,这是因为:
* n 二进制最高位为1,其余所有位为0;
* 2^0 --> 0001
* 2^1 --> 0010
* 2^2 --> 0100
* 2^3 --> 1000
* n - 1 二进制最高位为0,其余所有位为1;
* 2.一定满足 n > 0
* @param n
* @return
*/
public boolean isPowerOfTwo(int n) {
return n > 0 && (n & (n - 1)) == 0;
}

public static void main(String[] args) {

}

}
45 changes: 45 additions & 0 deletions Week 07/id_044/LeetCode_044_242.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package week_07.lesson18;

import java.util.Arrays;

public class ValidAnagram {


public static boolean isAnagram(String s, String t) {

/*if (s.length() != t.length()) {
return false;
}
int[] counter = new int[26];
int[] diffcount = new int[26];
for (int i = 0; i < s.length(); i++) {
counter[s.charAt(i) - 'a']++;
counter[t.charAt(i) - 'a']--;
}
return Arrays.equals(counter,diffcount);*/

if (s.length() != t.length()) {
return false;
}

int[] counter = new int[26];
for (int i = 0; i < s.length(); i++) {
counter[s.charAt(i) - 'a']++;
counter[t.charAt(i) - 'a']--;
}
for (int i = 0; i < s.length(); i++) {
if (counter[s.charAt(i) - 'a'] != 0) {
return false;
}
}
return true;

}

public static void main(String[] args) {

}

}
53 changes: 53 additions & 0 deletions Week 07/id_044/NOTE.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,57 @@
# NOTE
//冒泡排序
private static void bubbleSort(int[] arr) {

int len = arr.length;
for (int i = 0; i < len - 1; i++) {
for (int j = 0; j < len - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = temp;
}
}
}

}

//选择排序
private static void selectionSort(int[] arr) {
int len = arr.length;
int minIndex,temp;
for (int i = 0; i < len - 1; i++) {
minIndex = i;
for (int j = i + 1; j < len; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}

//插入排序
private static void insertionSort(int[] arr) {
int len = arr.length;
int preIndex,current;
for (int i = 1; i < len; i++) {
preIndex = i - 1;
current = arr[i];
//从preIndex位置开始,向前查找,如果前一个元素比后一个元素大,则交换
while (preIndex > -1 && arr[preIndex] > current) {
arr[preIndex + 1] = arr[preIndex];
preIndex--;
}
arr[preIndex + 1] = current;
}
}








0 comments on commit b59e97b

Please sign in to comment.