This column mainly records Likou's record of brushing questions, preparing for the Blue Bridge Cup , for review and optimization of the algorithm , and I hope to bring you some help. The blogger is an algorithm novice. I hope you don't laugh at it. Today I want to share Yes——"Records of Likou Brushing Questions—796. Rotating Strings, 884. Uncommon Words in Two Sentences, 1046. The Weight of the Last Stone".
Table of contents
884. Uncommon Words in Two Sentences
1046. The Weight of the Last Stone
796. Rotate Strings
topic description
Given two strings,
s
andgoal
. If after a number of rotation operations,s
can becomegoal
, then returntrue
.
s
The rotation operation is tos
move the leftmost character to the rightmost.
- For example, if
s = 'abcde'
, after one rotation the result is'bcdea'
.Example 1:
Input: s = "abcde", goal = "cdeab" Output: trueExample 2:
Input: s = "abcde", goal = "abced" Output: false
problem solving ideas
Use the while loop to judge, and continuously add the left string of goal to the right to compare with the s string, and return true if they are equal, and return false if they are not equal after the comparison.
problem solving code
def rotateString(s: str, goal: str) -> bool:
if len(s) != len(goal):
return False
count = 0
while count <= len(goal):
temp = goal[0]
goal = goal[1::]
goal += temp
if goal == s:
return True
count+=1
return False
884. Uncommon Words in Two Sentences
topic description
A sentence is a string of words separated by spaces. Each word consists of lowercase letters only.
A word is uncommon if it occurs exactly once in one of the sentences but not in the other .
Given your two sentences
s1
ands2
, return a list of all uncommon words . The words in the returned list can be organized in any order .Example 1:
输入:s1 = "this apple is sweet", s2 = "this apple is sour" 输出:["sweet","sour"]Example 2:
Input: s1 = "apple apple", s2 = "banana" Output: ["banana"]
problem solving ideas
First cut the string into a list, and then traverse the list twice to count the relationship between elements.
problem solving code
def uncommonFromSentences(s1: str, s2: str) -> list[str]:
list_1 = s1.split(" ")
list_2 = s2.split(" ")
result = []
for i in list_1:
if list_1.count(i) == 1 and list_2.count(i)==0:
result.append(i)
for i in list_2:
if list_2.count(i) == 1 and list_1.count(i)==0:
result.append(i)
return result
1046. The Weight of the Last Stone
topic description
There is a pile of stones, each of which weighs a positive integer.
Each turn, the two heaviest stones are chosen and smashed together. Suppose the weights of stones are
x
andy
, respectivelyx <= y
. Then the possible outcomes of crushing are as follows:
- If
x == y
, then both stones are completely crushed;- If
x != y
, thenx
a stone of weight will be completely shattered, andy
a stone of weight will have a new weight ofy-x
.In the end, at most one stone will remain. Returns the weight of this stone. If there are no stones left, return
0
.Example:
Input: [2,7,4,1,8,1] Output: 1 Explanation: First select 7 and 8 to get 1, so the array is converted to [2,4,1,1,1], Then select 2 and 4 to get 2, so the array is converted to [2,1,1,1], followed by 2 and 1 to get 1, so the array is converted to [1,1,1], Finally, 1 and 1 are selected to get 0, and the final array is converted to [1], which is the weight of the last remaining stone.
problem solving ideas
Use the while loop to judge, the sorted method does not change the order of the original list, only needs to sort, compare the relationship between the first and second largest elements and perform corresponding operations. Finally, remember to judge whether the list is empty, if it is empty, return 0 directly.
problem solving code
def lastStoneWeight(stones: list[int]) -> int:
while len(stones) > 1:
stones_1 = sorted(stones,reverse=-1)
max_stone = stones_1[0]
sed_stone = stones_1[1]
if max_stone == sed_stone:
stones.remove(max_stone)
stones.remove(sed_stone)
elif sed_stone < max_stone:
stones.remove(max_stone)
stones.remove(sed_stone)
stones.append(max_stone-sed_stone)
if len(stones)==0:
return 0
return stones[0]