个人学习编程(3-23) leetcode刷题
字符串的移动:
您将得到一个包含小写英文字母的字符串s和一个矩阵shift,其中
shift[i] = [direction, amount]:
direction可以为0(左移)或1(右移)。
amount是字符串s被移动的量。
左移1表示删除s的第一个字符并将其附加到末尾。
类似地,向右移动1意味着删除最后一个字符并将其添加到开头。
两层for循环:
char * stringShift(char * s,int** shift,int shiftSize,int* shiftColSize){
for (int i = 0; i < shiftSize; i++){
int len = strlen(s);
int direction = shift[i][0];//二维数组中的元素的值第一位是方向
int amount = shift[i][1];//第二位是代表次数
if (direction == 0){ //左移
for (int j = 0; j < amount; j++){
int temp = s[0];
for (int k = 0; k < len -1; k++){
s[k] = s[k + 1];
}
s[len - 1] = temp;
}
}
else{
for (int r = 0; r < amount; r++){
int temp = s[len - 1];
for (int c = len - 1; c > 0; c--){
s[c] = s[c - 1];
}
s[0] = temp;
}
}
}
return s;
}
左移1 == 右移len - 1:
char * stringShift(char * s,int** shift,int shiftSize,int* shiftColSize){
int len = strlen(s);
int direction = shift[i][0];
int amount = shift[i][1];
amount = amount % len;
if (direction == 0){
amount = amount;
}else{
amount = len - amount;
for (int k = 1; k < amount; k++){
char temp = s[0];
for (int j = 0; j < len - 1; j++){
s[j] = s[j + 1];
}
s[len - 1] = temp;
}
}
return s;
}
Finally (写翻转函数):
void reverse(char* s,int start,int end){
end--;
char temp;
while (start < end){
temp = s[start];
s[start] = s[end];
s[end] = temp;
start++;
end--;
}
}
char* stringShift(char * s,int** shift,int shiftSize,int* shiftColSize){
int len = strlen(s);
int totalAmount = 0;
for (int i = 0; i < shiftSize; i++){
int direction = shift[i][0];
int amount = shift[i][1];
amount = amount % len;
if (direction == 1){
amount = len - amount;
}
totalAmount = totalAmount + amount;
}
totalAmount %= len;
reverse(s, 0, totalAmount);
reverse(s,totalAmount,len);
reverse(s,0,len);
return s;
}
数组除了自己的乘积:
输入:
Copy Code
4 1 2 3 4
输出:
Copy Code
24 12 8 6
遍历:
这个方法很好,就是算出来全部的乘积,然后除以每一个重新赋值到这个位置就可以得到任务需求
#include <stdio.h>
long long multiply(int* arr, int n) {
long long total_product = 1;
for (int i = 0; i < n; i++) {
total_product *= arr[i];
}
return total_product;
}
int main() {
int n;
scanf("%d", &n);
int arr[n];
// 输入数组
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// 计算整个数组的乘积
long long total_product = multiply(arr, n);
// 输出每个位置的乘积(总乘积除以当前元素)
for (int i = 0; i < n; i++) {
arr[i] = total_product / arr[i];
printf("%lld ", arr[i]); // 输出 long long 类型
}
printf("\n"); // 输出完毕后换行
return 0;
}
按照0的数量:
#include <bits/stdc++.h>
int* productExceptSelf(int* nums,int numsSize,int* returnSize){
*returnSize = numsSize;
int* result = (int *)malloc(sizeof(int)*numsSize);
int numberOfZeros = 0;
for (int i = 0; i < numsSize; i++){
if (nums[i] == 0){
numberOfZeros++;
}
}
if (numberOfZeros == 1){
int total = 1;
for (int i = 0; i < numsSize; i++){
if (nums[i] != 0){
total *= nums[i];
}
}
for (int i = 0; i < numsSize; i++){
if (nums[i] != 0){
result[i] = 0;
} else{
result[i] = total;
}
}
}
else if (numberOfZeros >= 2){
for (int i = 0; i < numsSize; i++){
result[i] = 0;
}
}
else{
int total = 1;
for (int k = 0; k < numsSize; k++){
total *=nums[k];
}
for (int i = 0; i < numsSize; i++){
result[i] = total / nums[i];
}
}
return result;
}
待理解的最优解:
#include <bits/stdc++.h>
int* productExceptSelf(int* nums,int numsSize,int* returnSize){
*returnSize = numsSize;
int* result = (int*)malloc(sizeof(int)*numsSize);
int rights[numsSize];
rights[numsSize - 1] = 1;
for(int i = numsSize - 2;i >= 0;i--){
rights[i] = rights[i + 1] * nums[i + 1];
}
int left = 1;
for (int i = 0; i < numsSize; i++){
result[i] = left * rights[i];
left = left * nums[i];
}
return result;
}
X进制转为Y进制:(10进制作为中转站)
#include <stdio.h>
#include <bits/stdc++.h>
#include <stdlib.h>
int main() {
char s1[105];
int len,x1,x2;
int count = 0;
char s2[105];
int i;
int ans = 0;
printf("请输入进制字符串以及要转的进制");
scanf("%s %d %d",&s1,&x1,&x2);
len = strlen(s1);
for (i = 0; i < len; i++){
ans = ans * x1;
if(s1[i] >= '0' && s1[i] <= '9'){
ans += s1[i] -'0';
}else{
ans += s1[i] - 'A' + 10;
}
}
printf("这个数转为10进制是:%d\n",ans);
while (ans > 0){
int w = ans % x2;
if(w < 10){
s2[count++] = w + '0';
}else{
s2[count++] = (w-10) + 'a';
}
ans/=x2;
}
for (int i = count - 1; i >= 0; i--){
printf("%c",s2[i]);
}
return 0;
}