
Bit Operation
文章平均质量分 71
flyatcmu
这个作者很懒,什么都没留下…
展开
-
Convert a Number to Hexadecimal
Given an integer, write an algorithm to convert it to hexadecimal. For negative integer,two’s complementmethod is used.Note:All letters in hexadecimal (a-f) must be in lowercase. The hexadecimal string must not contain extra leading0s. If the numbe...原创 2020-06-28 14:01:57 · 162 阅读 · 0 评论 -
Sum of Two Integers
Calculate the sum of two integersaandb, but you arenot allowedto use the operator+and-.思路:用XOR来进行加计算,类似: 1+2 = 0001 + 0010 = 0011 = 3,进位,用&然后,tricky的地方在于 while循环是用b来判断是否为0,而且carry用原创 2016-07-02 11:45:36 · 338 阅读 · 0 评论 -
Counting Bits
Given a non negative integer numbernum. For every numbersiin the range0 ≤ i ≤ numcalculate the number of 1's in their binary representation and return them as an array.Example 1:Input: 2Output: [0,1,1]Example 2:Input: 5Output: [0,1,1,2,1,2...原创 2020-05-25 05:11:03 · 174 阅读 · 0 评论 -
Find the Difference
Given two stringssandtwhich consist of only lowercase letters.Stringtis generated by random shuffling stringsand then add one more letter at a random position.Find the letter that was原创 2016-08-29 12:24:14 · 476 阅读 · 0 评论 -
Single Number II
Given an array of integers, every element appears three times except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without using原创 2014-02-09 09:56:30 · 453 阅读 · 0 评论 -
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 ...原创 2014-01-02 07:27:54 · 462 阅读 · 0 评论 -
Number of 1 Bits
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as theHamming weight).For example, the 32-bit integer ’11' has binary representation000000原创 2016-07-02 11:32:32 · 273 阅读 · 0 评论 -
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...原创 2016-10-02 06:19:10 · 221 阅读 · 0 评论 -
Integer Replacement
Given a positive integer n and you can do operations as follow:If n is even, replace n with n/2.If n is odd, you can replace n with either n + 1 or n - 1.What is the minimum number o原创 2016-09-13 03:08:07 · 856 阅读 · 0 评论 -
Binary Watch
A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom represent the minutes (0-59).Each LED represents a zero or one, with the least significant bit o原创 2016-09-19 06:51:03 · 1985 阅读 · 0 评论 -
Divide Two Integers
Divide two integerswithout using multiplication, division and mod operator.If it will overflow(exceeding 32-bit signed integer representation range), return2147483647The integer division should ...原创 2014-12-16 08:05:33 · 599 阅读 · 0 评论 -
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 =原创 2020-05-09 04:49:21 · 276 阅读 · 0 评论 -
Maximum Product of Word Lengths
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lower case lett原创 2016-08-21 01:32:36 · 292 阅读 · 0 评论 -
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 as0011100101原创 2015-03-09 15:18:26 · 674 阅读 · 0 评论