- 打乱数组
ts
class Solution {
private nums: number[];
constructor(nums: number[]) {
this.nums = nums;
}
reset(): number[] {
return this.nums;
}
shuffle(): number[] {
//洗牌算法
let res = [...this.nums];
for (let i = 0; i < res.length; i++) {
const _i = Math.floor(Math.random() * res.length)
const temp = res[_i]
res[_i] = res[i]
res[i] = temp
}
return res;
}
}
- 最小栈
ts
class MinStack {
private nums: number[]
constructor() {
this.nums = []
}
push(val: number): void {
this.nums.push(val)
}
pop(): void {
if (this.nums.length) {
this.nums.pop()
}
}
top(): number {
if (this.nums.length) {
return this.nums[this.nums.length - 1]
} else {
return null
}
}
getMin(): number {
if (!this.nums.length) {
return null
}
let min = this.nums[0]
this.nums.forEach(e => {
min = Math.min(min, e)
})
return min
}
}
ts 把min单独计算
class MinStack {
private nums: number[]
private min: number
constructor() {
this.nums = []
}
push(val: number): void {
if (this.min === undefined || this.min === null) {
this.min = val
} else {
this.min = Math.min(this.min, val)
}
this.nums.push(val)
}
pop(): void {
if (this.nums.length) {
const boo = this.nums[this.nums.length - 1] === this.min
this.nums.pop()
if (boo) {
if (this.nums.length) {
let min = this.nums[0]
this.nums.forEach(e => {
min = Math.min(min, e)
})
this.min = min
} else {
this.min = null
}
}
}
}
top(): number {
if (this.nums.length) {
return this.nums[this.nums.length - 1]
} else {
return null
}
}
getMin(): number {
return this.min
}
}
- Fizz Buzz
ts
function fizzBuzz(n: number): string[] {
let strs: string[] = new Array(n)
for (let i = 0; i < n; i++) {
const b3 = (i + 1) % 3 === 0
const b5 = (i + 1) % 5 === 0
if (b3 && b5) {
strs[i] = 'FizzBuzz'
} else if (b3) {
strs[i] = 'Fizz'
} else if (b5) {
strs[i] = 'Buzz'
} else {
strs[i] = (i+1).toString()
}
}
return strs
};
py
class Solution:
def fizzBuzz(self, n: int) -> List[str]:
strs = []
for i in range(n):
b3 = (i+1)%3==0
b5 = (i+1)%5==0
if b3 and b5:
strs.append('FizzBuzz')
elif b3:
strs.append('Fizz')
elif b5:
strs.append('Buzz')
else:
strs.append(str(i+1))
return strs
C#
public class Solution {
public IList<string> FizzBuzz(int n) {
// List<string> str = [];
var str = new List<string>();
for(int i =0;i<n;i++)
{
bool b3 = (i+1)%3 == 0;
bool b5 = (i+1)%5 == 0;
if(b3&&b5)
{
str.Add("FizzBuzz");
}
else if(b3)
{
str.Add("Fizz");
}
else if(b5)
{
str.Add("Buzz");
}
else
{
str.Add((i+1).ToString());
}
}
return str;
}
}
- 计数质数
ts
function countPrimes(n: number): number {
// 特殊情况
if (n <= 2) return 0
let arr = new Array(n).fill(true)
let sum = 0
for (let i = 2; i < n; i++) {
if (arr[i]) {
sum++
for (let i2 = i; i2 < n; i2 += i) {
arr[i2] = false
}
}
}
return sum
}
py
class Solution:
def countPrimes(self, n: int) -> int:
if n<=2:
return 0
is_prime = [1] * n
count = 0
for i in range(2, n):
if is_prime[i]:
count += 1
for j in range(i*i, n, i):
is_prime[j] = 0
return count
C#
public class Solution {
public int CountPrimes(int n) {
if(n<=2)
{
return 0;
}
int sum = 0;
var list = new int[n];
for(int i =2;i<n;i++)
{
if(list[i] == 0)
{
sum++;
for(int i2 = i;i2<n;i2+=i)
{
list[i2] = 1;
}
}
}
return sum;
}
}
- 3 的幂
ts
function isPowerOfThree(n: number): boolean {
if (n === 1) return true
const chu3 = (num): boolean => {
if (num === 3) return true
if (num < 3 && num > -3) return false
return chu3(num / 3)
}
return chu3(n)
};
Python
class Solution:
def isPowerOfThree(self, n: int) -> bool:
if n == 1:
return True
def chu3(num:int)-> bool:
if num == 3:
return True
if -3<num<3:
return False
return chu3(num/3)
return chu3(n)
Python
class Solution:
def isPowerOfThree(self, n: int) -> bool:
while(n != 0 and n%3==0):
n = n/3
return n == 1
C#
public class Solution {
public bool IsPowerOfThree(int n) {
while (n != 0 && n % 3 == 0) {
n /= 3;
}
return n == 1;
}
}