leetcode题解: Next Permutation

最近还一直在刷leetcode,当然,更多时候只是将题解写在自己的电脑上,没有分享出来。偶尔想起来的时候,就写出来。

public class Solution {
   public void nextPermutation(int[] nums) {
        if(nums==null||nums.length<=1) return;
        nextPermutationHelp( nums,0,nums.length-1);

    }
	public void nextPermutationHelp(int []nums,int begin,int end){
	int i=end;
		if((i>=1)&&(nums[i]>nums[i-1])) {  // 1 2 3 4这类的
			reverse(nums,i-1,i);
		    return ;
		}
		for(i=end;i>=1;i--){                    //1 2 4 3这类
			if(nums[i]>nums[i-1]) {       //从后往前扫描
				 break;
			}
		}
		reverse(nums,i,end);                   //如上面的1 2 4 3 ,先进行逆转,得到1 2 3 4
		if(i>=1){
			for(int j=i;j<=end;j++){      // 然后进行递增,找到比2大的第一个数,进行交换,最后得到 1 3 2 4
				if(nums[j]>nums[i-1]) {
					int t=nums[i-1];
					nums[i-1]=nums[j];
					nums[j]=t;
					break;
				}

			}
		}

	}
	public void reverse(int[] nums,int begin,int end){
		for(int i=begin,j=end;i<=(begin+end)/2;i++,j--){
			int temp=nums[i];
			nums[i]=nums[j];
			nums[j]=temp;
		}

	}
}

  

时间: 2024-10-05 13:22:25

leetcode题解: Next Permutation的相关文章

[LeetCode]题解(python):031-Next Permutation

题目来源 https://leetcode.com/problems/next-permutation/ Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible

[LeetCode] 031. Next Permutation (Medium) (C++/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 031. Next Permutation (Medium) 链接: 题目:https://oj.leetcode.com/problems/next-permutation/ 代码(github):https://github.com/illuz/leetcode 题意: 求一个序列的下一个排列. 分析: 可以用

(leetcode题解)Pascal&#39;s Triangle

Pascal's Triangle  Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 题意实现一个杨辉三角. 这道题只要注意了边界条件应该很好实现出来,C++实现如下 vector<vector<int>> generate(int

leetcode 题解:Search in Rotated Sorted Array II (旋转已排序数组查找2)

题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 说明: 1)和1比只是有重复的数字,整体仍采用二分查找 2)方法二 : 实现:  

[LeetCode 题解]: Binary Tree Preorder Traversal

Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 题意 先序遍历二叉树,递归的思路是普通的,能否用迭代呢? 非递归思路:<借助stack>

LeetCode题解

Reverse Words in a String 考虑几个特殊的情况1.若字符窜s="  "2.字符窜s=“a  b  d     e”3.字符窜s=“ a” class Solution { public: void reverseWords(string &s) { int i; int cas=0; string st[100]; s+=' '; for(i=0;i<s.size();i++) { if(i==0 && s[0]==' ') con

leetcode题解:Search in Rotated Sorted Array(旋转排序数组查找)

题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no du

[LeetCode 题解]: Reverse Nodes in K-Groups

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. You may not alter the values in the nodes, only nod

[LeetCode 题解]: Linked List Cycle II

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up:Can you solve it without using extra space? 题意: 给定一个链表,找到环起始的位置.如果环不存在,返回NULL. 分析: (1)首先要判断该链表是否有环.如果没有环,那么返回NULL. (2)其次,当已知环存在后,寻找环起始的位置. 思路: (

[LeetCode 题解]:Candy

There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get more candies