如果您对[Swift]LeetCode665.非递减数列|Non-decreasingArray感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于[Swift]LeetCode
如果您对[Swift]LeetCode665. 非递减数列 | Non-decreasing Array感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于[Swift]LeetCode665. 非递减数列 | Non-decreasing Array的详细内容,我们还将为您解答非递减数列什么意思的相关问题,并且为您提供关于(non-)interactive (non-)login shell、1009. Increasing and Decreasing (构造 / 最长递增 (减) 子序列) 2020 Multi-University Training Contest 7、665. Non-decreasing Array - LeetCode、array – 为什么`Array(0,1,2)== Array(0,1,2)`不返回预期的结果?的有价值信息。
本文目录一览:- [Swift]LeetCode665. 非递减数列 | Non-decreasing Array(非递减数列什么意思)
- (non-)interactive (non-)login shell
- 1009. Increasing and Decreasing (构造 / 最长递增 (减) 子序列) 2020 Multi-University Training Contest 7
- 665. Non-decreasing Array - LeetCode
- array – 为什么`Array(0,1,2)== Array(0,1,2)`不返回预期的结果?
[Swift]LeetCode665. 非递减数列 | Non-decreasing Array(非递减数列什么意思)
Given an array with n
integers,your task is to check if it Could become non-decreasing by modifying at most 1
element.
We define an array is non-decreasing if array[i] <= array[i + 1]
holds for every i
(1 <= i < n).
Example 1:
Input: [4,2,3] Output: True Explanation: You Could modify the first to to get a non-decreasing array. 41
Example 2:
Input: [4,1] Output: False Explanation: You can‘t get a non-decreasing array by modify at most one element.
Note: The n
belongs to [1,10,000].
给定一个长度为 n
的整数数组,你的任务是判断在最多改变 1
个元素的情况下,该数组能否变成一个非递减数列。
我们是这样定义一个非递减数列的: 对于数组中所有的 i
(1 <= i < n),满足 array[i] <= array[i + 1]
。
示例 1:
输入: [4,3] 输出: True 解释: 你可以通过把第一个4变成1来使得它成为一个非递减数列。
示例 2:
输入: [4,1] 输出: False 解释: 你不能在只改变一个元素的情况下将其变为非递减数列。
说明: n
的范围为 [1,000]。
44ms
1 class Solution { 2 func checkPossibility(_ nums: [Int]) -> Bool { 3 var i = 0 4 var j = nums.count - 1 5 while i < j && nums[i] <= nums[i+1] { 6 i += 1 7 } 8 while i < j && nums[j] >= nums[j-1] { 9 j -= 1 10 } 11 let head = (i == 0) ? Int.min : nums[i-1] 12 let next = (j == nums.count - 1) ? Int.max : nums[j+1] 13 14 if j - i <= 1 && (head < nums[j] || nums[i] < next) { 15 return true 16 } else { 17 return false 18 } 19 } 20 }
188ms
1 class Solution { 2 func checkPossibility(_ nums: [Int]) -> Bool { 3 4 if nums.count < 3 { 5 return true 6 } 7 8 var i = 0 9 10 while i <= nums.count - 2,nums[i] <= nums[i + 1] { 11 i += 1 12 } 13 14 var j = nums.count - 1 15 16 if i >= j - 1 { 17 return true 18 } 19 20 while 1 <= j,nums[j - 1] <= nums[j] { 21 j -= 1 22 } 23 24 if j <= 1 { 25 return true 26 } 27 28 return ((j - i == 2) && (nums[i] <= nums[j])) 29 || 30 ((j - i == 1) 31 && 32 ((0 < i) && (nums[i - 1] <= nums[j]) 33 || ((j < nums.count - 1) && (nums[i] <= nums[j + 1])))) 34 } 35 }
1 class Solution { 2 func checkPossibility(_ nums: [Int]) -> Bool { 3 var nums = nums 4 var cnt:Int = 1 5 var n:Int = nums.count 6 for i in 1..<n 7 { 8 if nums[i] < nums[i - 1] 9 { 10 if cnt == 0 {return false} 11 if i == 1 || nums[i] >= nums[i - 2] 12 { 13 nums[i - 1] = nums[i] 14 } 15 else 16 { 17 nums[i] = nums[i - 1] 18 } 19 cnt -= 1 20 } 21 } 22 return true 23 } 24 }
196ms
1 class Solution { 2 func checkPossibility(_ nums: [Int]) -> Bool { 3 var c = 0 4 var nums = [-Int.max] + nums 5 var low = nums[0] 6 7 for (i,n) in nums.enumerated().dropFirst() { 8 if n < nums[i-1] { 9 if n >= low { 10 nums[i-1] = low 11 } else { 12 nums[i] = nums[i-1] 13 } 14 c += 1 15 if c > 1 { 16 return false 17 } 18 } 19 if i > 1 { 20 low = nums[i-1] 21 } 22 } 23 24 return true 25 } 26 }
200ms
1 class Solution { 2 func checkPossibility(_ nums: [Int]) -> Bool { 3 var newNums = nums 4 if newNums.count <= 2 { 5 return true 6 } 7 8 var i = 1 9 var j = 2 10 var noOfChange = 0 11 12 if (newNums[i] < newNums[i-1]) { 13 newNums[i-1] = newNums[i] 14 noOfChange = noOfChange + 1 15 } 16 17 while j <= newNums.count - 1 { 18 if (newNums[i] > newNums[j]) { 19 //if [j] > [i-1],change value of [i] -> [i-1] 20 if (newNums[j] > newNums[i-1]) { 21 newNums[i] = newNums[i-1] 22 } 23 //else,change [j] -> [i] 24 else { 25 newNums[j] = newNums[i] 26 } 27 28 noOfChange = noOfChange + 1 29 } 30 j = j + 1 31 i = i + 1 32 } 33 34 return noOfChange <= 1 35 } 36 }
208ms
1 class Solution { 2 func checkPossibility(_ nums: [Int]) -> Bool { 3 4 if nums.count < 3 { 5 return true 6 } 7 8 var index = -1 9 10 for i in 0 ..< (nums.count - 1) { 11 if nums[i] > nums[i+1] { 12 if index != -1 { return false } 13 index = i 14 } 15 } 16 17 return ((index == -1) 18 || (index == 0) 19 || (index == nums.count - 2) 20 || (nums[index+1] - nums[index-1] > 0) 21 || (nums[index+2] - nums[index] > 0)) 22 } 23 }
284ms
1 class Solution { 2 func checkPossibility(_ nums: [Int]) -> Bool { 3 guard nums.count > 1 && nums.count <= 10000 else { 4 return nums.count == 1 5 } 6 var array = nums 7 var modified = false 8 for i in 0 ..< array.count - 1 { 9 if array[i] > array[i + 1] { 10 if modified { 11 return false 12 } 13 if i > 0 && array[i - 1] > array[i + 1] { 14 array[i + 1] = array[i] 15 } 16 modified = true 17 } 18 } 19 return true 20 } 21 }
(non-)interactive (non-)login shell
1 login shell
当bash以login shell形式登录的时候,bash会依次执行下列脚本,进行关键全局变量的初始化,如PATH
。
/etc/profile
~/.bash_profile
~/.bash_login
~/.profile
使用-
,-l
,--login
选项可指定以login shell的形式登录,--noprofile
选项可使bash不去执行这些脚本。
当login shell退出的时候,bash会执行如下脚本进行推出前处理:
~/.bash_logout
/etc/bash.bash_logout
2 interactive shell
使用-i
选项启动interactive shell,该类型的shell读下列脚本进行关键匿名的初始化,如alias ll='ls -l --color=auto'
~/.bashrc
初始化文件可通过--norc
选项屏蔽,或--rcfile
重新指定
3 注意
/etc/bashrc
一般会被/etc/profile
,~/.bashrc
用到,所以无论是interactive shell还是login shell,该文件的内容都会生效。
4 结论
无论interactive login shell,interactive non-login shell,non-interactive login shell,non-interactive non-login file,不同类型shell的组合其本质区别就在于初始化的脚本运行顺序不同。
5 参考
- man bash
- What are the differences between a login shell and interactive shell? - stackoverflow
1009. Increasing and Decreasing (构造 / 最长递增 (减) 子序列) 2020 Multi-University Training Contest 7
传送门
思路:
- 题意: 让构造一个长度为 n 的序列,使得其最长递增子序列长度为 x,最长递减子序列的长度为 y。若无法构成自己输出 “NO”。
- 官方题解:
- cls 代码思路:
将整个序列分成 x 块,每一块找一个元素出来形成的就是最长递增子序列;而递减序列正好是某一整块元素。
代码实现:
#include<bits/stdc++.h>
#define endl ''\n''
#define null NULL
#define ll long long
#define int long long
#define pii pair<int, int>
#define lowbit(x) (x &(-x))
#define ls(x) x<<1
#define rs(x) (x<<1+1)
#define me(ar) memset(ar, 0, sizeof ar)
#define mem(ar,num) memset(ar, num, sizeof ar)
#define rp(i, n) for(int i = 0, i < n; i ++)
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define pre(i, n, a) for(int i = n; i >= a; i --)
#define IOS ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
const int way[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
using namespace std;
const int inf = 0x7fffffff;
const double PI = acos(-1.0);
const double eps = 1e-6;
const ll mod = 1e9 + 7;
const int N = 2e5 + 5;
int t, n, x, y;
vector<int> vt;
signed main()
{
IOS;
cin >> t;
while(t --){
cin >> n >> x >> y;
int a = sqrt(n), b = n/a;
if(a*b != n) b ++;
if(x+y > n+1 || x+y < a+b) cout << "NO" << endl;
else{
cout << "YES" << endl;
vt.clear();
for(int i = x; i; i --){
int xx = min(n-i+1, y);
for(int j = n-xx+1; j <= n; j ++) vt.push_back(j);
n -= xx;
}
for(int i = vt.size()-1; ~i; i --) cout << vt[i] << " \n"[!i];
}
}
return 0;
}
665. Non-decreasing Array - LeetCode
Question
665. Non-decreasing Array
Solution
题目大意:
思路:当前判断2的时候可以将当前元素2变为4,也可以将上一个元素4变为2,再判断两变化后是否满足要求。
Java实现:
public boolean checkPossibility(int[] nums) {
if (nums == null || nums.length < 3) return true;
int count = 0;
// 判断前2个
if (nums[1] < nums[0]) {
nums[0] = nums[1] - 1;
count++;
}
for (int i = 2; i < nums.length; i++) {
if (nums[i] < nums[i - 1]) {
count++;
if (nums[i - 2] <= nums[i] - 1) {
nums[i - 1] = nums[i] - 1;
} else if (i == nums.length -1 || nums[i + 1] >= nums[i - 1] + 1) {
nums[i] = nums[i - 1] + 1;
} else {
return false;
}
}
}
return count < 2;
}
别人实现:
public boolean checkPossibility(int[] nums) {
int cnt = 0; //the number of changes
for(int i = 1; i < nums.length && cnt<=1 ; i++){
if(nums[i-1] > nums[i]){
cnt++;
//modify nums[i-1] of a priority
if(i-2<0 || nums[i-2] <= nums[i])nums[i-1] = nums[i];
else nums[i] = nums[i-1]; //have to modify nums[i]
}
}
return cnt<=1;
}
array – 为什么`Array(0,1,2)== Array(0,1,2)`不返回预期的结果?
我期望Array(0,1,2)== Array(0,2)比较自然等式。例如,检查与其他数组的相应元素相比,数组的所有元素是否都返回true。
人们告诉我,Scala的数组只是一个Java [],只比较身份。是不是更有意义的重写Array’sequals方法比较自然平等?
解决方法
尝试下面2.8(编辑/注:从RC3,GenericArray是ArraySeq – 感谢retronym指出这一点):
import scala.collection.mutable.{GenericArray=>GArray,WrappedArray=>WArray} scala> GArray(0,2) == GArray(0,2) res0: Boolean = true scala> (Array(0,2):WArray[Int]) == (Array(0,2):WArray[Int]) res1: Boolean = true
GenericArray的行为就像Array,除了所有的Scala集合好处添加进来。WrappedArray wraps Java [] array;上面,我把一个简单的数组给它(比调用隐式转换函数容易),然后比较包装的数组。这些包装,尽管支持一个[]数组,也给你所有的收集好东西。
我们今天的关于[Swift]LeetCode665. 非递减数列 | Non-decreasing Array和非递减数列什么意思的分享已经告一段落,感谢您的关注,如果您想了解更多关于(non-)interactive (non-)login shell、1009. Increasing and Decreasing (构造 / 最长递增 (减) 子序列) 2020 Multi-University Training Contest 7、665. Non-decreasing Array - LeetCode、array – 为什么`Array(0,1,2)== Array(0,1,2)`不返回预期的结果?的相关信息,请在本站查询。
本文标签: