1. 程式人生 > 實用技巧 >不積跬步無以至千里(三)

不積跬步無以至千里(三)

【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; }