题目来源:LeetCode: 231. Power of Two
Given an integer, write a function to determine if it is a power of two.
思路
- 按位操作为出发点
- 所有2的幂数共性为:二进制中某一位为1,其他位均为0
- 逐位右移,当遇到首个非0的位时,判断是否仅存在唯一的1
时间复杂度:O(logn), 空间复杂度O(1)
记录、分享、成长
题目来源:LeetCode: 231. Power of Two
Given an integer, write a function to determine if it is a power of two.
时间复杂度:O(logn), 空间复杂度O(1)
问题描述:将输入的32位无符号整数对应的二进制数,按位反转后返回对应的整数。
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 00111001011110000010100101000000).
Follow up:
If this function is called many times, how would you optimize it?
Related problem: Reverse Integer
Note:按位操作,对输入从后向前逐一处理。
题目来源:LeetCode: 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 00000000000000000000000000001011, so the function should return 3.
Note:从末尾开始,逐位统计1的个数;末位为1时为奇数,除2操作即为右移一位。
题目来源:LeetCode: 217. Contains Duplicate
Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.
White space characters include carriage returns, line feeds, spaces, and tabs.
CR: Carriage Returns, 回车,常常标记为\r
LF: Line Feeds,换行,常常标记为\n
Spaces,空格
Tabs,制表符
DOS及Windows下:CR LF(\r\n)回车换行表示切换至新的一行
Linux及Unix下:LF(\n)换行表示切换至新的一行
Mac OS下:CR(\r)回车符表示切换至下一行
跨系统时由于系统间差异导致显示问题:Windows打开Linux下的文件时,所有行合并为一行;Linux打开Windows下的文件后,出现很多^M符号。
Linux下删除^M
的方法:cat test.txt | tr -d "\r" > newTest.txt
Linux也有实现与Windows系统文件格式互换的命令:unix2dos, dos2unix
unix2dos 是把linux文件格式转换成windows文件格式
dos2unix 是把windows格式转换成linux文件格式
本文结合实例分析了Java中equals和==操作符的异同,参考了诸多文献,并给出了相应的实例程序。
In Java, all variables are either primitive types or references.
Java中所有变量均为直接数据类型或引用
==: 比较引用是否指向同一个对象。
.equals(): 比较引用所指向对象的内容(值)是否相同(不同类可自定义重写该方法)。
== operator checks if the references of both the objects are the same.
equals method compares values for equality.
Java中equals和==的区别 值类型是存储在内存中的堆栈(简称栈),而引用类型的变量在栈中仅仅是存储引用类型变量的地址,而其本身则存储在堆中。
==操作比较的是两个变量的值是否相等,对于引用型变量表示的是两个变量在堆中存储的地址是否相同,即栈中的内容是否相同。
equals操作表示的两个变量是否是对同一个对象的引用,即堆中的内容是否相同。