zoukankan      html  css  js  c++  java
  • leetcode-每日打卡-day 5

    leetcode 每日打卡

    those times when you get up early and you work hard; those times when you stay up late and you work hard; those times when don’t feel like working — you’re too tired, you don’t want to push yourself — but you do it anyway. That is actually the dream. That’s the dream. It’s not the destination, it’s the journey. And if you guys can understand that, what you’ll see happen is that you won’t accomplish your dreams, your dreams won’t come true, something greater will. mamba out


    那些你早出晚归付出的刻苦努力,你不想训练,当你觉的太累了但还是要咬牙坚持的时候,那就是在追逐梦想,不要在意终点有什么,要享受路途的过程,或许你不能成就梦想,但一定会有更伟大的事情随之而来。 mamba out~

    2020.2.12


    记录下来自己做题时得思路,并不一定是最优解

    461. 汉明距离

    解法一:
    class Solution {
    public:
        int hammingDistance(int x, int y) {
            int ans = 0;
            while(x > 0 || y > 0)
            {
                if((x & 1) ^ (y & 1))ans++;
                x = x >> 1;y =  y >> 1;
            }
            return ans;
        }
    };
    
    解法二
    class Solution {
    public:
        int hammingDistance(int x, int y) {
            int a = x ^ y , sum = 0;
            while(a)
            {
                sum += a & 1;
                a = a >> 1;
            }
            return sum;
        }
    };
    

    1342. 将数字变成 0 的操作次数

    class Solution {
    public:
        int numberOfSteps (int num) {
            int ans = 0;
            while(num)
            {
                ans++;
                if(num % 2 == 0)num = num >> 1;
                else num -= 1;
            }
            return ans;
        }
    };
    

    1290. 二进制链表转整数

    class Solution {
    public:
        int getDecimalValue(ListNode* head) {
            string s;
            while(head)
            {
                if(head->val == 0)s += "0";
                else s+="1";
                head = head->next;
            }
            int ans = 0 ,k = 1;
            for(int i = s.size() - 1;i >= 0; i--)
            {
                if(s[i] == '1')ans += k;
                k *= 2;
            }
            return ans;
        }
    };
    
  • 相关阅读:
    URL中#号(井号)的作用
    Sublime Text2 快捷键汇总
    Sublime Text2 使用及插件配置
    Sumblime Text 2 常用插件以及安装方法
    CSS禁止选择文本功能(兼容IE,火狐等浏览器)
    JavaScript 判断 URL
    纯真IP数据库格式读取方法(JAVA/PHP/Python)
    Sublime Text 2 性感无比的代码编辑器!程序员必备神器!跨平台支持Win/Mac/Linux
    base64:URL背景图片与web页面性能优化
    数独DFS实现
  • 原文地址:https://www.cnblogs.com/wlw-x/p/12301093.html
Copyright © 2011-2022 走看看