
位运算
JackZhangNJU
未来的路还很长
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
leetcode 136. Single Number 位操作
Given an array of integers, every element appears twice except for one. Find that single one.Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra me原创 2017-09-15 11:01:17 · 392 阅读 · 0 评论 -
leetcode 187. Repeated DNA Sequences 编码计数统计重复字符串 + 移动窗口
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: “ACGAATTCCG”. When studying DNA, it is sometimes useful to identify repeated sequences within the DNA.Write原创 2017-09-20 09:44:54 · 684 阅读 · 0 评论 -
leetcode 191. Number of 1 Bits 数字中1的数量 + 位运算
Write a function that takes an unsigned integer and returns the number of ’1’ bits it has (also known as the Hamming weight).For example, the 32-bit integer ’11’ has binary representation 0000000000原创 2017-09-20 10:19:44 · 315 阅读 · 0 评论 -
leetcode 201. Bitwise AND of Numbers Range 最长公共前缀问题 + 位操作
Given a range [m, n] where 0 For example, given the range [5, 7], you should return 4.这道题最自觉的做法就是循环做位and操作,但是我在网上看到了一个做法, 想法很棒,这道题可以直接转化为另一个问题:再一串连续的数组中,寻找他们的公共前缀。直接暴力去计算可能要超时代码如下:/* * 这道题原创 2017-09-21 09:06:37 · 241 阅读 · 0 评论 -
leetcode 403. Frog Jump 青蛙跳问题+典型深度优先遍历问题
A frog is crossing a river. The river is divided into x units and at each unit there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.Given a list of s...原创 2018-01-29 19:10:58 · 1191 阅读 · 0 评论 -
leetcode 421. Maximum XOR of Two Numbers in an Array 最大的异或运算值 + 位运算
Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231.Find the maximum result of ai XOR aj, where 0 ≤ i, j < n.Could you do this in O(n) runtime?Example:Input: [3,...原创 2017-12-11 14:53:06 · 522 阅读 · 0 评论 -
leetcode 409. Longest Palindrome 可以构造的最长的回文字符串
Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.This is case sensitive, for example “Aa” is not consi...原创 2017-12-07 20:06:35 · 338 阅读 · 0 评论 -
leetcode 693. Binary Number with Alternating Bits 二进制的数据是否交替出现+暴力判断
Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values.Example 1: Input: 5 Output: True Explanation: The binary represent...原创 2017-12-23 17:19:14 · 369 阅读 · 0 评论 -
leetcode 461. Hamming Distance 汉明距离 + 位运算
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.Given two integers x and y, calculate the Hamming distance.Note: 0 ≤ x, y < 2...原创 2018-02-08 09:28:01 · 362 阅读 · 0 评论 -
leetcode 477. Total Hamming Distance 任意两数字汉明距离和+32Bit遍历求解
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.Now your job is to find the total Hamming distance between all pairs of the given n...原创 2017-12-13 09:41:10 · 415 阅读 · 0 评论 -
leetcode 393. UTF-8 Validation UTF-8编码判断 + 位运算
A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules:For 1-byte character, the first bit is a 0, followed by its unicode code. For n-bytes character, the first n-bits原创 2017-12-05 21:49:06 · 760 阅读 · 0 评论 -
leetcode 338. Counting Bits 位计算 + 统计二进制1的数量
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1’s in their binary representation and return them as an array.Example: For num = 5 you原创 2017-10-02 17:11:19 · 322 阅读 · 0 评论 -
leetcode 137. Single Number II
Given an array of integers, every element appears three times except for one, which appears exactly once. Find that single one.Note: Your algorithm should have a linear runtime complexity. Could you i原创 2017-09-17 10:42:55 · 548 阅读 · 0 评论 -
leetcode 190. Reverse Bits
Reverse bits of a given 32 bits unsigned integer.For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110原创 2017-09-20 10:15:34 · 538 阅读 · 0 评论 -
leetcode 222. Count Complete Tree Nodes 计算满二叉树的节点数量 + DFS深度优先遍历 + 公式计算
Given a complete binary tree, count the number of nodes.Definition of a complete binary tree from Wikipedia: In a complete binary tree every level, except possibly the last, is completely filled, a原创 2017-09-25 10:14:45 · 392 阅读 · 0 评论 -
leetcode 231. Power of Two 2的幂次方数的判断 + 统计二进制数据1的数量
Given an integer, write a function to determine if it is a power of two.就是判断一个数是不是2的次方数。代码如下:public class Solution{ public boolean isPowerOfTwo(int n) { if(n0) re原创 2017-09-25 11:29:14 · 325 阅读 · 0 评论 -
leetcode 260. Single Number III 神奇的位操作 + 找到两个只出现一次的数
Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.For example:Given nums =原创 2017-09-27 09:24:31 · 949 阅读 · 0 评论 -
leetcode 268. Missing Number 等差数列求和 + 位操作
Given an array containing n distinct numbers taken from 0, 1, 2, …, n, find the one that is missing from the array.For example, Given nums = [0, 1, 3] return 2.Note: Your algorithm should run in原创 2017-09-27 09:48:08 · 419 阅读 · 0 评论 -
leetcode 326. Power of Three 3的幂指数 + 对数函数实现
Given an integer, write a function to determine if it is a power of three.Follow up: Could you do it without using any loop / recursion?最直接的方法是使用log函数,还有一个从网上看到的别的方法,也可以,不过很难想。建议和这一道题leetcode 3原创 2017-10-02 12:28:13 · 1221 阅读 · 0 评论 -
leetcode 89. Gray Code 按照index递归DFS解决 + Grey码生成公式 + 位运算直接计算
The gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integer n representing the total number of bits in the code, print the sequence of gr原创 2017-09-10 22:12:47 · 534 阅读 · 0 评论 -
leetcode 540. Single Element in a Sorted Array 异或位操作
Given a sorted array consisting of only integers where every element appears twice except for one element which appears once. Find this single element that appears only once.Example 1: Input: [1,1,...原创 2017-12-18 09:28:04 · 263 阅读 · 0 评论