• 算法进修Day-34


    算法进修Day-34

    二进制求和

    难度:简单
    题目要求:
    给你两个二进制字符串 ab ,以二进制字符串的形式返回它们的和。

    示例1

    输入:a = “11”, b = “1”
    输出:“100”

    示例2

    输入:a = “1010”, b = “1011”
    输出:“10101”

    题解

    i n d e x 1 index_1 index1 i n d e x 2 index_2 index2 分别表示二进制整数 a a a b b b 遍历到的下标,用 c a r r y carry carry 表示进位值,初始时 i n d e x 1 index_1 index1 i n d e x 2 index_2 index2 分别指向整数 a a a b b b 的最大下标, c a r r y = 0 carry=0 carry=0。使用字符串保存两个二进制整数之和的每一位,计算过程中依次将每一位结果拼接到字符串的末尾

    i n d e x 1 ≥ 0 , i n d e x 2 ≥ 0 , c a r r y ≠ 0 index_1\geq 0,index_2\geq 0, carry\neq 0 index10,index20,carry=0 三个条件中至少有一个成立,对于每一位,执行如下操作

    • d i g i t 1 digit_1 digit1 d i g i t 2 digit_2 digit2 分别为 a [ i n d e x 1 ] a[index_1] a[index1] b [ i n d e x 2 ] b[index_2] b[index2]对应的数字,如果 i n d e x 1 < 0 index_1<0 index1<0,则 d i g i t 1 = 0 digit_1=0 digit1=0,如果 i n d e x 2 < 0 index_2<0 index2<0 d i g i t 2 = 0 digit_2=0 digit2=0
    • 计算 s u m = d i g i t 1 + d i g i t 2 + c a r r y sum=digit_1+digit_2+carry sum=digit1+digit2+carry,则两个二进制整数之和的当前位置的值为 s u m % 2 sum \%2 sum%2,进位值 s u m 2 \frac{sum}{2} 2sum,将 s u m % 2 sum\%2 sum%2 拼接到字符串末尾,将 c a r r y carry carry 更新为 s u m 2 \frac{sum}{2} 2sum
    • i n d e x 1 index_1 index1 i n d e x 2 index_2 index2 分别向左移动一位

    操作结束之后,将字符串翻转之后得到两个二进制整数之和

    想法代码

    using System.Text;
    
    public class Solution
    {
        public static void Main(string[] args)
        {
            Solution solution = new Solution();
            string a = "11";
            string b = "1";
            string res = solution.AddBinary(a, b);
            Console.WriteLine(res);
        }
    
        public string AddBinary(string a, string b)
        {
            StringBuilder sb = new StringBuilder();
            int index1 = a.Length - 1, index2 = b.Length - 1;
            int carry = 0;
            while (index1 >= 0 || index2 >= 0 || carry != 0)
            {
                int digit1 = index1 >= 0 ? a[index1] - '0' : 0;
                int digit2 = index2 >= 0 ? b[index2] - '0' : 0;
                int sum = digit1 + digit2 + carry;
                sb.Append(sum % 2);
                carry = sum / 2;
                index1--;
                index2--;
            }
            StringBuilder sb2 = new StringBuilder();
            for (int i = sb.Length - 1; i >= 0; i--)
            {
                sb2.Append(sb[i]);
            }
            return sb2.ToString();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    68.文本左右对齐

    难度:困难
    题目要求:
    给定一个单词数组 words 和一个长度 maxWidth ,重新排版单词,使其成为每行恰好有 maxWidth 个字符,且左右两端对齐的文本。

    你应该使用 “贪心算法” 来放置给定的单词;也就是说,尽可能多地往每行中放置单词。必要时可用空格 ' ' 填充,使得每行恰好有 maxWidth 个字符。

    要求尽可能均匀分配单词间的空格数量。如果某一行单词间的空格不能均匀分配,则左侧放置的空格数要多于右侧的空格数。

    文本的最后一行应为左对齐,且单词之间不插入额外的空格。

    示例1

    输入:words = [“This”, “is”, “an”, “example”, “of”, “text”, “justification.”], maxWidth = 16
    输出:
    [
    “This is an”,
    “example of text”,
    "justification. "
    ]

    示例2

    输入:words = [“What”,“must”,“be”,“acknowledgment”,“shall”,“be”], maxWidth = 16
    输出:
    [
    “What must be”,
    "acknowledgment ",
    "shall be "
    ]

    示例3

    输入:words = [“Science”,“is”,“what”,“we”,“understand”,“well”,“enough”,“to”,“explain”,“to”,“a”,“computer.”,“Art”,“is”,“everything”,“else”,“we”,“do”],maxWidth = 20
    输出:
    [
    “Science is what we”,
    “understand well”,
    “enough to explain to”,
    “a computer. Art is”,
    “everything else we”,
    "do "
    ]

    题解

    遍历单词数组,对于每个单词 w o r d word word,尝试将 w o r d word word 拼接到当前行的末尾,如果当前行已经有单词则需要添加一个空格将单词分隔,考虑将 w o r d word word 拼接到当前行的末尾之后,当前行的宽度。

    如果当前行的宽度不超过 m a x W i d t h maxWidth maxWidth,则将 w o r d word word拼接到当前行的末尾。

    如果当前行的宽度超过 m a x W i d t h maxWidth maxWidth,则不能将 w o r d word word 拼接到当前行的末尾,当前行的单词添加完毕。此时需要将当前行的单词重新排版使得单词之间的空格均匀分配,将重新排版之后的当前行添加到结果中,然后将 w o r d word word 填入新的一行。重新排版当前行的做法如下。

    如果当前行只有一个单词,则将该单词左对齐,然后在右侧添加空格直到当前行有 m a x W i d t h maxWidth maxWidth 个字符。

    如果当前行有至少两个单词,则根据单词数量计算间隔数量,根据单词长度之和计算空格数量,将空格均匀分配在每个间隔中,满足左侧间隔的空格数大于等于右侧间隔的空格数。

    遍历单词数组结束之后,需要将剩余的单词填入末尾行。由于末尾行左对齐且相邻单词之间只有一个空格,因此将末尾行的单词使用一个空格分隔的方式依次拼接,然后在右侧添加空格直到末尾行有 m a x W i d t h maxWidth maxWidth 个字符。

    想法代码

    using System.Text;
    
    public class Solution
    {
        public static void Main(string[] args)
        {
            Solution solution = new Solution();
            string[] words = { "This", "is", "an", "example", "of", "text", "justification." };
            int maxWidth = 16;
            IList res = solution.FullJustify(words, maxWidth);
            foreach (var word in res)
            {
                Console.WriteLine(word);
            }
        }
    
        public IList FullJustify(string[] words, int maxWidth)
        {
            IList justification = new List();
            IList line = new List();
            int lineWidth = 0;
            int wordsCount = words.Length;
            for (int i = 0; i < wordsCount; i++)
            {
                string word = words[i];
                int newLength = lineWidth + (lineWidth > 0 ? 1 : 0) + word.Length;
                if (newLength <= maxWidth)
                {
                    lineWidth = newLength;
                }
                else
                {
                    string justifiedLine = JustifyLine(line, lineWidth, maxWidth);
                    justification.Add(justifiedLine);
                    line.Clear();
                    lineWidth = word.Length;
                }
                line.Add(word);
            }
            string justifiedLastLine = JustifyLastLine(line, maxWidth);
            justification.Add(justifiedLastLine);
            return justification;
        }
    
        public string JustifyLine(IList line, int lineWidth, int maxWidth)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(line[0]);
            int lineWords = line.Count;
            int splits = lineWords - 1;
            if (splits == 0)
            {
                while (sb.Length < maxWidth)
                {
                    sb.Append(" ");
                }
            }
            else
            {
                int spaces = maxWidth - (lineWidth - splits);
                int quotient = spaces / splits, remainder = spaces % splits;
                for (int i = 1; i < lineWords; i++)
                {
                    int currSpaces = quotient + (i <= remainder ? 1 : 0);
                    for (int j = 0; j < currSpaces; j++)
                    {
                        sb.Append(" ");
                    }
                    sb.Append(line[i]);
                }
            }
            return sb.ToString();
        }
    
        public string JustifyLastLine(IList line, int maxWidth)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(line[0]);
            int lineWords = line.Count;
            for (int i = 1; i < lineWords; i++)
            {
                sb.Append(" ");
                sb.Append(line[i]);
            }
            while (sb.Length < maxWidth)
            {
                sb.Append(" ");
            }
            return sb.ToString();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
  • 相关阅读:
    ssm电影院管理系统的设计与实现毕业设计源码241505
    Java代码审计——WebGoat XSS
    【组件封装】vue打字机效果和文字发光
    Visual Studio 调试 F5断点执行
    洛谷 Array 数论
    JVM(二)
    Python技巧---tqdm库的使用
    玩转Python:数据可视化,一个很高级的交互式Python库,附代码
    路由综合实验
    【考研线代】一. 行列式
  • 原文地址:https://blog.csdn.net/Aubyn11/article/details/133966538