-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
feat: add solutions to lc problems: No.2859~2861 #1644
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
solution/2800-2899/2859.Sum of Values at Indices With K Set Bits/Solution.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class Solution { | ||
public: | ||
int sumIndicesWithKSetBits(vector<int>& nums, int k) { | ||
int ans = 0; | ||
for (int i = 0; i < nums.size(); ++i) { | ||
if (__builtin_popcount(i) == k) { | ||
ans += nums[i]; | ||
} | ||
} | ||
return ans; | ||
} | ||
}; |
8 changes: 8 additions & 0 deletions
solution/2800-2899/2859.Sum of Values at Indices With K Set Bits/Solution.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
func sumIndicesWithKSetBits(nums []int, k int) (ans int) { | ||
for i, x := range nums { | ||
if bits.OnesCount(uint(i)) == k { | ||
ans += x | ||
} | ||
} | ||
return | ||
} |
3 changes: 3 additions & 0 deletions
solution/2800-2899/2859.Sum of Values at Indices With K Set Bits/Solution.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class Solution: | ||
def sumIndicesWithKSetBits(self, nums: List[int], k: int) -> int: | ||
return sum(x for i, x in enumerate(nums) if i.bit_count() == k) |
18 changes: 18 additions & 0 deletions
solution/2800-2899/2859.Sum of Values at Indices With K Set Bits/Solution.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
function sumIndicesWithKSetBits(nums: number[], k: number): number { | ||
let ans = 0; | ||
for (let i = 0; i < nums.length; ++i) { | ||
if (bitCount(i) === k) { | ||
ans += nums[i]; | ||
} | ||
} | ||
return ans; | ||
} | ||
|
||
function bitCount(n: number): number { | ||
let count = 0; | ||
while (n) { | ||
n &= n - 1; | ||
count++; | ||
} | ||
return count; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
solution/2800-2899/2860.Happy Students/Solution.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class Solution { | ||
public: | ||
int countWays(vector<int>& nums) { | ||
sort(nums.begin(), nums.end()); | ||
int ans = 0; | ||
int n = nums.size(); | ||
for (int i = 0; i <= n; ++i) { | ||
if ((i && nums[i - 1] >= i) || (i < n && nums[i] <= i)) { | ||
continue; | ||
} | ||
++ans; | ||
} | ||
return ans; | ||
} | ||
}; |
11 changes: 11 additions & 0 deletions
solution/2800-2899/2860.Happy Students/Solution.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
func countWays(nums []int) (ans int) { | ||
sort.Ints(nums) | ||
n := len(nums) | ||
for i := 0; i <= n; i++ { | ||
if (i > 0 && nums[i-1] >= i) || (i < n && nums[i] <= i) { | ||
continue | ||
} | ||
ans++ | ||
} | ||
return | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.