leetCode806. The number of rows required to write a string

Source: Internet
Author: User
Topic Description Tips Help submit a record community discussion reading answer a random question

We want to write the given string S from left to right on each line, the maximum width of each row is 100 units, if we write a letter that will make this line more than 100 units, then we should write this letter to the next line. We given an array of widths, this array widths[0] stands for ' A ' required units, Widths[1] stands for ' B ' required units, ..., widths[25] represents the units required for ' Z '.

Now answer two questions: at least how many lines can put down s, and how many units the last line is used. Return your answer as a list of integers of length 2.

Example 1:
input: 
widths = [10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10]
S = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
output: [3]
explanation: 
All characters have the same occupancy unit 10. So to write all 26 letters,
we need 2 full lines and a row of 60 units.
Example 2:
input: 
widths = [4,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10]
S = " Bbbcccdddaaa "
output: [2, 4]
explanation: 
Removing all characters of the letter ' A ' is the same unit 10, and the string" Bbbcccdddaa "will overwrite 9 * 10 + 2 * 4 = 98 units.
The last letter ' A ' will be written to the second line because there are only 2 units left in the first line.
So, the answer is 2 lines, and the second line has 4 units of width.

Note: The length of the string S is in the range of [1, 1000]. S contains only lowercase letters. Widths is an array of length 26. Solution Idea: Define 3 variables, num represents the total units per row, line represents the number of rows, r represents each occupancy unit, and then uses for to connect each string to the corresponding unit.

Import Java.util.Arrays;
		public class NumberOfLines {public static int[] NumberOfLines (int[] widths, String S) {int num = 0;
		int line=1;
			for (int i=0;i<s.length (); i++) {int r = 0;
				Switch (S.charat (i)) {case ' a ': num+=widths[0];
				R+=widths[0];
			Break
				Case ' B ': num+=widths[1];
				R+=WIDTHS[1];
			Break
				Case ' C ': num+=widths[2];
				R+=WIDTHS[2];
			Break
				Case ' d ': num+=widths[3];
				R+=WIDTHS[3];
			Break
				Case ' E ': num+=widths[4];
				R+=WIDTHS[4];
			Break
				Case ' F ': num+=widths[5];
				R+=WIDTHS[5];
			Break
				Case ' G ': num+=widths[6];
				R+=WIDTHS[6];
			Break
				Case ' H ': num+=widths[7];
				R+=WIDTHS[7];
			Break
				Case ' I ': num+=widths[8];
				R+=WIDTHS[8];
			Break
				Case ' J ': num+=widths[9];
				R+=WIDTHS[9];
			Break
				Case ' K ': num+=widths[10];
				R+=WIDTHS[10];
			Break
				Case ' l ': num+=widths[11];
				R+=WIDTHS[11];
			Break Case ' m ': num+=widths[12];
				R+=WIDTHS[12];
			Break
				Case ' n ': num+=widths[13];
				R+=WIDTHS[13];
			Break
				Case ' O ': num+=widths[14];
				R+=WIDTHS[14];
			Break
				Case ' P ': num+=widths[15];
				R+=WIDTHS[15];
			Break
				Case ' Q ': num+=widths[16];
				R+=WIDTHS[16];
			Break
				Case ' R ': num+=widths[17];
				R+=WIDTHS[17];
			Break
				Case ' s ': num+=widths[18];
				R+=WIDTHS[18];
			Break
				Case ' t ': num+=widths[19];
				R+=WIDTHS[19];
			Break
				Case ' u ': num+=widths[20];
				R+=WIDTHS[20];
			Break
				Case ' V ': num+=widths[21];
				R+=WIDTHS[21];
			Break
				Case ' W ': num+=widths[22];
				R+=WIDTHS[22];
			Break
				Case ' x ': num+=widths[23];
				R+=WIDTHS[23];
			Break
				Case ' Y ': num+=widths[24];
				R+=WIDTHS[24];
			Break
				Case ' Z ': num+=widths[25];
				R+=WIDTHS[25];
			Break
			Default:break;
				} if (num>100) {num=0;
				line++;
			Num+=r;
		}} int[] a = {line,num};
	return A; } PubLic static void Main (string[] args) {int[] widths = {10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10
		, 10,10,10,10};
		String S = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	System.out.println (arrays.tostring (NumberOfLines (widths, S)); }




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.