zoukankan      html  css  js  c++  java
  • leetcode Ch8-Others

    1. Rotate Image 旋转图像

    顺时针旋转90度:先沿水平线翻转,再沿主对角线翻转。

    逆时针旋转90度:先沿竖直线翻转,再沿主对角线翻转。

    顺时针旋转180度:水平翻转和竖直翻转各一次。 逆时针旋转180度效果同顺时针180度。

    2.Set Matrix Zeroes 

    空间O(1)方法:利用第一行和第一列。

    1.先确定第一行和第一列是否需要清零
    2.扫描剩下的矩阵元素,如果遇到了0,就将对应的第一行和第一列上的元素赋值为0 (反正早晚都要对它赋0,现在赋0能起到标记作用)
    3.根据第一行和第一列的信息,已经可以讲剩下的矩阵元素赋值为结果所需的值了
    4.根据1中确定的状态,处理第一行和第一列。

    ref:  http://fisherlei.blogspot.com/2013/01/leetcode-set-matrix-zeroes.html

    3. gas station

    consider the case that, if started at station i, and when goes to the station j, there is not enough gas to go the j+1 station. What happened now? For the brutal force method, we go back to the station i+1 and do the same thing. But, actually, if the accumutive gas cannot make it from j to j+1, then the stations from i to j are all not the start station.

    That is because, (1)the tank is unlimited, every time arrive to the station, the tank will fuel the max gas here, and comsume the cost to go to the next. (2)There can not be negative tank when arriving a station, at least the tank is empty. So, if i to j cannot go to j+1, then i+1 to j still cannot go to j+1... In this way, the next starting station we will try is not i+1, but the j+1. And after a single loop from i to j, we can find the result!

     原因很简单:tank里的油量是不可能是负数的。如果你从起点i处到j都到不了,那从i+1就更不可能到j了,因为从i到i+1后tank里可能会有些剩余的油,最差情况就是从i 到i+1后tank刚好空。如果带着剩余的油从i+1都到不了j,那以白手起家的状态从i+1出发更不可能到j了。

    所以,如果从i到j的累积和为负,那i不用从i+1开始继续循环,直接跳到 j 处开始循环就行了。

    explanation ref: http://yucoding.blogspot.com/2013/12/leetcode-question-gas-station.html

    code refer to soulmachine.

    4. candy

    为什么从左向右扫一次,又从右向左扫一次?

    举个最简单的例子,如果ratings是5,4,3,2,1, 从左向右扫能到的candy数1,1,1,1,1,但从右向左扫能到的candy数是1,2,3,4,5。

    对每个位置都取一个max。

    ref: http://yucoding.blogspot.com/2014/02/leetcode-question-candy.html

    5. single number II

    还是位操作。设一个32位的count数组,用来统计每一位出现的次数。如果哪一位出现的次数不是3的倍数,那就在最后算result的时候按照其权重加进去。

     1 class Solution {
     2 public:
     3     int singleNumber(vector<int>& nums) {
     4         int n = nums.size();
     5         vector<int> count(sizeof(int) * 8, 0);
     6         for (int i = 0; i < sizeof(int) * 8; i++) {
     7             for (int j = 0; j < n; j++) {
     8                 count[i] += (nums[j] >> i) & 1;
     9             }
    10         }
    11         int result = 0;
    12         for (int i = 0; i < count.size(); i++) {
    13             result += (count[i] % 3) << i;
    14         }
    15         return result;
    16     }
    17 };
    View Code
  • 相关阅读:
    js获取客户端time,cookie,url,ip,refer,user_agent信息:
    生成springboot docker镜像 并上传到阿里云镜像厂库
    install pymongo,mysql
    No module named MYSQLdb 问题解决
    CentOS7下安装python-pip
    task_payment_byonlinedown
    SPLIT_STR
    通过 Composer 安装 Laravel 安装器
    laravel Faker-1.faker假数据
    laravel-admin安装时执行php arisan admin:install 命令时报SQLSTATE[42000]: Syntax error or acce ss violation: 1071 Specified key was too long; max key length is 1000 bytes
  • 原文地址:https://www.cnblogs.com/forcheryl/p/4713100.html
Copyright © 2011-2022 走看看