首页 > 其他分享 >LeetCode刷题记录.Day9

LeetCode刷题记录.Day9

时间:2022-11-09 00:13:10浏览次数:79  
标签:map set return Day9 int sum LeetCode 刷题

快乐数

题目链接202. 快乐数 - 力扣(LeetCode)

class Solution {
public:
    int getSum(int n){
        int sum = 0;
        while(n){
            sum += (n % 10) * (n % 10);
            n /= 10;
        }
        return sum;
    }
    bool isHappy(int n) {
        unordered_set<int> set;
        while(1){
            int sum = getSum(n);
            if (sum == 1){
                return true;
            }
            if(set.find(sum) != set.end()){
                return false;
            }
            else{
                set.insert(sum);
            }
            n = sum;
        }
    }
};

整体不算难。抽象出问题后发现只存在两种情况,第一种,经过运算后结果唯一。第二种,经过运算后无限循环

然后无限循环的时候必定会存在两次运算的结果相等的情况,所以可以用哈希表方法,记录下每次运算结果,快速查找每次运算结果是否和上一次重复,如果重复,必定为false。

两数之和

题目链接1. 两数之和 - 力扣(LeetCode)

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        std::unordered_map <int,int> map;
        for(int i = 0; i< nums.size(); i++){
            auto j = map.find(target - nums[i]); //查找是否已经存在符合目标值的数
            if(j != map.end()){
                return {j->second, i}; //作为索引key的j实际上是数组值,而j对应的value是数组下标
            }else{
                map.insert(pair<int, int>(nums[i],i)); //数组值做索引key,下标做值value
            }
        }
        return {};
    }
};

以前写python的时候用字典干过类似思路的事情。就是把一个按照值的某种规则重新排列,大概思想很类似,也是建立一个字典用值做索引,下标做值来进行查找操作。算是比较好理解这道题的思路,顺便看了一下卡哥给出的python解法,确实和我想的差不多

标签:map,set,return,Day9,int,sum,LeetCode,刷题
From: https://www.cnblogs.com/tianmaster/p/16871759.html

相关文章

  • 【HDLBits刷题笔记】17 Verification: Wrting Testbenches&CS450
    Tb/clock这题要求给dut模块一个时钟。moduletop_module();regclk;always#5clk=~clk;initialbeginclk=0;enddutu0(clk);......
  • leetcode-520-easy
    DetectCapitalWedefinetheusageofcapitalsinawordtoberightwhenoneofthefollowingcasesholds:Alllettersinthiswordarecapitals,like"USA".......
  • leetcode-1784-easy
    CheckifBinaryStringHasatMostOneSegmentofOnesGivenabinarystringswithoutleadingzeros,returntrueifscontainsatmostonecontiguoussegment......
  • leetcode-819-easy
    MostCommonWordGivenastringparagraphandastringarrayofthebannedwordsbanned,returnthemostfrequentwordthatisnotbanned.Itisguaranteedthe......
  • LeetCode 435. Non-overlapping Intervals
    贪心按照有边界排序,只有先选择了右边边界小的,才可以放下更多的区间classSolution{public:interaseOverlapIntervals(vector<vector<int>>&intervals){......
  • leetcode-1260-easy
    Shift2DGridGivena2Dgridofsizemxnandanintegerk.Youneedtoshiftthegridktimes.Inoneshiftoperation:Elementatgrid[i][j]movestogrid......
  • leetcode-561-easy
    ArrayPartitionGivenanintegerarraynumsof2nintegers,grouptheseintegersintonpairs(a1,b1),(a2,b2),...,(an,bn)suchthatthesumofmin(ai,bi......
  • leetcode-1854-easy
    MaximumPopulationYearYouaregivena2Dintegerarraylogswhereeachlogs[i]=[birthi,deathi]indicatesthebirthanddeathyearsoftheithperson.The......
  • leetcode-1684-easy
    CounttheNumberofConsistentStringsYouaregivenastringallowedconsistingofdistinctcharactersandanarrayofstringswords.Astringisconsistenti......
  • LeetCode 135. Candy
    贪心算法贪心策略:在每次遍历中,仅考虑并更新相邻一侧的大小关系classSolution{public:intcandy(vector<int>&ratings){intsize=ratings.size();......