# LeetCode算法代码笔记（31-35）

`给自己的目标：[LeetCode](https://leetcode.com/ "Online Judge Platform") 上每日一题`

## 31. Next Permutation

``````1,2,3 ->next
1,3,2 ->next
2,1,3 ->next
2,3,1 ->next
3,1,2 ->next
3,2,1 ->next
1,2,3 ->loop
``````

``````public class Solution {
public void nextPermutation(int[] nums) {
if (nums.length == 0 || nums.length == 1) return;
int len = nums.length;
int index = len - 1;
int value = nums[index];
for (index = index - 1; index >= 0; index--) {
if (nums[index] < value) {
value = nums[index];
break;
}
value = nums[index];
}
if (index < 0) {
reversal(nums, 0, len - 1);
} else {
for (int j = len - 1; j > index; j--) {
if (nums[j] > value) {
nums[index] = nums[j];
nums[j] = value;
reversal(nums, index + 1, len - 1);
break;
}
}
}
}

public void reversal(int[] nums, int start, int end) {
int len = end + 1 - start;
for (int i = 0; i < len / 2; i++) {
int k = nums[start + i];
nums[start + i] = nums[end - i];
nums[end - i] = k;
}
}
}
``````

## 32. Longest Valid Parentheses

``````")()())", where the longest valid parentheses substring is "()()", which has length = 4.
``````

``````public class Solution {
public int longestValidParentheses(String s) {
if (s.isEmpty()) return 0;
Stack<Integer> ms = new Stack<>();
int maxlen = 0;
int last = -1;  // Position of the last unmatched ')'
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '(') ms.push(i); //
else if (ms.empty()) last = i; // == ')' and the stack is empty, it means this is a non-matching ')'
else {
ms.pop();           // pops the matching '('.
if (ms.empty()) // If there is nothing left in the stack, it means this ')' matches a '(' after the last unmatched ')'
maxlen = Math.max(maxlen, i - last);
else    // otherwise,
maxlen = Math.max(maxlen, i - ms.peek());
}
}
return maxlen;
}
}
``````

## 33.Search in Rotated Sorted Array

``````/**
* 简单遍历
*/
public class Solution {
public int search(int[] nums, int target) {
for (int i = 0; i < nums.length; i++) {
if (nums[i] == target) {
return i;
}
}
return -1;
}
}
``````

## 34. Search for a Range

``````For example,
Given [5, 7, 7, 8, 8, 10] and target value 8,
return [3, 4].
``````

``````public class Solution {
public int[] searchRange(int[] nums, int target) {
int start = -1, end = -1;
int st = 0, len = nums.length - 1;
while (st <= len) {
int mid = (len + st + 1) / 2;
if (nums[mid] == target) {
start = binarySearch(nums, st, mid - 1, target, true);
end = binarySearch(nums, mid + 1, len, target, false);
if (start == -1) start = mid;
if (end == -1) end = mid;
break;
} else if (nums[mid] > target) {
len = mid - 1;
} else if (nums[mid] < target) {
st = mid + 1;
}
}
return new int[]{start, end};
}

public int binarySearch(int[] nums, int start, int end, int target, boolean left) {
if (start > end) {
return -1;
}
int mid = (start + end + 1) / 2;
int index = -1;
if (nums[mid] == target) {
if (left) {
index = binarySearch(nums, start, mid - 1, target, left);
} else {
index = binarySearch(nums, mid + 1, end, target, left);
}
if (index == -1) {
return mid;
}
return index;
} else if (nums[mid] > target) {
return binarySearch(nums, start, mid - 1, target, left);
} else if (nums[mid] < target) {
return binarySearch(nums, mid + 1, end, target, left);
} else {
return index;
}
}
}
``````

## 35. Search Insert Position

``````Here are few examples.
[1,3,5,6], 5 → 2
[1,3,5,6], 2 → 1
[1,3,5,6], 7 → 4
[1,3,5,6], 0 → 0
``````

``````public class Solution {
public int searchInsert(int[] nums, int target) {
int len = nums.length;
int start = 0;
int end = len - 1;
int index = start;
while (start <= end) {
int mid = (start + end) / 2;
if (target > nums[mid]) {
start = mid + 1;
index = start;
} else if (target < nums[mid]) {
end = mid - 1;
index = mid;
} else {
return mid;
}
}
return index;
}
}
``````

### 推荐阅读更多精彩内容

• 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪：刷leetcod...
土汪阅读 11,318评论 0 33
• leetcode刷题记录本文记录一下leetcode刷题记录，记录一下自己的解法和心得。 LeetCode Two...
EarthChen阅读 1,898评论 0 6
• 第5章 引用类型(返回首页) 本章内容 使用对象 创建并操作数组 理解基本的JavaScript类型 使用基本类型...
大学一百阅读 1,400评论 0 4
• Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具（例如配置管理，服务发现，断路器，智...
卡卡罗2017阅读 118,269评论 14 132
• 你问我为什么沉默 我所有的静默 都是为了最后 最后笑成一树花开 ……
云妮yunni阅读 37评论 0 2