Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

675-Week 07 #1480

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Week 08/id_675/LeetCode_557_675.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class Solution {
public String reverseWords(String s) {
String words[] = s.split(" ");
StringBuilder res=new StringBuilder();
for (String word: words)
res.append(new StringBuffer(word).reverse().toString() + " ");
return res.toString().trim();
}
}
28 changes: 28 additions & 0 deletions Week 08/id_675/LeetCode_91_675.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution {
public int numDecodings(String s) {
if (s == null || s.length() == 0) {
return 0;
}
return digui(s, 0);
}

private int digui(String s, int start) {
if (s.length() == start) {
return 1;
}
if (s.charAt(start) == '0') {
return 0;
}
int ans1 = digui(s, start + 1);
int ans2 = 0;
if (start < s.length() - 1) {
int ten = (s.charAt(start) - '0') * 10;
int one = (s.charAt(start + 1) - '0');
if (ten + one <= 26) {
ans2 = digui(s, start + 2);
}
}
return ans1 + ans2;
}

}
10 changes: 10 additions & 0 deletions Week 3/id_675/LeetCode_122_675.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Solution {
public int maxProfit(int[] prices) {
int maxprofit = 0;
for (int i = 1; i < prices.length; i++) {
if (prices[i] > prices[i - 1])
maxprofit += prices[i] - prices[i - 1];
}
return maxprofit;
}
}
36 changes: 36 additions & 0 deletions Week 3/id_675/LeetCode_200_675.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class Solution {
void dfs(char[][] grid, int r, int c) {
int nr = grid.length;
int nc = grid[0].length;

if (r < 0 || c < 0 || r >= nr || c >= nc || grid[r][c] == '0') {
return;
}

grid[r][c] = '0';
dfs(grid, r - 1, c);
dfs(grid, r + 1, c);
dfs(grid, r, c - 1);
dfs(grid, r, c + 1);
}

public int numIslands(char[][] grid) {
if (grid == null || grid.length == 0) {
return 0;
}

int nr = grid.length;
int nc = grid[0].length;
int num_islands = 0;
for (int r = 0; r < nr; ++r) {
for (int c = 0; c < nc; ++c) {
if (grid[r][c] == '1') {
++num_islands;
dfs(grid, r, c);
}
}
}

return num_islands;
}
}
75 changes: 75 additions & 0 deletions Week 7/id_675/LeetCode_1122_675.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//给你两个数组,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 排序 数组


import java.time.chrono.IsoChronology;
import java.util.Arrays;
import java.util.HashMap;

//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int[] relativeSortArray(int[] arr1, int[] arr2) {

int[] temp = new int[1001];

for (int a : arr1){
temp[a] = temp[a] + 1;
}

int i = 0;

for(int num:arr2){
while(temp[num]-- > 0){
arr1[i++] = num;
}
}

for(int j = 0; j < 1001; ++j){
while(temp[j]-- > 0){
arr1[i++] = j;
}
}
return arr1;

// int first = 0;
// for (int i = 0;i<arr2.length;i++){
// for (int j = 0;j<arr1.length;j++){
// if (arr1[j] == arr2[i]){
// arr1[j] = arr1[first];
// arr1[first] = arr2[i];
// first = first +1;
// }
// }
// }
// System.out.println(first);
// Arrays.sort(arr1,first,arr1.length-1);
// return arr1;
}
}
//leetcode submit region end(Prohibit modification and deletion)
64 changes: 64 additions & 0 deletions Week 7/id_675/LeetCode_146_675.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//运用你所掌握的数据结构,设计和实现一个 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 设计


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

//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)