class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
if(amount==0){
return 0;
}
sort(coins.begin(),coins.end());
int dp[amount+1];
for(int i=0;i<=amount;i++){
dp[i] = INT32_MAX-1;//先都初始化为最大值(-1的目的是为了在后面加1的时候不要溢出)
}
for(int i=0;i<coins.size();i++){
if(coins[i]>amount){
break;//如果面额比金额大,直接截断
}
dp[coins[i]]=1;//如果面额等于金额,就是1张币
}
for(int i=1;i<=amount;i++){
for(int j=0;j<coins.size();j++){
if(coins[j]>i){
break;
}
dp[i] = min(dp[i],dp[i-coins[j]]+1);//dp[i-coins[j]]本质上就是类似“11-5==6”
}
}
return dp[amount]==INT32_MAX-1?-1:dp[amount];
}
};
作者:rubisco-777
链接:https://leetcode-cn.com/problems/coin-change/solution/cdong-tai-gui-hua-60ms96mb-by-rubisco-77-2knx/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
这个可能是触及到我的思维盲区了,看了半天,还在b站上找了一个视频,才看懂的。
1217.求奇数偶数个数
var minCostToMoveChips = function(position){
let odd = even = 0;
for(const p of position){
p & 1 ? odd++ : even++;
}
return Math.min(odd,even);
};
作者:javascript-8
链接:https://leetcode-cn.com/problems/minimum-cost-to-move-chips-to-the-same-position/solution/tan-xin-si-xiang-qiu-qi-ou-shu-de-ge-shu-3ik1/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
55.跳跃游戏
感觉贪心算法的重点是要每一步都进行更新,
class Solution {
public:
bool canJump(vector<int>& nums) {
int n = nums.size();
int rightmost = 0;
for (int i = 0; i < n; ++i) {
if (i <= rightmost) {
rightmost = max(rightmost, i + nums[i]);
if (rightmost >= n - 1) {
return true;
}
}
}
return false;
}
};
bool canJump(vector<int>& A){
int n = A.size(); last = n-1;
for(int i=n-2; i>=0; --i){
if(i + A[i] >= last){
last = i;
}
return last == 0;
}
}
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/jump-game/solution/tiao-yue-you-xi-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
下图是leecode官方教程
动态规划*************************************************************************************************
509.斐波那契数列
滚动:
func fib(n int) int {
if n < 2 {
return n
}
p, q, r := 0, 0, 1
for i := 2; i <= n; i++ {
p = q
q = r
r = p + q
}
return r
}
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/fibonacci-number/solution/fei-bo-na-qi-shu-by-leetcode-solution-o4ze/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
62.不同路径(感觉也是在更新)
class Solution {
public:
int uniquePaths(int m, int n) {
vector<vector<int>> f(m, vector<int>(n));
for (int i = 0; i < m; ++i) {
f[i][0] = 1;
}
for (int j = 0; j < n; ++j) {
f[0][j] = 1;
}
for (int i = 1; i < m; ++i) {
for (int j = 1; j < n; ++j) {
f[i][j] = f[i - 1][j] + f[i][j - 1];
}
}
return f[m - 1][n - 1];
}
};
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/unique-paths/solution/bu-tong-lu-jing-by-leetcode-solution-hzjf/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
720.词典中最长单词
class Solution {
public:
string longestWord(vector<string>& words) {
sort(words.begin(), words.end());//从小到大排序
unordered_set<string> mySet;
string ans = ""; //从空开始记录
mySet.insert(ans);
for(auto& word: words){
if(mySet.find(string(word.begin(), word.end() - 1)) != mySet.end()){
if(word.size() > ans.size()) ans = word; //如果长度更长,更新答案
mySet.insert(word); //记录这个单词
}
}
return ans;
}
};
作者:MGA_Bronya
链接:https://leetcode-cn.com/problems/longest-word-in-dictionary/solution/720-ci-dian-zhong-zui-chang-de-dan-ci-ha-n5cs/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
func longestWord(words []string) string {
hashMap:=map[string]int{}
sort.Strings(words)
fmt.Println(words)
result:=""
for _,word:=range words {
if len(word) == 1 {
hashMap[word]++
if len(result) == 0 {
result = word
}
continue
}
if _,ok := hashMap[word[0:len(word)-1]];ok {
hashMap[word]++
if len(result) < len(word) {
result = word
}
}
}
return result
}
作者:cfc-9
链接:https://leetcode-cn.com/problems/longest-word-in-dictionary/solution/golang-pai-xu-ha-xi-biao-by-cfc-9-fpxt/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
class Trie{
private:
bool is_string;
Trie *next[26];
public:
Trie(){
is_string=false;
memset(next,0,sizeof(next));
}
void insert(string word){
Trie *root=this;
for(const auto& w:word){
if(root->next[w-'a']==nullptr)root->next[w-'a']=new Trie();
root=root->next[w-'a'];
}
root->is_string=true;
}
bool search(string word){
Trie *root=this;
for(const auto& w:word){
//当节点值存在时,判断该节点是否表示为一个字符串,不是的话,直接返回false,否则继续循环;当节点值不存在时直接返回false
if(root->next[w-'a']==nullptr||root->next[w-'a']->is_string==false)return false;
root=root->next[w-'a'];
}
return true;
}
};
class Solution {
public:
string longestWord(vector<string>& words) {
if(words.size()==0)return "";
Trie* root=new Trie();
//第一次遍历,建立前缀树
for(const auto& word:words)
root->insert(word);
string result="";
//第二次遍历,寻找最长单词
for(const auto& word:words){
if(root->search(word))
{
if(word.size()>result.size())result=word;//更新最长单词
else if(word.size()==result.size()&&word<result)result=word;//长度相等的单词,取字典序小的单词
}
}
return result;
}
};
作者:xiaoneng
链接:https://leetcode-cn.com/problems/longest-word-in-dictionary/solution/cqian-zhui-shu-zui-jian-mo-ban-by-xiaoneng/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。