forked from algorithm004-05/algorithm004-05
-
Notifications
You must be signed in to change notification settings - Fork 0
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 algorithm004-05#1108 from poulen/master
390-week 07
- Loading branch information
Showing
11 changed files
with
568 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,58 @@ | ||
//给你两个数组,arr1 和 arr2, | ||
// | ||
// | ||
// arr2 中的元素各不相同 | ||
// arr2 中的每个元素都出现在 arr1 中 | ||
// | ||
// | ||
// 对 arr1 中的元素进行排序,使 arr1 中项的相对顺序和 arr2 中的相对顺序相同。未在 arr2 中出现过的元素需要按照升序放在 arr1 的末尾。 | ||
// | ||
// | ||
// | ||
// 示例: | ||
// | ||
// 输入:arr1 = [2,3,1,3,2,4,6,7,9,2,19], arr2 = [2,1,4,3,9,6] | ||
//输出:[2,2,2,1,4,3,3,9,6,7,19] | ||
// | ||
// | ||
// | ||
// | ||
// 提示: | ||
// | ||
// | ||
// arr1.length, arr2.length <= 1000 | ||
// 0 <= arr1[i], arr2[i] <= 1000 | ||
// arr2 中的元素 arr2[i] 各不相同 | ||
// arr2 中的每个元素 arr2[i] 都出现在 arr1 中 | ||
// | ||
// Related Topics 排序 数组 | ||
|
||
|
||
|
||
//leetcode submit region begin(Prohibit modification and deletion) | ||
// 1. 计数排序 | ||
class Solution { | ||
public int[] relativeSortArray(int[] arr1, int[] arr2) { | ||
if (arr1.length == 0 || arr2.length == 0) {return arr1;} | ||
int[] all = new int[1001]; | ||
int[] result = new int[arr1.length]; | ||
for (int num : arr1) { | ||
all[num]++; | ||
} | ||
int index = 0; | ||
for (int num : arr2) { | ||
while (all[num] > 0) { | ||
result[index++] = num; | ||
all[num]--; | ||
} | ||
} | ||
for (int i = 0; i < all.length; i++) { | ||
while (all[i] > 0) { | ||
result[index++] = i; | ||
all[i]--; | ||
} | ||
} | ||
return result; | ||
} | ||
} | ||
//leetcode submit region end(Prohibit modification and deletion) |
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,55 @@ | ||
//运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制。它应该支持以下操作: 获取数据 get 和 写入数据 put 。 | ||
// | ||
// 获取数据 get(key) - 如果密钥 (key) 存在于缓存中,则获取密钥的值(总是正数),否则返回 -1。 | ||
//写入数据 put(key, value) - 如果密钥不存在,则写入其数据值。当缓存容量达到上限时,它应该在写入新数据之前删除最近最少使用的数据值,从而为新的数据值留出空间。 | ||
// | ||
// 进阶: | ||
// | ||
// 你是否可以在 O(1) 时间复杂度内完成这两种操作? | ||
// | ||
// 示例: | ||
// | ||
// LRUCache cache = new LRUCache( 2 /* 缓存容量 */ ); | ||
// | ||
//cache.put(1, 1); | ||
//cache.put(2, 2); | ||
//cache.get(1); // 返回 1 | ||
//cache.put(3, 3); // 该操作会使得密钥 2 作废 | ||
//cache.get(2); // 返回 -1 (未找到) | ||
//cache.put(4, 4); // 该操作会使得密钥 1 作废 | ||
//cache.get(1); // 返回 -1 (未找到) | ||
//cache.get(3); // 返回 3 | ||
//cache.get(4); // 返回 4 | ||
// | ||
// Related Topics 设计 | ||
|
||
|
||
|
||
//leetcode submit region begin(Prohibit modification and deletion) | ||
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; | ||
} | ||
} | ||
|
||
/** | ||
* 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); | ||
*/ | ||
//leetcode submit region end(Prohibit modification and deletion) |
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,49 @@ | ||
//颠倒给定的 32 位无符号整数的二进制位。 | ||
// | ||
// | ||
// | ||
// 示例 1: | ||
// | ||
// 输入: 00000010100101000001111010011100 | ||
//输出: 00111001011110000010100101000000 | ||
//解释: 输入的二进制串 00000010100101000001111010011100 表示无符号整数 43261596, | ||
// 因此返回 964176192,其二进制表示形式为 00111001011110000010100101000000。 | ||
// | ||
// 示例 2: | ||
// | ||
// 输入:11111111111111111111111111111101 | ||
//输出:10111111111111111111111111111111 | ||
//解释:输入的二进制串 11111111111111111111111111111101 表示无符号整数 4294967293, | ||
// 因此返回 3221225471 其二进制表示形式为 10101111110010110010011101101001。 | ||
// | ||
// | ||
// | ||
// 提示: | ||
// | ||
// | ||
// 请注意,在某些语言(如 Java)中,没有无符号整数类型。在这种情况下,输入和输出都将被指定为有符号整数类型,并且不应影响您的实现,因为无论整数是有符号的还是无符号的,其内部的二进制表示形式都是相同的。 | ||
// 在 Java 中,编译器使用二进制补码记法来表示有符号整数。因此,在上面的 示例 2 中,输入表示有符号整数 -3,输出表示有符号整数 -1073741825。 | ||
// | ||
// | ||
// | ||
// | ||
// 进阶: | ||
//如果多次调用这个函数,你将如何优化你的算法? | ||
// Related Topics 位运算 | ||
|
||
|
||
|
||
//leetcode submit region begin(Prohibit modification and deletion) | ||
// 1. 位运算 时间O(1) 空间O(1) | ||
public class Solution { | ||
// you need treat n as an unsigned value | ||
public int reverseBits(int n) { | ||
int result = 0; | ||
for (int i = 0; i < 32; i++) { | ||
result = (result << 1) + (n & 1); | ||
n >>= 1; | ||
} | ||
return result; | ||
} | ||
} | ||
//leetcode submit region end(Prohibit modification and deletion) |
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,55 @@ | ||
//编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 ‘1’ 的个数(也被称为汉明重量)。 | ||
// | ||
// | ||
// | ||
// 示例 1: | ||
// | ||
// 输入:00000000000000000000000000001011 | ||
//输出:3 | ||
//解释:输入的二进制串 00000000000000000000000000001011 中,共有三位为 '1'。 | ||
// | ||
// | ||
// 示例 2: | ||
// | ||
// 输入:00000000000000000000000010000000 | ||
//输出:1 | ||
//解释:输入的二进制串 00000000000000000000000010000000 中,共有一位为 '1'。 | ||
// | ||
// | ||
// 示例 3: | ||
// | ||
// 输入:11111111111111111111111111111101 | ||
//输出:31 | ||
//解释:输入的二进制串 11111111111111111111111111111101 中,共有 31 位为 '1'。 | ||
// | ||
// | ||
// | ||
// 提示: | ||
// | ||
// | ||
// 请注意,在某些语言(如 Java)中,没有无符号整数类型。在这种情况下,输入和输出都将被指定为有符号整数类型,并且不应影响您的实现,因为无论整数是有符号的还是无符号的,其内部的二进制表示形式都是相同的。 | ||
// 在 Java 中,编译器使用二进制补码记法来表示有符号整数。因此,在上面的 示例 3 中,输入表示有符号整数 -3。 | ||
// | ||
// | ||
// | ||
// | ||
// 进阶: | ||
//如果多次调用这个函数,你将如何优化你的算法? | ||
// Related Topics 位运算 | ||
|
||
|
||
|
||
//leetcode submit region begin(Prohibit modification and deletion) | ||
// 1.位运算 时间O(1) 空间O(1) | ||
public class Solution { | ||
// you need to treat n as an unsigned value | ||
public int hammingWeight(int n) { | ||
int count = 0; | ||
while (n != 0) { | ||
n = n & (n - 1); | ||
count ++; | ||
} | ||
return count; | ||
} | ||
} | ||
//leetcode submit region end(Prohibit modification and deletion) |
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,30 @@ | ||
//给定一个整数,编写一个函数来判断它是否是 2 的幂次方。 | ||
// | ||
// 示例 1: | ||
// | ||
// 输入: 1 | ||
//输出: true | ||
//解释: 20 = 1 | ||
// | ||
// 示例 2: | ||
// | ||
// 输入: 16 | ||
//输出: true | ||
//解释: 24 = 16 | ||
// | ||
// 示例 3: | ||
// | ||
// 输入: 218 | ||
//输出: false | ||
// Related Topics 位运算 数学 | ||
|
||
|
||
|
||
//leetcode submit region begin(Prohibit modification and deletion) | ||
// 1. 位运算 时间O(1) 空间O(1) | ||
class Solution { | ||
public boolean isPowerOfTwo(int n) { | ||
return n > 0 && (n & (n - 1)) == 0; | ||
} | ||
} | ||
//leetcode submit region end(Prohibit modification and deletion) |
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,43 @@ | ||
//给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。 | ||
// | ||
// 示例 1: | ||
// | ||
// 输入: s = "anagram", t = "nagaram" | ||
//输出: true | ||
// | ||
// | ||
// 示例 2: | ||
// | ||
// 输入: s = "rat", t = "car" | ||
//输出: false | ||
// | ||
// 说明: | ||
//你可以假设字符串只包含小写字母。 | ||
// | ||
// 进阶: | ||
//如果输入字符串包含 unicode 字符怎么办?你能否调整你的解法来应对这种情况? | ||
// Related Topics 排序 哈希表 | ||
|
||
|
||
|
||
//leetcode submit region begin(Prohibit modification and deletion) | ||
// 3. 数组计数法 | ||
class Solution { | ||
public boolean isAnagram(String s, String t) { | ||
if (s.length() != t.length()) { | ||
return false; | ||
} | ||
int[] count = new int[26]; | ||
for (int i = 0; i < s.length(); i++) { | ||
count[s.charAt(i) - 'a'] ++; | ||
count[t.charAt(i) - 'a'] --; | ||
} | ||
for (int i : count) { | ||
if (i != 0) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} | ||
//leetcode submit region end(Prohibit modification and deletion) |
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,42 @@ | ||
//给定一个非负整数 num。对于 0 ≤ i ≤ num 范围中的每个数字 i ,计算其二进制数中的 1 的数目并将它们作为数组返回。 | ||
// | ||
// 示例 1: | ||
// | ||
// 输入: 2 | ||
//输出: [0,1,1] | ||
// | ||
// 示例 2: | ||
// | ||
// 输入: 5 | ||
//输出: [0,1,1,2,1,2] | ||
// | ||
// 进阶: | ||
// | ||
// | ||
// 给出时间复杂度为O(n*sizeof(integer))的解答非常容易。但你可以在线性时间O(n)内用一趟扫描做到吗? | ||
// 要求算法的空间复杂度为O(n)。 | ||
// 你能进一步完善解法吗?要求在C++或任何其他语言中不使用任何内置函数(如 C++ 中的 __builtin_popcount)来执行此操作。 | ||
// | ||
// Related Topics 位运算 动态规划 | ||
|
||
|
||
|
||
//leetcode submit region begin(Prohibit modification and deletion) | ||
// 2. 动态规划+最高有效位 时间O(n) 空间O(n) | ||
class Solution { | ||
public int[] countBits(int num) { | ||
int[] result = new int[num + 1]; | ||
int count = 0; | ||
int bit = 1; | ||
while (bit <= num) { | ||
while (bit + count <= num && count < bit) { | ||
result[bit + count] = result[count] + 1; | ||
count++; | ||
} | ||
count = 0; | ||
bit <<= 1; | ||
} | ||
return result; | ||
} | ||
} | ||
//leetcode submit region end(Prohibit modification and deletion) |
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,51 @@ | ||
//给定一个数组 nums ,如果 i < j 且 nums[i] > 2*nums[j] 我们就将 (i, j) 称作一个重要翻转对。 | ||
// | ||
// 你需要返回给定数组中的重要翻转对的数量。 | ||
// | ||
// 示例 1: | ||
// | ||
// | ||
//输入: [1,3,2,3,1] | ||
//输出: 2 | ||
// | ||
// | ||
// 示例 2: | ||
// | ||
// | ||
//输入: [2,4,3,5,1] | ||
//输出: 3 | ||
// | ||
// | ||
// 注意: | ||
// | ||
// | ||
// 给定数组的长度不会超过50000。 | ||
// 输入数组中的所有数字都在32位整数的表示范围内。 | ||
// | ||
// Related Topics 排序 树状数组 线段树 二分查找 分治算法 | ||
|
||
|
||
|
||
//leetcode submit region begin(Prohibit modification and deletion) | ||
// 1. 归并排序 | ||
class Solution { | ||
public int reversePairs(int[] nums) { | ||
return merge(nums, 0, nums.length - 1); | ||
} | ||
private int merge (int[] nums, int start, int end) { | ||
if (start >= end) { | ||
return 0; | ||
} | ||
int mid = (start + end) >> 1; | ||
int cnt = merge(nums, start, mid) + merge(nums, mid + 1, end); | ||
for (int i = start, j = mid + 1; i <= mid; i++) { | ||
while (j <= end && nums[i] / 2.0 > nums[j]) { | ||
j++; | ||
} | ||
cnt += j - (mid + 1); | ||
} | ||
Arrays.sort(nums, start, end + 1); | ||
return cnt; | ||
} | ||
} | ||
//leetcode submit region end(Prohibit modification and deletion) |
Oops, something went wrong.