leetcode刷题笔记342 4的幂】的更多相关文章

题目描述: 给定一个整数 (32位有符整数型),请写出一个函数来检验它是否是4的幂. 示例:当 num = 16 时 ,返回 true . 当 num = 5时,返回 false. 问题进阶:你能不使用循环/递归来解决这个问题吗? 题目分析: 如231题同样思路,还是通过位操作来解决这道 首先判断下输入为0和负数的情况 然后分析4的幂的特点0,4,16 化为二进制0,0100,00010000 跟求2的幂不同的是此处少了2,8 化为2进制   ,0010,00001000 0 0 0 0 0 1…
题目描述: 给定一个整数,写一个函数来判断它是否是2的幂. 题目分析: 判断一个整数是不是2的幂,可根据二进制来分析.2的幂如2,4,8,等有一个特点: 二进制数首位为1,其他位为0,如2为10,4为100 2&(2-1)=0   4&(4-1)=0 即得出结论如果一个数n为2的幂,则n(n-1)=0 此题通过率37.6%,挺高的 解答代码: C++版: class Solution { public: bool isPowerOfTwo(int n) { ) return false;…
题目描述: 给出一个整数,写一个函数来确定这个数是不是3的一个幂. 后续挑战:你能不使用循环或者递归完成本题吗? 题目分析: 既然不使用循环或者递归,那我可要抖机灵了 如果某个数n为3的幂 ,则k=log3N 代码思路: 首先求出int范围最大的3的幂   Max3 如果n为3的幂,则Max3必定能整除n so,直接上代码 解答代码: C++版: class Solution { public: bool isPowerOfThree(int n) { )return false; const…
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己的切身经历更新): 算法不是纯粹拼智商的,智商高,就一定很厉害,不够聪明,就一定不行.算法是一种技能,是可以通过科学合理的方式训练出来的能力.目前国内大厂的算法考察,基本不会超过leetcode 中等难度,上限难度基本都是leetcode 中等题里面的中等难度 基本的算法数据结构是有限的.比如说链表…
本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你可以尽可能地完成更多的交易(多次买卖一支股票). 注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票). 思路:买高不买低,向上即可 class Solution(object): def maxProfit(self, prices): "…
学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链接:https://leetcode-cn.com/problemset/all/ 一.题意 难度:中等 https://leetcode-cn.com/problems/integer-to-roman/ 罗马数字包含以下七种字符:I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5…
1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口(两个指针包围的区域为当前的窗口),经常用于区间搜索. 若两个指针指向同一数组,但是遍历方向相反,那我们可以用来进行搜索,这时我们要搜索的数组往往需要排序好. 2.双指针类型 在目前的刷到过的题目种,遇到了两类双指针问题 第一类是快慢指针,顾名思义,就是一个fast指针,一个slow指针,一前一后,…
LeetCode1-9 本文更多是作为一个习题笔记,没有太多讲解 1.两数之和 题目请点击链接 ↑ 最先想到暴力解法,直接双循环,但是这样复杂度为n平方 public int[] twoSum(int[] nums, int target) { for (int i = nums.length - 1; i >= 0; i--) { for (int j = 0; j < i; j++) { if (nums[i] + nums[j] == target) { int[] twoSum = n…
(1)Best Time to Buy and Sell Stock Total Accepted: 10430 Total Submissions: 33800My Submissions Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie…
题目描述 实现 atoi,将字符串转为整数. 在找到第一个非空字符之前,需要移除掉字符串中的空格字符.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字符即为整数的值.如果第一个非空字符是数字,则直接将其与之后连续的数字字符组合起来,形成整数. 字符串可以在形成整数的字符后面包括多余的字符,这些字符可以被忽略,它们对于函数没有影响. 当字符串中的第一个非空字符序列不是个有效的整数:或字符串为空:或字符串仅包含空白字符时,则不进行转换. 若函数不能执行有…
题目描述: 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab"输出:[ ["aa","b"], ["a","a","b"]] 来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/palindrome-partitioning 解题思路: 1.显然回溯法:求所有可能…
题目描述: 格雷编码是一个二进制数字系统,在该系统中,两个连续的数值仅有一个位数的差异. 给定一个代表编码总位数的非负整数 n,打印其格雷编码序列.格雷编码序列必须以 0 开头. 来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/gray-code 解题思路: 当n=1时:0,1 当n=2时,仅需对n=1的所有结果最高位添加1&&&并且逆序添加 0,1 10,11->逆序  11,10 最终结果0,1,11,10: publ…
题目描述: 编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 ‘1’ 的个数(也被称为汉明重量). 示例: 输入: 输出: 解释: 32位整数 的二进制表示为 . 题目分析: 判断32位二进制中1的个数,此题利用n&(n-1)性质可解 解答代码: C++版: class Solution { public: int hammingWeight(uint32_t n) { ; ){ n=n&(n-); ans++; } return ans; } }; Code Pytho…
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that: Only one letter can be changed at a time Each intermediate word must exist in the dictionary For example, Given:start = "hit…
Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab",Return 1 since the palindrome partitioning ["aa",…
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n). For example,S = "ADOBECODEBANC"T = "ABC" Minimum window is "BANC". Note:If there is no such window i…
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ... 1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off as "one 2, then one 1" or 1211. Given an…
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 题解:开始想到的方法比较偷懒,直接遍历一遍数组,把每个ListNode对应的值放到数组里面,然后把数组转换成BST,想来这个题的本意肯定不是这样. 自己也想到了bottom-up的方法,但是没想明白怎么个bottom-up法,真的是昨天做梦都在想=.= 今天早上终于弄懂了,用递归…
Sort a linked list in O(n log n) time using constant space complexity. 题解:实现一个链表的归并排序即可.主要分为三部分: 1.找到中点并返回的函数findMiddle; 2.归并函数merge; 3.排序函数sortList. 数组的findMiddle函数非常容易实现,链表就有一点tricky了.首先设置两个指针,一个slow初始化为head,一个fast初始化为head.next,然后slow一次走一步,fast一次走两…
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-space cha…
Given a sorted linked list, delete all duplicates such that each element appear only once. For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3. 题解:水题不解释 /** * Definition for singly-linked list. * struct Li…
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example,Given the following matrix: [ [ , , ], [ , , ], [ , , ] ] You should return [1,2,3,6,9,8,7,4,5]. 题解:还是找规律的题:设有四个变量Xs,Xe,Ys,Ye,如下图所示,它…
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order,We get the following sequence (ie, for n = 3): Given n and k, return the kth permutation sequence. Note: Given n will be between…
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? 解题:果然不能晚上做题,效率好低.看了讨论才学会的解法.设置一个指针next指向当前访问的…
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 解题:应该是很简单的一道题,纠结了好久T_T 基本思路很简单,用栈模拟就可以了.首先根节…
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example,Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 1 2 3解题: 有一个很简单的结论,就是节点数为n的二叉树共有h(n)种形态,其中h(n)是卡特兰数,h…
Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). Ho…
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 解题:设定一个变量sum存放反转后的答案,每次取输入x的最后一位n,并用sum = sum*10+n更新sum. 例如题设的数字123,初始sum为0,过程如下: 取末尾的3,sum=3,x=12 取末尾的2,sum=32,x=1 取末尾的1,sum=321,x=0 特别注意x一开始就是0的处理,直接返回0就可以了.…
What if the given tree could be any binary tree? Would your previous solution still work? Note: You may only use constant extra space. For example,Given the following binary tree, 1 / \ 2 3 / \ \ 4 5 7 After calling your function, the tree should loo…
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? 题解:简单的模拟题,每次用answer列表存放上一层的值,用temp列表存放当前层的值,只要计算好temp中需要重新计算的元素的索引范围[1,i-1]…