Redian新闻
>
Trump看见小红帽的川粉就会嘲笑 (转载)
avatar
Trump看见小红帽的川粉就会嘲笑 (转载)# EB23 - 劳工卡
f*e
1
*************************
int temp = A[i];
A[i] = A[A[i] -1];
A[A[i] - 1] = temp;
******************************
int temp = A[A[i]-1];
A[A[i]-1] = A[i];
A[i] = temp; ---------这几行过了, leetcode test.
*******************************
Given an unsorted integer array, find the first missing positive integer.
For example,
Given [1,2,0] return 3,
and [3,4,-1,1] return 2.
Your algorithm should run in O(n) time and uses constant space.
My solution:
public class Solution {
public int firstMissingPositive(int[] A) {
int i = 0;
while(i < A.length){
if(A[i] > 0 && A[i] <= A.length && A[i] != i + 1 && A[i] != A[A[
i] - 1]){
int temp = A[A[i]-1];
A[A[i]-1] = A[i];
A[i] = temp;
} else{
++i;
}
}

int j = 0;

while(j < A.length && A[j] - 1 == j) ++j;

return j+1;
}
}
百思不得其解, 请高人指点.
avatar
d*e
2
【 以下文字转载自 USANews 讨论区 】
发信人: cliffmudd (克女婿), 信区: USANews
标 题: Trump看见小红帽的川粉就会嘲笑
发信站: BBS 未名空间站 (Thu Dec 28 10:42:19 2017, 美东)
Trump只要看见usanews的小红帽的川粉,就会嘲笑:The little chinese. I love you
people. You have been learning so hard to look like us whites, but you will
never become white. No way. .. No way... No Way. We have a chinese
congresswoman in the capital
to represent NYC, and her name is “Mulan Judy Chu”, but she is not really
chinese. She is Taiwanese. You
little red hats will Make America Great Again.
avatar
l*8
3
int temp = A[i];
A[i] = A[A[i] -1];
A[A[i] - 1] = temp; // A[i] 已经不是原来的A[i]了, 改成A[temp-1] = temp;

【在 f**********e 的大作中提到】
: *************************
: int temp = A[i];
: A[i] = A[A[i] -1];
: A[A[i] - 1] = temp;
: ******************************
: int temp = A[A[i]-1];
: A[A[i]-1] = A[i];
: A[i] = temp; ---------这几行过了, leetcode test.
: *******************************
: Given an unsorted integer array, find the first missing positive integer.

avatar
f*e
4
不是用, temp存下来了吗?

【在 l*********8 的大作中提到】
: int temp = A[i];
: A[i] = A[A[i] -1];
: A[A[i] - 1] = temp; // A[i] 已经不是原来的A[i]了, 改成A[temp-1] = temp;

avatar
l*8
5
A[A[i] - 1] 里的A[i]不是原来的A[i]了

【在 f**********e 的大作中提到】
: 不是用, temp存下来了吗?
avatar
d*3
6
avatar
f*e
7
囧....
多谢两位.:-)
avatar
l*r
8
我也刚做了这题。:)

【在 f**********e 的大作中提到】
: 囧....
: 多谢两位.:-)

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。