You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For a web developer, it is very important to know how to design a web page's size. So, given a specific rectangular web page’s area, your job by now is to design a rectangular web page, whose length L and width W satisfy the following requirements:
4
+
5
+
1. The area of the rectangular web page you designed must equal to the given target area.
6
+
7
+
2. The width W should not be larger than the length L, which means L >= W.
8
+
9
+
3. The difference between length L and width W should be as small as possible.
10
+
You need to output the length L and the width W of the web page you designed in sequence.
11
+
Example:
12
+
Input: 4
13
+
Output: [2, 2]
14
+
Explanation: The target area is 4, and all the possible ways to construct it are [1,4], [2,2], [4,1].
15
+
But according to requirement 2, [1,4] is illegal; according to requirement 3, [4,1] is not optimal compared to [2,2]. So the length L is 2, and the width W is 2.
16
+
17
+
"""
18
+
"""
19
+
暴力搜索
20
+
"""
21
+
importmath
22
+
classSolution(object):
23
+
defconstructRectangle(self, area):
24
+
"""
25
+
:type area: int
26
+
:rtype: List[int]
27
+
"""
28
+
mid=int(math.sqrt(area))
29
+
whilemid>0:
30
+
ifarea%mid==0:
31
+
return [int(area/mid),int(mid)]
32
+
mid-=1
Collapse file: easy/字符串/438_Find All Anagrams in a String.py
Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. You may assume the given string consists of lowercase English letters only and its length will not exceed 10000.
4
+
5
+
Example 1:
6
+
Input: "abab"
7
+
8
+
Output: True
9
+
10
+
Explanation: It's the substring "ab" twice.
11
+
Example 2:
12
+
Input: "aba"
13
+
14
+
Output: False
15
+
Example 3:
16
+
Input: "abcabcabcabc"
17
+
18
+
Output: True
19
+
20
+
Explanation: It's the substring "abc" four times. (And the substring "abcabc" twice.)
21
+
22
+
"""
23
+
"""my solution"""
24
+
25
+
classSolution(object):
26
+
defrepeatedSubstringPattern(self, s):
27
+
"""
28
+
:type s: str
29
+
:rtype: bool
30
+
"""
31
+
foriinrange(1, len(s) /2+1):
32
+
iflen(s) %i==0:
33
+
leng=len(s) /i
34
+
35
+
ifs[:i] *leng==s:
36
+
returnTrue
37
+
returnFalse
38
+
39
+
"""
40
+
Basic idea:
41
+
42
+
First char of input string is first char of repeated substring
43
+
Last char of input string is last char of repeated substring
44
+
Let S1 = S + S (where S in input string)
45
+
Remove 1 and last char of S1. Let this be S2
46
+
If S exists in S2 then return true else false
47
+
Let i be index in S2 where S starts then repeated substring length i + 1 and repeated substring S[0: i+1]
0 commit comments