Skip to content

补169,221,309,714 #793

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

Open
wants to merge 1 commit 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
30 changes: 30 additions & 0 deletions Week_04/id_6/LeetCode_169_6.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@


// LeetCode 169



// 此次 从n/2分开,分别统计数字出现次数
// 多种数字的话,可以用散列表<num, 次数>保存
public int majorityElement(int[] nums) {
int n = nums.length;
if (n <= 2){
return nums[0];
}

HashMap<Integer, Integer> map = new HashMap<>();
int i = 0;
for (i = 0; i < n; i++) {
if (map.containsKey(nums[i])){
int m = map.get(nums[i]) + 1;
if (m > n/2){
break;
}
map.put(nums[i], m);
}else {
map.put(nums[i], 1);
}
}

return nums[i];
}
79 changes: 79 additions & 0 deletions Week_04/id_6/LeetCode_221_6.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@


// LeetCode 309


// [ 1. Trie树 + 回溯算法 ]

class TrieNode {

public char data;
public TrieNode[] children = new TrieNode[26];
public boolean isEndingChar = false;
public TrieNode(char data){
this.data = data;
}
}

// root
TrieNode root = new TrieNode('\\');

public void addWord(String word){
char[] wd = word.toCharArray();

TrieNode p = root;
// 遍历单词,在Tire中查找
for (int i = 0; i < wd.length; i++) {
int index = wd[i] - 'a';
// 字符不存在
if (p.children[index] == null){
p.children[index] = new TrieNode(wd[i]);
}
p = p.children[index];
}
p.isEndingChar = true;
}

public boolean search(String word){
TrieNode p = root;

return find(word, root);
}


// 先按前缀匹配在Trie树中查找
// 遇到‘.’时,从该点children钟任意一个存在字符向下去匹配,dfs+回溯
public boolean find(String word, TrieNode curr){
TrieNode p = curr;

for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if (c == '.'){
// 从任何一个children[index]!=null 递归的去查找
// 如果匹配失败,回溯到上一个点,继续
for (int j = 0; j < p.children.length; j++) {
if (p.children[j] != null){
String wd = word.substring(i+1, word.length());
if (find(wd, p.children[j])){
return true;
}
}
}
return false;
}else {
int index = c - 'a';
if (p.children[index] != null){
p = p.children[index];
}else {
return false;
}
}

}
// 不是结尾
if (!p.isEndingChar){
return false;
}

return true;
}
31 changes: 31 additions & 0 deletions Week_04/id_6/LeetCode_309_6.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@


// LeetCode 309


// [ 1. 动态规划 ]

public int maxProfit(int[] prices) {
int len = prices.length;
if (len == 0){
return 0;
}

int[] buy = new int[len];
int[] sell = new int[len];

buy[0] = 0 - prices[0];
sell[0] = 0;

for (int i = 1; i < len; i++) {
if (i == 1){
buy[i] = Math.max(sell[0]-prices[i], buy[i-1]);
}else {
buy[i] = Math.max(sell[i-2]-prices[i], buy[i-1]);
}

sell[i] = Math.max(prices[i]+buy[i-1], sell[i-1]);
}

return Math.max(sell[len-1], buy[len-1]);
}
60 changes: 60 additions & 0 deletions Week_04/id_6/LeetCode_714_6.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@


// LeetCode 714


// [ 1. 贪心算法 ]
// 1)问题抽象:n天股票价格中,抽取某些天进行买卖
// 2)利润最大:最低价买,最高价卖
// ?? 手续费起什么作用 减手续费后的利润如果小于0,则不卖
public int maxProfit(int[] prices, int fee) {
if (prices.length <= 1){
return 0;
}

int minPirce = prices[0];
int sum = 0;

for (int i = 1; i < prices.length; i++) {
if (prices[i] <= minPirce){
minPirce = prices[i];
}else {
// 当天价格卖出利润大于0
int n = prices[i] - minPirce - fee;
if (n > 0){
sum += n;
// 后面有可能存在利润更大的,如果出现这种情况,把那部分利润补回来就行了
// ??
minPirce = prices[i] - fee;
}
}
}
return sum;
}




// [ 1. 动态规划 ]

public int maxProfit(int[] prices, int fee) {
if (prices.length <= 1){
return 0;
}

int len = prices.length;
int[] buy = new int[len];
int[] sell = new int[len];

// 第一天
buy[0] = 0-prices[0];
sell[0] = prices[0];

// 推导公式
for (int i = 1; i < prices.length; i++) {
buy[i] = Math.max(buy[i-1], sell[i-1]-prices[i]);
sell[i] = Math.max(sell[i-1], buy[i-1]+prices[i]-fee);
}

return Math.max(buy[len-1], sell[len-1]);
}