leetcode 1025. Divisor Game

leetcode
九章

题目描述

Alice and Bob take turns playing a game, with Alice starting first.

Initially, there is a number N on the chalkboard. On each player’s turn,
that player makes a move consisting of:

  • Choosing any x with 0 < x < N and N % x == 0.
  • Replacing the number N on the chalkboard with N - x.

Also, if a player cannot make a move, they lose the game.

Return True if and only if Alice wins the game, assuming both players play
optimally.

Example 1:

Input: 2
Output: true
Explanation: Alice chooses 1, and Bob has no more moves.

Example 2:

Input: 3
Output: false
Explanation: Alice chooses 1, Bob chooses 1, and Alice has no more moves.

Note:

  1. 1 <= N <= 1000

Tags: Math, Dynamic Programming

Difficulty: Easy

答案



leetcode 224. Basic Calculator

leetcode
九章

题目描述

Implement a basic calculator to evaluate a simple expression string.

The expression string may contain open ( and closing parentheses ), the
plus + or minus sign -, non-negative integers and empty spaces .

Example 1:

Input: "1 + 1"
Output: 2

Example 2:

Input: " 2-1 + 2 "
Output: 3

Example 3:

Input: "(1+(4+5+2)-3)+(6+8)"
Output: 23

Note:

  • You may assume that the given expression is always valid.
  • Do not use the eval built-in library function.

展开:

Difficulty: Hard

答案



leetcode 1041. Robot Bounded In Circle

leetcode
九章

题目描述

On an infinite plane, a robot initially stands at (0, 0) and faces north.
The robot can receive one of three instructions:

  • "G": go straight 1 unit;
  • "L": turn 90 degrees to the left;
  • "R": turn 90 degress to the right.

The robot performs the instructions given in order, and repeats them
forever.

Return true if and only if there exists a circle in the plane such that the
robot never leaves the circle.

Example 1:
Input: “GGLLGG” Output: true Explanation: The robot moves from (0,0) to (0,2), turns 180 degrees, and then returns to (0,0). When repeating these instructions, the robot remains in the circle of radius 2 centered at the origin.

Example 2:
Input: “GG” Output: false Explanation: The robot moves north indefinitely.

Example 3:
Input: “GL” Output: true Explanation: The robot moves from (0, 0) -> (0, 1) -> (-1, 1) -> (-1, 0) -> (0, 0) -> …

Note:

  1. 1 <= instructions.length <= 100
  2. instructions[i] is in {'G', 'L', 'R'}

Tags: Math

Difficulty: Medium

答案



leetcode 264. Ugly Number II

leetcode
九章

题目描述

Write a program to find the n-th ugly number.

Ugly numbers are positive numbers whose prime factors only include 2, 3, 5.

Example:

Input: n = 10
Output: 12
Explanation:1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers.

Note:

  1. 1 is typically treated as an ugly number.
  2. n does not exceed 1690.

Tags: Math, Dynamic Programming, Heap

Difficulty: Medium

答案



leetcode 263. Ugly Number

leetcode
九章

题目描述

Write a program to check whether a given number is an ugly number.

Ugly numbers are positive numbers whose prime factors only include 2, 3, 5.

Example 1:

Input: 6
Output: true
Explanation: 6 = 2 × 3

Example 2:

Input: 8
Output: true
Explanation: 8 = 2 × 2 × 2

Example 3:

Input: 14
Output: false 
Explanation:14 is not ugly since it includes another prime factor 7.

Note:

  1. 1 is typically treated as an ugly number.
  2. Input is within the 32-bit signed integer range: [−231, 231 − 1].

Tags: Math

Difficulty: Easy

答案



leetcode 231. Power of Two

leetcode
九章

题目描述

Given an integer, write a function to determine if it is a power of two.

Example 1:

Input: 1
Output: true 
Explanation: 20 = 1

Example 2:

Input: 16
Output: true
Explanation: 24 = 16

Example 3:

Input: 218
Output: false

Tags: Math, Bit Manipulation

Difficulty: Easy

答案



leetcode 50. Pow(x, n)

难度:Middle

leetcode
九章

题目描述

Implement pow( x , n
)
, which calculates x
raised to the power n (xn).

Example 1:

Input: 2.00000, 10
Output: 1024.00000

Example 2:

Input: 2.10000, 3
Output: 9.26100

Example 3:

Input: 2.00000, -2
Output: 0.25000
Explanation: 2-2 = 1/22 = 1/4 = 0.25

Note:

  • -100.0 < x < 100.0
  • n is a 32-bit signed integer, within the range [−231, 231 − 1]

Tags: Math, Binary Search

Difficulty: Medium

答案

leetcode 69. Sqrt(x)

难度:Easy

leetcode
九章

题目描述

Implement int sqrt(int x).

Compute and return the square root of x , where x is guaranteed to be a
non-negative integer.

Since the return type is an integer, the decimal digits are truncated and only
the integer part of the result is returned.

Example 1:

Input: 4
Output: 2

Example 2:

Input: 8
Output: 2
Explanation: The square root of 8 is 2.82842..., and since 
             the decimal part is truncated, 2 is returned.

Tags: Math, Binary Search

Difficulty: Easy

答案

leetcode 279. Perfect Squares

难度:Middle

leetcode
九章

题目描述

Given a positive integer n , find the least number of perfect square numbers
(for example, 1, 4, 9, 16, ...) which sum to n.

Example 1:

Input: _n_ = 12
Output: 3 
Explanation:12 = 4 + 4 + 4.

Example 2:

Input: _n_ = 13
Output: 2
Explanation:13 = 4 + 9.

Tags: Math, Dynamic Programming, Breadth-first Search

Difficulty: Medium

答案

leetcode 268. Missing Number

难度:Easy

leetcode
九章

题目描述

Given an array containing n distinct numbers taken from 0, 1, 2, ..., n,
find the one that is missing from the array.

Example 1:

Input: [3,0,1]
Output: 2

Example 2:

Input: [9,6,4,2,3,5,7,0,1]
Output: 8

Note :
Your algorithm should run in linear runtime complexity. Could you implement it
using only constant extra space complexity?

Tags: Array, Math, Bit Manipulation

Difficulty: Easy

答案


:D 一言句子获取中...