其他分享
首页 > 其他分享> > 不积跬步无以至千里(三)

不积跬步无以至千里(三)

作者:互联网

【58左旋转字符串】
[链接]https://leetcode-cn.com/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof/
char* reverseLeftWords(char* s, int n){
    int i = 0;
    if(s[0]=='\0'|| n<=0) return s;
    i = strlen(s); //统计s长度
    char *s1 =malloc(sizeof(char)*(i+1));
    memmove(s1, s+(n%i), (i-n%i));
    memmove(s1+(i-n%i), s,n%i);
    s1[i]='\0';
    return s1;
}

【剑指 Offer 58 - I. 翻转单词顺序】
[链接]https://leetcode-cn.com/problems/fan-zhuan-dan-ci-shun-xu-lcof/
/*
思路:用strtok分解字符串,然后将每个字符串进行倒叙记录到对应的数组位置,然后,再整体对换。
*/
char* reverseWords(char* s){
    int len = strlen(s);
    
    char** ret = (char*)calloc(10000, sizeof(char));
    
    char* token = NULL;
    //C 库函数 char *strtok(char *str, const char *delim) 分解字符串 str 为一组字符串,delim 为分隔符。
    //该函数返回被分解的第一个子字符串,如果没有可检索的字符串,则返回一个空指针。
    token = strtok(s, " ");
    if (token == NULL) {
        return "";
    }
    int count = 0;
    while (token) {
        //将每个字符串 存放到每个数组中
        ret[count] = (char*)calloc(strlen(token) + 1, sizeof(char));
        strcpy(ret[count], token);
        count++;
        //继续获取其他的子字符串
        token = strtok(NULL, " ");
        printf("----%s", token);
    }
    //void *calloc(size_t nitems, size_t size) nitems -- 要被分配的元素个数。size -- 元素的大小。
    char* ans = (char*)calloc(len + 1, sizeof(char));
    for (int i = count - 1; i >= 0; i--) {
        //strcat(char *dest, const char *src) 把 src 所指向的字符串追加到 dest 所指向的字符串的结尾
        strcat(ans, ret[i]);
        if (i != 0) {
            strcat(ans, " ");
        }
    }
    
    return ans;
}


char* reverseWords(char* s){
    int len=strlen(s);
    char *res=(char*)malloc(sizeof(char)*(len+1));
    char *ch=" ";   //分隔符为' '(空格))
    char *token;
    int i=0,k=0,j=0;
    token=strtok(s,ch);
    if(!token) return "";   //排除""和" "字符串
    while(token){
        int temp=strlen(token)-1;
            for(;temp>=0;temp--){
                res[k++]=token[temp];   //将the->eht放入结果字符串
            }
            res[k++]=' ';
        token=strtok(NULL,ch);
    }
    res[--k]='\0';
    for(i=0,j=k-1;i<j;i++,j--){     //将整个结果字符串逆置
        char t=res[i];
        res[i]=res[j];
        res[j]=t;
    }
    return res;
}

//通常做法  字符串反转 思路:从后向前遍历
char* reverseWords(char* s){
    int len = strlen(s);
    if(len == 0) return s;
    int num = 0;
    int g = 0;
    while(s[g] == ' '){
        g++;
    }
    if(g == len) return "";
    int i = len-1, j = len-1;
    char* res = (char*)malloc(sizeof(char) * (len+1));
    while(i != g-1){
        //从后往前找到第一个不是空格的位置
        while(i >= 0 && s[i] == ' '){
            i--;
        }
        int j = i; //从当前非空字符位置开始
        while(j >= 0 && s[j] != ' '){
            j--;  //找到第一个是空格的位置
        }
        for(int k = j+1; k <= i; k++){
            res[num++] = s[k];  //将找到的单词存放到结果数组里面
        }
        res[num++] = ' ';
        i = j;
    }
    res[num-1] = '\0';
    return res;
}



【翻转字符串】
void reverseStr(char *s)
{
    int i;
    int len = strlen(s);
    char ch;

    for (i = 0; i < len / 2; i++) {
        ch = s[i];
        s[i] = s[len - 1 - i];
        s[len - 1 - i] = ch;
    }
}


【1573. 分割字符串的方案数】
[链接]https://leetcode-cn.com/problems/number-of-ways-to-split-a-string/
int numWays(char * s){
        int count = 0;
        int res = 0;
        int len = strlen(s);
        for (int i = 0; i < len; i++) {
            if (s[i] == '1') count++;
        }


        if (count % 3 != 0) return 0;
        count /= 3;
        
        //cout << count << endl;
        int left_1 = 0; 
        int right_1 = len - 1;
        int left_2 = 0;
        int right_2 = len - 1;

        int left_cnt = 0, right_cnt = 0;
        if (count > 0) {
            while (left_cnt < count) {
                if (s[left_1] == '1') left_cnt++;
                left_1++;
            }

            left_2 = left_1;
            while (left_cnt <= count) {
                if (s[left_2] == '1') left_cnt++;
                left_2++;
            }

            while (right_cnt < count) {
                if (s[right_1] == '1') right_cnt++;
                right_1--;
            }

            right_2 = right_1;
            while (right_cnt <= count) {
                if (s[right_2] == '1') right_cnt++;
                right_2--;
            }

            res = (long long)(left_2 - left_1) * (right_1 - right_2) % 1000000007;

        }else {
            res = ((long long)(len - 1) * (len - 2) / 2) % 1000000007;
        }
        return res;
}

【787. K 站中转内最便宜的航班】
[链接]https://leetcode-cn.com/problems/cheapest-flights-within-k-stops/

int g_city[100][100];
int g_book[100];  //标记是否访问过标记 0 没有, 1访问过
int g_min_price;
void DFS(int n, int cur, int dst, int K, int dis, int step, int g_city[100][100], int g_book[100]){
    //剪枝 如果当前距离比之前的最小值还要大,或者当前的步数比最多中转K还要大
    if(dis > g_min_price || step > K){
        return;
    }
    if(cur == dst){
        if(dis < g_min_price){
            g_min_price = dis;
        }
        return;
    }

    for(int i = 0; i < n; i++){
        if(g_city[cur][i] != 0 && g_book[i] == 0){
            g_book[i] = 1;
            DFS(n, i, dst, K, dis + g_city[cur][i], step+1, g_city, g_book);
            g_book[i] = 0;
        }
    }
    return;
}
int findCheapestPrice(int n, int **flights, int flightsSize, int *flightsColSize, int src, int dst, int K)
{
    int i;
    g_min_price = INT_MAX;
    memset(g_city, 0, 100 * 100 * sizeof(int));
    memset(g_book, 0, 100 * sizeof(int));

    for(i = 0; i < flightsSize; i++){
        //当前所给数组的两个城市的价格
        g_city[flights[i][0]][flights[i][1]] = flights[i][2];
    }

    //从起点开始搜索
    g_book[src] = 1;
    DFS(n, src, dst, K, 0, -1, g_city, g_book);
    return (g_min_price == INT_MAX) ? -1 : g_min_price;

}

 

标签:city,int,无以至千里,char,token,book,不积跬步,left
来源: https://www.cnblogs.com/maleyang/p/13648520.html