国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Lintcode: Previous Permuation

2019-11-14 23:35:15
字體:
來源:轉載
供稿:網友
Lintcode: PRevious Permuation
Given a list of integers, which denote a permutation.Find the previous permutation in ascending order.NoteThe list may contains duplicate integers.ExampleFor [1,3,2,3], the previous permutation is [1,2,3,3]For [1,2,3,4], the previous permutation is [4,3,2,1]

跟Next Permutation很像,只不過條件改成

for (int i=nums.lenth-2; i>=0; i--)

  if (nums[i] > nums[i+1]) break;

for (int j=i; j<num.length-1; j++)

  if (nums[j+1]>=nums[i]) break;

 1 public class Solution { 2     /** 3      * @param nums: A list of integers 4      * @return: A list of integers that's previous permuation 5      */ 6     public ArrayList<Integer> previousPermuation(ArrayList<Integer> nums) { 7         // write your code 8         if (nums==null || nums.size()==0) return nums; 9         int i = nums.size()-2;10         for (; i>=0; i--) {11             if (nums.get(i) > nums.get(i+1)) break;12         }13         if (i >= 0) {14             int j=i;15             for (; j<=nums.size()-2; j++) {16                 if (nums.get(j+1) >= nums.get(i)) break;17             }18             int temp = nums.get(j);19             nums.set(j, nums.get(i));20             nums.set(i, temp);21         }22         reverse(nums, i+1);23         return nums;24     }25     26     public void reverse(ArrayList<Integer> nums, int k) {27         int l = k, r = nums.size()-1;28         while (l < r) {29             int temp = nums.get(l);30             nums.set(l, nums.get(r));31             nums.set(r, temp);32             l++;33             r--;34         }35     }36 }


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 千阳县| 盖州市| 五家渠市| 锡林郭勒盟| 诸城市| 东海县| 浠水县| 高平市| 永昌县| 南昌市| 阳朔县| 广安市| 密山市| 玉龙| 金乡县| 上饶市| 灌南县| 乐至县| 健康| 开阳县| 金塔县| 濮阳县| 行唐县| 兴义市| 巴中市| 嵩明县| 黔西县| 比如县| 乐东| 喀什市| 墨玉县| 清镇市| 岳阳县| 泽州县| 河西区| 忻城县| 台南县| 浦东新区| 崇信县| 凤翔县| 自贡市|