国产探花免费观看_亚洲丰满少妇自慰呻吟_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 }


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 中方县| 百色市| 莲花县| 视频| 武夷山市| 龙口市| 崇明县| 惠州市| 盘山县| 东安县| 平利县| 大同市| 宿州市| 武穴市| 汾阳市| 天镇县| 普安县| 兰坪| 昌都县| 禹州市| 历史| 樟树市| 镇赉县| 通海县| 荆州市| 普格县| 永寿县| 通江县| 清原| 娄烦县| 绵阳市| 黄梅县| 分宜县| 长乐市| 勐海县| 和林格尔县| 武川县| 西丰县| 密云县| 定州市| 永嘉县|