|
亲!马上注册或者登录会查看更多内容!
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
test4.out 这个 test case 过不了,代码如下,不知道哪里出了问题,希望能看到 test case 里面的内容。
- class Solution {
- public boolean isAlmostSorted(int [] a) {
- int len = a.length;
- int start = 0;
- int end = len-1;
-
- if(isSorted(a)) return true;
-
- while(start < len - 1 && a[start] <= a[start + 1])
- start++;
- while(end > 0 && a[end] >= a[end - 1])
- end--;
-
- int tmp = a[start];
- a[start] = a[end];
- a[end] = tmp;
-
- if(isSorted(a)) return true;
-
- while(--end > ++start) {
- tmp = a[start];
- a[start] = a[end];
- a[end] = tmp;
- }
-
- if(isSorted(a)) return true;
-
- return false;
-
- }
-
- private boolean isSorted(int []a) {
- for (int i = 0; i < a.length-1; i++) {
- if ( a【i】 > a[i+1] ) {
- return false;
- }
- }
- return true;
- }
-
- }
复制代码
|
|