Jan 12 - Power of Two; Integer; Bit Manipulation;

Two‘s complement of integer:

https://zh.wikipedia.org/wiki/%E4%BA%8C%E8%A3%9C%E6%95%B8

Bit Manipulation:

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html

代码:

public class Solution {
    public boolean isPowerOfTwo(int n) {
        int count = 0;
        if((n & Integer.MIN_VALUE) != 0){
            //n = ~n + 1;
            return false;
        }
        for(int i = 32; i > 0; i--){
            count += n & 1;
            n = n>>1;
        }
        return count == 1;
    }
}

  

时间: 2024-10-08 20:41:48

Jan 12 - Power of Two; Integer; Bit Manipulation;的相关文章

Jan 14 - Power of three; Math; Integer; Digit;

No loop; No recursion; 我没找到规律 但是从讨论区看到别人的思路: If N is a power of 3: It follows that 3^X == N It follows that log (3^X) == log N It follows that X log 3 == log N It follows that X == (log N) / (log 3) For the basis to hold, X must be an integer. 代码: pu

LeetCode之Easy篇 ——(12)Roman to Integer

Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 罗马数字: 基本字符 I V X L C D M 相应的阿拉伯数字表示为 1 5 10 50 100 500 1000 1.相同的数字连写.所表示的数等于这些数字相加得到的数.如:Ⅲ=3: 2.小的数字在大的数字的右边.所表示的数等于这些数字相加得到的数. 如:Ⅷ=8.Ⅻ=12: 3

Jan 12 - Implement Queue using Stacks; Stack; Queue Implementation;

代码: class MyQueue { // Push element x to the back of queue. Stack<Integer> stack = new Stack<>(); Stack<Integer> aux = new Stack<>(); public void push(int x) { while(!stack.isEmpty()){ aux.push(stack.pop()); } stack.push(x); while(

Jan 08 - Reverse Bits; Binary; Integer; Bits; 复习位运算和二进制表达

复习 二进制位操作(bit operation),32bit integer 取值范围:-2^31 -- 2^31-1 0 1 1 1 1 1 1 1 = 127 0 0 0 0 0 0 1 0 = 2 0 0 0 0 0 0 0 1 = 1 0 0 0 0 0 0 0 0 = 0 1 1 1 1 1 1 1 1 = −1 1 1 1 1 1 1 1 0 = −2 1 0 0 0 0 0 0 1 = −127 1 0 0 0 0 0 0 0 = −128 (2‘ compliment)二补数系统

Jan 12 - Delete Node in a Linked List; Data Structure; Linked List; Pointer;

代码: /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution { public void deleteNode(ListNode node) { if(node == null) return; while(node.next != null)

Jan 12 - Lowest Common Ancestor of a Binary Search Tree; Tree; BST; Recursion;

/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q)

Jan 12 - Summary Ranges;Array;String

Functions of StringBuilder: 代码: public class Solution { public List<String> summaryRanges(int[] nums) { List<String> list = new ArrayList<>(); if(nums.length == 0) return list; int head = 0; int k = -1; StringBuilder sb = new StringBuild

Jan 12 - Valid Anagram; Hash Table; Sort; Alphabets; String;

创建两个数组存储字母的个数,再对比两个数组是否相同 代码: public class Solution { public boolean isAnagram(String s, String t) { int[] alphabets1 = new int[26]; int[] alphabets2 = new int[26]; if(s.length() != t.length()) return false; for(int i = 0; i < s.length(); i++){ alpha

Jan 12 - Binary Tree Paths; Tree; Recursion; DFS;

代码: /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public List<String> binaryTreePaths(TreeNode root) { List<String>