File tree 3 files changed +117
-1
lines changed
lcci/17.04.Missing Number
3 files changed +117
-1
lines changed Original file line number Diff line number Diff line change 26
26
27
27
<!-- 这里可写通用的实现逻辑 -->
28
28
29
- 利用异或的特性,` res = res ^ x ^ x ` 。对同一个值异或两次,结果等于它本身。最后异或的结果,就是只出现一次的数字,即数组中缺失的整数。
29
+ 1 . 排序
30
+ - 排序 ` nums ` ,检查 ` nums[i] == i ` ,返回条件不成立的 ` i ` 。
31
+ - 若全部符合条件,则返回 ` nums.length ` 。
32
+ 2 . 数学
33
+ - 遍历 ` nums ` ,统计总和,并记录其中的最大值。
34
+ - 若最大值与 ` nums.length ` 一致,使用高斯算法(` (n + 1) * n / 2 ` )计算总和,与遍历总和相减,得到结果。
35
+ - 不一致,则缺失的就是 ` nums.length ` 。
36
+ 3 . 位运算
37
+ - 利用异或的特性,` res = res ^ x ^ x ` 。对同一个值异或两次,结果等于它本身。最后异或的结果,就是只出现一次的数字,即数组中缺失的整数。
30
38
31
39
<!-- tabs:start -->
32
40
@@ -90,6 +98,55 @@ public:
90
98
};
91
99
```
92
100
101
+ ### **Rust**
102
+
103
+ ```rust
104
+ impl Solution {
105
+ pub fn missing_number(mut nums: Vec<i32>) -> i32 {
106
+ nums.sort();
107
+ let n = nums.len() as i32;
108
+ for i in 0..n {
109
+ if i != nums[i as usize] {
110
+ return i;
111
+ }
112
+ }
113
+ n
114
+ }
115
+ }
116
+ ```
117
+
118
+ ``` rust
119
+ impl Solution {
120
+ pub fn missing_number (nums : Vec <i32 >) -> i32 {
121
+ let n = nums . len () as i32 ;
122
+ let mut sum = 0 ;
123
+ let mut max = 0 ;
124
+ for num in nums {
125
+ sum += num ;
126
+ max = max . max (num );
127
+ }
128
+ if max == n {
129
+ ((1 + max ) * max / 2 ) - sum
130
+ } else {
131
+ n
132
+ }
133
+ }
134
+ }
135
+ ```
136
+
137
+ ``` rust
138
+ impl Solution {
139
+ pub fn missing_number (nums : Vec <i32 >) -> i32 {
140
+ let mut res = 0 ;
141
+ let n = nums . len ();
142
+ for i in 0 .. n {
143
+ res ^= nums [i ] ^ (i + 1 ) as i32 ;
144
+ }
145
+ res
146
+ }
147
+ }
148
+ ```
149
+
93
150
### ** ...**
94
151
95
152
```
Original file line number Diff line number Diff line change @@ -88,6 +88,55 @@ public:
88
88
};
89
89
```
90
90
91
+ ### **Rust**
92
+
93
+ ```rust
94
+ impl Solution {
95
+ pub fn missing_number(mut nums: Vec<i32>) -> i32 {
96
+ nums.sort();
97
+ let n = nums.len() as i32;
98
+ for i in 0..n {
99
+ if i != nums[i as usize] {
100
+ return i;
101
+ }
102
+ }
103
+ n
104
+ }
105
+ }
106
+ ```
107
+
108
+ ``` rust
109
+ impl Solution {
110
+ pub fn missing_number (nums : Vec <i32 >) -> i32 {
111
+ let n = nums . len () as i32 ;
112
+ let mut sum = 0 ;
113
+ let mut max = 0 ;
114
+ for num in nums {
115
+ sum += num ;
116
+ max = max . max (num );
117
+ }
118
+ if max == n {
119
+ ((1 + max ) * max / 2 ) - sum
120
+ } else {
121
+ n
122
+ }
123
+ }
124
+ }
125
+ ```
126
+
127
+ ``` rust
128
+ impl Solution {
129
+ pub fn missing_number (nums : Vec <i32 >) -> i32 {
130
+ let mut res = 0 ;
131
+ let n = nums . len ();
132
+ for i in 0 .. n {
133
+ res ^= nums [i ] ^ (i + 1 ) as i32 ;
134
+ }
135
+ res
136
+ }
137
+ }
138
+ ```
139
+
91
140
### ** ...**
92
141
93
142
```
Original file line number Diff line number Diff line change
1
+ impl Solution {
2
+ pub fn missing_number ( nums : Vec < i32 > ) -> i32 {
3
+ let mut res = 0 ;
4
+ let n = nums. len ( ) ;
5
+ for i in 0 ..n {
6
+ res ^= nums[ i] ^ ( i + 1 ) as i32 ;
7
+ }
8
+ res
9
+ }
10
+ }
You can’t perform that action at this time.
0 commit comments