day29 * 491.递增子序列* 46.全排列* 47.全排列 II

目录

491. 非递减子序列

46. 全排列

47. 全排列 II


491. 非递减子序列

给你一个整数数组 nums ,找出并返回所有该数组中不同的递增子序列,递增子序列中 至少有两个元素 。你可以按 任意顺序 返回答案。

数组中可能含有重复元素,如出现两个整数相等,也可以视作递增序列的一种特殊情况。

示例 1:

输入:nums = [4,6,7,7]
输出:[[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]

示例 2:

输入:nums = [4,4,3,2,1]
输出:[[4,4]]
class Solution:
    def findSubsequences(self, nums: List[int]) -> List[List[int]]:
        path=[]
        result=[]
        self.backTracking(nums,0,path,result)
        return result
         
    def backTracking(self,nums,start,path,result):
        if len(path)>=2: #满足了至少有两个元素
            result.append(path[:])
        if start==len(nums):
            return  
        exist=set()
        for i in range(start,len(nums)):
            if (len(path)>0 and nums[i]<path[-1]) or nums[i] in exist:
                continue #若遇到递减序列或已经存在 则跳过
            exist.add(nums[i]) #标注已经在同一层存在过的
            path.append(nums[i])
            self.backTracking(nums,i+1,path,result)
            path.pop()

46. 全排列

给定一个不含重复数字的数组 nums ,返回其 所有可能的全排列 。你可以 按任意顺序 返回答案。

示例 1:

输入:nums = [1,2,3]
输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

示例 2:

输入:nums = [0,1]
输出:[[0,1],[1,0]]

示例 3:

输入:nums = [1]
输出:[[1]]

class Solution:
    def permute(self, nums: List[int]) -> List[List[int]]:
        exist=[]
        path=[]
        result=[]
        self.backTracking(nums,exist,path,result)
        return result

    def backTracking(self,nums,exist,path,result):
        if len(path)==len(nums):
            result.append(path[:])
            return
        for i in range(0,len(nums)):
            if nums[i] in exist: #通过exist确保垂直不重复
                continue
            exist.append(nums[i])
            path.append(nums[i])
            self.backTracking(nums,exist,path,result)
            path.pop()
            exist.pop() #exist垂直结束后要回溯

47. 全排列 II

给定一个可包含重复数字的序列 nums ,按任意顺序 返回所有不重复的全排列。

示例 1:

输入:nums = [1,1,2]
输出:
[[1,1,2],
 [1,2,1],
 [2,1,1]]

示例 2:

输入:nums = [1,2,3]
输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

class Solution:
    def permuteUnique(self, nums: List[int]) -> List[List[int]]:
        nums.sort()
        exist=[0]*len(nums)
        path=[]
        result=[]
        self.backTracking(nums,exist,path,result)
        return result
 
    def backTracking(self,nums,exist,path,result):
        if len(path)==len(nums):
            result.append(path[:])
            return
        for i in range(0,len(nums)):
            if (i > 0 and nums[i] == nums[i - 1] and not exist[i-1]) or exist[i]: #通过exist确保垂直不重复 通过i > 0 and nums[i] == nums[i - 1] and not exist[i-1](代表是横着)确保横着不重复
                continue
            exist[i]=1
            path.append(nums[i])
            self.backTracking(nums,exist,path,result)
            path.pop()
            exist[i]=0 #exist垂直结束后要回溯

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以将这些常量提取出来,并使用它们来计算方位角(azimuth)和仰角(elevation)。以下是一个示例代码: ```java import java.util.TimeZone; public class Main { private static final double MEAN_LONGITUDE_OFFSET = 4.894967873; private static final double MEAN_ANOMALY_COEFFICIENT = 0.01720197034; private static final double ECLIPTIC_LONGITUDE_COEFFICIENT_1 = 0.03342305518; private static final double ECLIPTIC_LONGITUDE_COEFFICIENT_2 = 0.0003490658504; private static final double OBLIQUITY_COEFFICIENT_1 = 0.4090877234; private static final double OBLIQUITY_COEFFICIENT_2 = 0.000000006981317008; private static final double SIDEREAL_COEFFICIENT_1 = 4.894961213; private static final double SIDEREAL_COEFFICIENT_2 = 6.300388099; public static void main(String[] args) { // 其他变量的定义和赋值 // Decimal hour of the day at Greenwich double greenwichtime = hour - timezone + minute / 60 + second / 3600; // Days from J2000, accurate from 1901 to 2099 double daynum = 367 * year - 7 * (year + (month + 9) / 12) / 4 + 275 * month / 9 + day - 730531.5 + greenwichtime / 24; //Mean longitude of the sun double mean_long = daynum * MEAN_ANOMALY_COEFFICIENT + MEAN_LONGITUDE_OFFSET; double mean_anom = daynum * MEAN_ANOMALY_COEFFICIENT + 6.240040768; double eclip_long = mean_long + ECLIPTIC_LONGITUDE_COEFFICIENT_1 * Math.sin(mean_anom) + ECLIPTIC_LONGITUDE_COEFFICIENT_2 * Math.sin(2 * mean_anom); double obliquity = OBLIQUITY_COEFFICIENT_1 - OBLIQUITY_COEFFICIENT_2 * daynum; double rasc = Math.atan2(Math.cos(obliquity) * Math.sin(eclip_long), Math.cos(eclip_long)); double decl = Math.asin(Math.sin(obliquity) * Math.sin(eclip_long)); double sidereal = SIDEREAL_COEFFICIENT_1 + SIDEREAL_COEFFICIENT_2 * daynum + rlon; double hour_ang = sidereal - rasc; double elevation = Math.asin(Math.sin(decl) * Math.sin(rlat) + Math.cos(decl) * Math.cos(rlat) * Math.cos(hour_ang)); //Local azimuth of the sun double azimuth = Math.atan2(-Math.cos(decl) * Math.cos(rlat) * Math.sin(hour_ang), Math.sin(decl) - Math.sin(rlat) * Math.sin(elevation)); azimuth = into_range(Math.toDegrees(azimuth), 0, 360); elevation = into_range(Math.toDegrees(elevation), -180, 180); // 其他操作 } private static double into_range(double value, double min, double max) { double range = max - min; return ((value - min) % range + range) % range + min; } } ``` 这样,您可以在计算方位角和仰角时直接使用这些常量,使代码更加易读和易于维护。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值