【LeetCode】50. Pow(x, n) (3 solutions)】的更多相关文章

Pow(x, n) Implement pow(x, n). 按照定义做的O(n)肯定是TLE的. 利用这个信息:x2n = (xn)2 有个注意点,当n为负是,直接取反是不可行的. 由于int的表示范围是[2-31, 231-1],当n为INT_MIN时,取反会溢出. 因此需要对n==INT_MIN单独考虑. 另外,除以2可以用右移1位来实现. 解法一:递归 class Solution { public: double pow(double x, int n) { ) { if(n == I…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 递归 迭代 日期 题目地址: https://leetcode.com/problems/powx-n/description/ 题目描述: Implement pow(x, n), which calculates x raised to the power n (x^n). Example 1: Input: 2.00000, 10 Outpu…
Implement pow(x, n). Example 1: Input: 2.00000, 10 Output: 1024.00000 Example 2: Input: 2.10000, 3 Output: 9.26100 package medium; public class L50MyPow { // 调用Math.pow() 函数 public double mypow(double x, int n) { double nn = n; return Math.pow(x, nn)…
一天一道LeetCode系列 (一)题目 Implement pow(x, n). (二)解题 题目很简单,实现x的n次方. /* 需要注意一下几点: 1.n==0时,返回值为1 2.x==1时,返回值为1:x==-1时,根据n的奇偶来判断 3.n==-2147483648,特殊情况,int的范围时-2147483648-2147483647, */ class Solution { public: double myPow(double x, int n) { if(n==0||x==1) r…
package y2019.Algorithm.array; import java.util.HashSet; import java.util.Set; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * @ClassName: FairCandySwap * @Author: xiaof * @Description: TODO 888. Fair Candy Swap * * Alice and Bob…
Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 逐区间做处理. 解法一:非递归 class Solution { public: string intToRoman(int num) { string ret; //M<-->1000 ) { ret += 'M'; num -= ; } //t…
Palindrome Linked List Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time and O(1) space? 解法一: 一次遍历,装入vector,然后再一次遍历判断回文. 时间复杂度O(n),空间复杂度O(n) /** * Definition for singly-linked list. * struct ListNode…
Reverse Linked List Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either iteratively or recursively. Could you implement both? 解法一:非递归 /** * Definition for singly-linked list. * struct ListNode { * int va…
Number of Islands Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are…
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including t…
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 解法一:非递归 从左到右遍历每个字符,并记录上个字符来处理双字符情况即可. class Solution { public: int romanToInt(string s) { ; ; ; i < s.size(); i ++) { switch(…
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length…
First Missing Positive Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2. Your algorithm should run in O(n) time and uses constant space. 解法一:O(nlogn) time and O(1) sp…
Single Number II Given an array of integers, every element appears threetimes except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 解法一:开辟map记录次数 class So…
Longest Common Prefix Write a function to find the longest common prefix string amongst an array of strings. 解法一: 思路:设置一个位数记录器num,遍历所有字符串的第num位.如果都相同,则num++. 直到某字符串结束或者所有字符串的第num位不都相同,则返回[0~num-1]位,即最长公共前缀. class Solution { public: string longestComm…
Search Insert Position Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples.[1,3,5,…
Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initializ…
Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space? 解法一: 使用unordered_map记录当前节点是否被访问过,如访问过说明有环,如到达尾部说明无环. /** * Definition for singly-linked list. * struct ListNode { * int va…
Set Matrix Zeroes Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click to show follow up. Follow up: Did you use extra space?A straight forward solution using O(mn) space is probably a bad idea.A simple…
Find Peak Element A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[i+1], find a peak element and return its index. You may imagine that num[-1] = num[n] = -∞. For example, in array [1, 2, 3, 1],…
[2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/9797184.html [7]Reverse Integer (2018年12月23日, review) 给了一个32位的整数,返回它的reverse后的整数.如果reverse后的数超过了整数的范围,就返回 0. Example 1: Input: 123 Output: 321 Example 2:…
题目如下: 解题思路:我的想法对于数组中任意一个元素,找出其左右两边最近的小于自己的元素.例如[1,3,2,4,5,1],元素2左边比自己小的元素是1,那么大于自己的区间就是[3],右边的区间就是[4,5].那么对于元素2来说,和左边区间合并组成[2,3]以及和右边区间合并组成[2,4,5],这两段区间包括2在内的所有subarray的最小值都是2,其分别可以组成的subarry的个数是 len([3])和len([4,5]),左右区间合并在一起可以组成的subarray的个数是len([3])…
题目如下: On an NxN chessboard, a knight starts at the r-th row and c-th column and attempts to make exactly K moves. The rows and columns are 0 indexed, so the top-left square is (0, 0), and the bottom-right square is (N-1, N-1). A chess knight has 8 po…
题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from these points, with sides not necessarily parallel to the x and y axes. If there isn't any rectangle, return 0. Example 1: Input: [[1,2],[2,1],[1,0],[…
[LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree-of-an-array/description/ 题目描述: Given a non-empty array of non-negative integers nums, the degree of this array is defined as the maximum frequency of…
[LeetCode]760. Find Anagram Mappings 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/find-anagram-mappings/description/ 题目描述: Given two lists Aand B, and B is an anagram of A. B is an anagram of A means B is made by randomizing the order o…
[LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/number-of-matching-subsequences/description/ 题目描述: Given string S and a dictionary of words words,…
[LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/my-calendar-ii/description/ 题目描述: Implement a MyCalendarTwo class to store your events. A new event can be added if…
[LeetCode]732. My Calendar III解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/my-calendar-iii/description/ 题目描述: Implement a MyCalendarThree class to store your events. A new event can always be added. Your class will have one method, book…
[LeetCode]873. Length of Longest Fibonacci Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/length-of-longest-fibonacci-subsequence/description/ 题目描述: A sequence X_…