Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9c32c3c

Browse files
committed
feat: add solutions to lcp problem: No.06
1 parent 77246c4 commit 9c32c3c

File tree

7 files changed

+93
-29
lines changed

7 files changed

+93
-29
lines changed

‎lcp/LCP 06. 拿硬币/README.md‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,50 @@
4242
<!-- 这里可写当前语言的特殊实现逻辑 -->
4343

4444
```python
45-
45+
class Solution:
46+
def minCount(self, coins: List[int]) -> int:
47+
return sum((coin + 1) // 2 for coin in coins)
4648
```
4749

4850
### **Java**
4951

5052
<!-- 这里可写当前语言的特殊实现逻辑 -->
5153

5254
```java
55+
class Solution {
56+
public int minCount(int[] coins) {
57+
int ans = 0;
58+
for (int coin : coins) {
59+
ans += (coin + 1) / 2;
60+
}
61+
return ans;
62+
}
63+
}
64+
```
65+
66+
### **C++**
67+
68+
```cpp
69+
class Solution {
70+
public:
71+
int minCount(vector<int>& coins) {
72+
int ans = 0;
73+
for (int coin : coins) ans += (coin + 1) / 2;
74+
return ans;
75+
}
76+
};
77+
```
78+
79+
### **Go**
5380
81+
```go
82+
func minCount(coins []int) int {
83+
ans := 0
84+
for _, coin := range coins {
85+
ans += (coin + 1) / 2
86+
}
87+
return ans
88+
}
5489
```
5590

5691
### **...**

‎lcp/LCP 06. 拿硬币/Solution.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution {
2+
public:
3+
int minCount(vector<int>& coins) {
4+
int ans = 0;
5+
for (int coin : coins) ans += (coin + 1) / 2;
6+
return ans;
7+
}
8+
};

‎lcp/LCP 06. 拿硬币/Solution.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
func minCount(coins []int) int {
2+
ans := 0
3+
for _, coin := range coins {
4+
ans += (coin + 1) / 2
5+
}
6+
return ans
7+
}

‎lcp/LCP 06. 拿硬币/Solution.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution {
2+
public int minCount(int[] coins) {
3+
int ans = 0;
4+
for (int coin : coins) {
5+
ans += (coin + 1) / 2;
6+
}
7+
return ans;
8+
}
9+
}

‎lcp/LCP 06. 拿硬币/Solution.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def minCount(self, coins: List[int]) -> int:
3+
return sum((coin + 1) // 2 for coin in coins)

‎solution/0600-0699/0645.Set Mismatch/README.md‎

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ class Solution {
8585
int eor = 0;
8686
for (int i = 1; i <= nums.length; ++i) {
8787
eor ^= (i ^ nums[i - 1]);
88-
<<<<<<< Updated upstream
8988
}
9089
int diff = eor & (~eor + 1);
9190
int a = 0;
@@ -158,27 +157,14 @@ public:
158157
if ((nums[i - 1] & diff) == 0) {
159158
a ^= nums[i - 1];
160159
}
161-
=======
162-
}
163-
int diff = eor & (~eor + 1);
164-
int a = 0;
165-
for (int i = 1; i <= nums.length; ++i) {
166-
if ((nums[i - 1] & diff) == 0) {
167-
a ^= nums[i - 1];
168-
}
169-
>>>>>>> Stashed changes
170160
if ((i & diff) == 0) {
171161
a ^= i;
172162
}
173163
}
174164
int b = eor ^ a;
175165
for (int num : nums) {
176166
if (a == num) {
177-
<<<<<<< Updated upstream
178167
return {a, b};
179-
=======
180-
return new int[]{a, b};
181-
>>>>>>> Stashed changes
182168
}
183169
}
184170
return {b, a};

‎solution/0600-0699/0645.Set Mismatch/README_EN.md‎

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ class Solution {
6161
int eor = 0;
6262
for (int i = 1; i <= nums.length; ++i) {
6363
eor ^= (i ^ nums[i - 1]);
64-
<<<<<<< Updated upstream
6564
}
6665
int diff = eor & (~eor + 1);
6766
int a = 0;
@@ -134,27 +133,14 @@ public:
134133
if ((nums[i - 1] & diff) == 0) {
135134
a ^= nums[i - 1];
136135
}
137-
=======
138-
}
139-
int diff = eor & (~eor + 1);
140-
int a = 0;
141-
for (int i = 1; i <= nums.length; ++i) {
142-
if ((nums[i - 1] & diff) == 0) {
143-
a ^= nums[i - 1];
144-
}
145-
>>>>>>> Stashed changes
146136
if ((i & diff) == 0) {
147137
a ^= i;
148138
}
149139
}
150140
int b = eor ^ a;
151141
for (int num : nums) {
152142
if (a == num) {
153-
<<<<<<< Updated upstream
154143
return {a, b};
155-
=======
156-
return new int[]{a, b};
157-
>>>>>>> Stashed changes
158144
}
159145
}
160146
return {b, a};
@@ -164,6 +150,8 @@ public:
164150
165151
### **Go**
166152
153+
把每个数都放到它应该在的位置,最后出现"异常"的就是重复的数和丢失的数。
154+
167155
```go
168156
func findErrorNums(nums []int) []int {
169157
n := len(nums)
@@ -181,6 +169,34 @@ func findErrorNums(nums []int) []int {
181169
}
182170
```
183171

172+
也可以使用位运算。
173+
174+
```go
175+
func findErrorNums(nums []int) []int {
176+
eor, n := 0, len(nums)
177+
for i := 1; i <= n; i++ {
178+
eor ^= (i ^ nums[i-1])
179+
}
180+
diff := eor & (-eor)
181+
a := 0
182+
for i := 1; i <= n; i++ {
183+
if (nums[i-1] & diff) == 0 {
184+
a ^= nums[i-1]
185+
}
186+
if (i & diff) == 0 {
187+
a ^= i
188+
}
189+
}
190+
b := eor ^ a
191+
for _, num := range nums {
192+
if a == num {
193+
return []int{a, b}
194+
}
195+
}
196+
return []int{b, a}
197+
}
198+
```
199+
184200
### **C++**
185201

186202
```cpp

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /