leetcode68. 文本左右对齐

骑猪看日落 2022-09-11 04:23 293阅读 0赞

在这里插入图片描述
思路

  1. 按照题目的意思来
  2. 计算长度时,每一行的字符长度之和不能超过最大值,两个单词之间至少有一个空格,分为中间单词和最后一个单词。
  3. 填充时,分为最后一排左对齐和非最后一排散列对齐
  4. 其中非最后一排还分为一个单词和多个单词处理的过程,一个单词,直接填充就完事了
  5. 最后一排的最后一个单词后面需要将多的位置用空格补齐
    代码

    include

    include

    include

    char fullJustify(char words, int wordsSize, int maxWidth, int* returnSize){

    1. int i=0;
    2. char **res = (char**)malloc(sizeof(char*)*wordsSize);
    3. int count = 0;
    4. * returnSize=0;
    5. int starti=0,endi=0;
    6. while(i<wordsSize) {
    7. while(i<wordsSize&&(count+strlen(words[i])+1<=maxWidth||count+strlen(words[i])==maxWidth)) {
    8. if(count==0)
    9. starti = i;
    10. if(count+strlen(words[i])+1<=maxWidth)//中间单词
    11. count = count + strlen(words[i])+1;
    12. else if(count+strlen(words[i])==maxWidth)//最后一个单词
    13. count=count+strlen(words[i]);
    14. i++;//注意弹出while循环的i是破坏表达式的i,使用时需要i-1;
    15. }
    16. {
    17. if(i!=wordsSize){ //非最后一行
    18. char *re = malloc(sizeof(char)*(maxWidth*2));
    19. re[0] = '\0';
    20. endi = i-1;
    21. count = 0;
    22. int wordcount = endi -starti +1;
    23. int blockcount = wordcount - 1;
    24. if(blockcount==0){ //一个单词左对齐
    25. strcat(re,words[starti]);
    26. for(int j = 0;j<maxWidth - strlen(words[starti]);j++) {
    27. strcat(re," ");
    28. }
    29. }
    30. else { //多个单词
    31. int allcount = 0;
    32. for(int j = starti;j<=endi;j++){
    33. allcount = allcount+strlen(words[j]);
    34. }
    35. int block = (maxWidth-allcount)/blockcount;//平均空格数
    36. int outblock = (maxWidth-allcount)%blockcount;//多出来的空格数
    37. for(int j = starti;j<=endi;j++){
    38. if(j!=endi){
    39. //printf("words[j] = %s\n",words[j]);
    40. strcat(re,words[j]);
    41. for(int k = 0;k<block;k++){
    42. strcat(re," ");
    43. }
    44. if(outblock!=0){
    45. strcat(re," ");
    46. outblock--;
    47. }
    48. }
    49. else {
    50. strcat(re,words[j]);
    51. }
    52. }
    53. }
    54. res[(*returnSize)] = re;
    55. }
    56. else { //最后一排
    57. char *re = malloc(sizeof(char)*(maxWidth*2));
    58. re[0] = '\0';
    59. endi = i-1;
    60. int outblock = maxWidth;
    61. for(int j = starti;j<=endi;j++){
    62. if(j!=endi){ //中间左对齐
    63. strcat(re,words[j]);
    64. strcat(re," ");
    65. outblock = outblock -strlen(words[j]) -1;
    66. }
    67. else {
    68. strcat(re,words[j]);
    69. outblock = outblock -strlen(words[j]);
    70. for(int k = 0;k<outblock;k++){ //如果空位没用完还是需要补齐
    71. strcat(re," ");
    72. }
    73. }
    74. }
    75. res[(*returnSize)] = re;
    76. }
    77. (*returnSize)++;
    78. count = 0;
    79. }
    80. }
    81. return res;

    }
    // void test(char **word) {
    // printf(“%s\n”,word[0]);
    // printf(“%s\n”,word[1]);
    // }

    int main() {

    1. char *word[7]={ "This", "is", "an", "example", "of", "text", "justification."};
    2. int maxwidth = 16;
    3. int wordssize = 7;
    4. int returnsize = 0;
    5. char **re = fullJustify(word,wordssize,maxwidth,&returnsize);
    6. printf("returnsize= %d\n",returnsize);
    7. for(int i =0;i<returnsize;i++){
    8. printf("%s\n",re[i]);
    9. }
    10. printf("%d\n",strlen(re[returnsize-1]));
    11. // char words[15] = "this";
    12. // strcpy(words,' ');
    13. // strcpy(words,"test");
    14. // printf("%s\n",words);

    }

发表评论

表情:
评论列表 (有 0 条评论,293人围观)

还没有评论,来说两句吧...

相关阅读

    相关 字体左右对齐

    在遇到表单格式的页面的时候,常常都需要将 左边的标题全部进行左右对齐 overflow: hidden; text-align: justify; t