mirror of
https://github.com/lWolvesl/leetcode.git
synced 2026-06-04 02:21:15 +08:00
Merge branch 'main' of https://github.com/lWolvesl/leetcode
This commit is contained in:
@@ -0,0 +1,15 @@
|
|||||||
|
#include <vector>
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int largestMagicSquare(std::vector<std::vector<int>>& grid) {
|
||||||
|
int m = grid.size();
|
||||||
|
int n = grid[0].size();
|
||||||
|
std::vector<std::vector<int>> preM(m,std::vector<int>(n,0)),preN(m,std::vector<int>(n,0));
|
||||||
|
for (int i = 0; i < m; i++) {
|
||||||
|
for(int j = 0;j < n;j++){
|
||||||
|
preM[m][n] += grid[m][n];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
#include <vector>
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
std::vector<int> minBitwiseArray(std::vector<int>& nums) {
|
||||||
|
std::vector<int> ans;
|
||||||
|
for(auto num:nums){
|
||||||
|
if (num == 2) {
|
||||||
|
ans.push_back(-1);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
int temp = num;
|
||||||
|
int t=0;
|
||||||
|
while (temp & 1) {
|
||||||
|
t++;
|
||||||
|
temp >>= 1;
|
||||||
|
}
|
||||||
|
ans.push_back(num ^ (1 << (t-1)));
|
||||||
|
}
|
||||||
|
return ans;
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
#include <climits>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
bool isIncrease(std::vector<int>& nums){
|
||||||
|
for (int i = 1; i<nums.size(); ++i) {
|
||||||
|
if (nums[i] < nums[i-1]) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
int minimumPairRemoval(std::vector<int>& nums) {
|
||||||
|
int ans = 0;
|
||||||
|
while (!isIncrease(nums)) {
|
||||||
|
int index = 0;
|
||||||
|
int minX = INT_MAX;
|
||||||
|
for (int i = 0; i< nums.size()-1; i++) {
|
||||||
|
int temp = nums[i]+nums[i+1];
|
||||||
|
if (minX > temp) {
|
||||||
|
minX = temp;
|
||||||
|
index = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
nums.erase(nums.begin()+index+1);
|
||||||
|
nums[index] = minX;
|
||||||
|
ans++;
|
||||||
|
}
|
||||||
|
return ans;
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
#include <string>
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int vowelConsonantScore(std::string s) {
|
||||||
|
int v =0,c=0;
|
||||||
|
std::string col = "aeiou";
|
||||||
|
for (auto i : s) {
|
||||||
|
if (i > 122 || i < 97) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (col.find(i) != std::string::npos) {
|
||||||
|
v++;
|
||||||
|
}else {
|
||||||
|
c++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (c==0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return v/c;
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package A
|
||||||
|
|
||||||
|
import "sort"
|
||||||
|
|
||||||
|
func minimumAbsDifference(arr []int) [][]int {
|
||||||
|
sort.Slice(arr, func(i, j int) bool {
|
||||||
|
return arr[i] < arr[j]
|
||||||
|
})
|
||||||
|
ans := [][]int{}
|
||||||
|
n := len(arr)
|
||||||
|
minDiff := arr[1] - arr[0]
|
||||||
|
for i := 1; i < n; i++ {
|
||||||
|
temp := arr[i] - arr[i-1]
|
||||||
|
if minDiff > temp {
|
||||||
|
minDiff = temp
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for i := 1; i < n; i++ {
|
||||||
|
temp := arr[i] - arr[i-1]
|
||||||
|
if temp == minDiff {
|
||||||
|
ans = append(ans, []int{arr[i-1], arr[i]})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ans
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package A
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sort"
|
||||||
|
)
|
||||||
|
|
||||||
|
func minimumDifference(nums []int, k int) int {
|
||||||
|
if k == 1 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
n := len(nums)
|
||||||
|
sort.Ints(nums)
|
||||||
|
ans := nums[n-1] - nums[0]
|
||||||
|
for i := 0; i+k-1 < n; i++ {
|
||||||
|
temp := nums[i+k-1] - nums[i]
|
||||||
|
if ans > temp {
|
||||||
|
ans = temp
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ans
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package A
|
||||||
|
|
||||||
|
func minBitwiseArray(nums []int) []int {
|
||||||
|
ans := make([]int, 0, len(nums))
|
||||||
|
for _, num := range nums {
|
||||||
|
if num == 2 {
|
||||||
|
ans = append(ans, -1)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
temp := num
|
||||||
|
t := 0
|
||||||
|
for temp&1 == 1 {
|
||||||
|
t++
|
||||||
|
temp >>= 1
|
||||||
|
}
|
||||||
|
ans = append(ans, num^(1<<(t-1)))
|
||||||
|
}
|
||||||
|
return ans
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package A
|
||||||
|
|
||||||
|
import "math"
|
||||||
|
|
||||||
|
func isIncrease(nums []int) bool {
|
||||||
|
for i := range nums {
|
||||||
|
if i == len(nums)-1 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if nums[i] > nums[i+1] {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func minimumPairRemoval(nums []int) int {
|
||||||
|
ans := 0
|
||||||
|
index := 0
|
||||||
|
temp := 0
|
||||||
|
for !isIncrease(nums) {
|
||||||
|
minX := math.MaxInt
|
||||||
|
for i := range nums {
|
||||||
|
if i == len(nums)-1 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
temp = nums[i] + nums[i+1]
|
||||||
|
if minX > temp {
|
||||||
|
minX = temp
|
||||||
|
index = i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
nums[index] = minX
|
||||||
|
nums = append(nums[:index+1], nums[index+2:]...)
|
||||||
|
ans++
|
||||||
|
}
|
||||||
|
return ans
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package A
|
||||||
|
|
||||||
|
func minCost(n int, edges [][]int) int {
|
||||||
|
edgeM := make([][]int, n)
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
edgeM[i] = make([]int, n)
|
||||||
|
}
|
||||||
|
for _, e := range edges {
|
||||||
|
edgeM[e[0]][e[1]] = e[2]
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package A
|
||||||
|
|
||||||
|
import "math/bits"
|
||||||
|
|
||||||
|
func concatenatedBinary(n int) int {
|
||||||
|
var res int64 = 0
|
||||||
|
const mod = 1e9 + 7
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
b := bits.Len32(uint32(i + 1))
|
||||||
|
res = ((res << b) + int64(i+1)) % mod
|
||||||
|
}
|
||||||
|
return int(res)
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package A
|
||||||
|
|
||||||
|
import "math/bits"
|
||||||
|
|
||||||
|
func reverseBits(n int) int {
|
||||||
|
res := 0
|
||||||
|
for i := 0; i < 32; i++ {
|
||||||
|
res = (res << 1) | (n & 1)
|
||||||
|
n >>= 1
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
func reverseBits2(n int) int {
|
||||||
|
return int(bits.Reverse32((uint32(n))))
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package A
|
||||||
|
|
||||||
|
func constructTransformedArray(nums []int) []int {
|
||||||
|
n := len(nums)
|
||||||
|
res := make([]int, n)
|
||||||
|
for i, v := range nums {
|
||||||
|
if v > 0 {
|
||||||
|
res[i] = nums[(i+v)%n]
|
||||||
|
}
|
||||||
|
if v == 0 {
|
||||||
|
res[i] = v
|
||||||
|
}
|
||||||
|
if v < 0 {
|
||||||
|
res[i] = nums[((i+v)%n+n)%n]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package A
|
||||||
|
|
||||||
|
func hasAlternatingBits(n int) bool {
|
||||||
|
n = n ^ (n >> 1)
|
||||||
|
return n&(n+1) == 0
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user