原创 【Lintcode】664. Counting Bits

題目地址: https://www.lintcode.com/problem/counting-bits/description 給定nnn,求0∼n0\sim n0∼n的每個數的二進制表示中111的個數。 思路是動態規劃。令f[

原创 【Leetcode】461. Hamming Distance

題目地址: https://leetcode.com/problems/hamming-distance/ 給定兩個數,判斷其二進制表示對應位有多少位不同的。直接求異或然後算一下有多少個111即可。可以用lowbit來做。代碼如下

原创 【Leetcode】788. Rotated Digits

題目地址: https://leetcode.com/problems/rotated-digits/ 給定一個正整數,如果其每一位旋轉180°180\degree180°後可以得到一個新的合法數字,並且和自己不一樣,稱其爲”好數

原创 【Leetcode】874. Walking Robot Simulation

題目地址: https://leetcode.com/problems/walking-robot-simulation/ 給定一個二維網格,有個機器人從(0,0)(0,0)(0,0)出發,再給定一系列命令,以整數表示−1-1−1

原创 【Leetcode】1441. Build an Array With Stack Operations

題目地址: https://leetcode.com/problems/build-an-array-with-stack-operations/ 給定一個數組target,再給定一個數字nnn,另外給一個棧,從[1,2,...,

原创 【Leetcode】728. Self Dividing Numbers

題目地址: https://leetcode.com/problems/self-dividing-numbers/ 給定一個正整數區間範圍[l,r][l, r][l,r],挑出所有能被其各個位上的數整除的數字(不允許其中某一位爲

原创 【Leetcode】641. Design Circular Deque

題目地址: https://leetcode.com/problems/design-circular-deque/ 設計循環雙端隊列。初始化時會給定一個capacity,不需要擴容。當添加元素的時候,如果隊列滿,則返回false

原创 【Leetcode】507. Perfect Number

題目地址: https://leetcode.com/problems/perfect-number/ 給定一個正整數,判斷其是否是完全數(即perfect number,指的是其除了自己以外的正因子之和等於自己)。 遍歷其因子即

原创 【Leetcode】1222. Queens That Can Attack the King

題目地址: https://leetcode.com/problems/queens-that-can-attack-the-king/ 給定一個8×88\times88×8棋盤的若干座標,橫縱座標範圍爲0∼70\sim 70∼7

原创 【Leetcode】1086. High Five

題目地址: https://leetcode.com/problems/high-five/ 給定一個二維數組,每一行代表某個學生的某一科目分數,第一個數字是學生id,第二個數字是分數。要求返回一個數組,存儲每個學生最高的555門

原创 【Leetcode】498. Diagonal Traverse

題目地址: https://leetcode.com/problems/diagonal-traverse/ 給定一個二維矩陣nnn行mmm列,顯然形如從左下到右上的對角線,一共有n+m−1n + m - 1n+m−1條。現在從右

原创 【Leetcode】670. Maximum Swap

題目地址: https://leetcode.com/problems/maximum-swap/ 給定一個十進制正整數nnn,允許調換其中兩個位置的值(也可以選擇不調換),問能得到的最大整數是多少。 先將nnn的各個位上的數字取

原创 【Leetcode】504. Base 7

題目地址: https://leetcode.com/problems/base-7/ 將一個整數轉爲777進制。只需注意一下其是否爲000,是否爲負數即可。其餘步驟就按照不停取餘即可。代碼如下: public class Sol

原创 【Leetcode】1351. Count Negative Numbers in a Sorted Matrix

題目地址: https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/ 給定一個二維矩陣,每一行從左到右(非嚴格)單調下降,每一列從上到下(非嚴

原创 【Lintcode】595. Binary Tree Longest Consecutive Sequence

題目地址: https://www.lintcode.com/problem/binary-tree-longest-consecutive-sequence/description 給定一棵二叉樹,求所有從上向下的路徑中,數值連