π’ preface
π Algorithm problem π |
- π² Punching out an algorithm problem every day is not only a learning process, but also a sharing process π
- π² Tip: the problem-solving programming languages in this column are C# and Java
- π² To maintain a state of learning every day, let's work together to become the great God of algorithm π§!
- π² Today is the 49th day of punching out the force deduction algorithm π!
π Algorithm problem π |
π² Example of original question: summary interval
Given an ordered integer array nums without duplicate elements.
Returns a list of the smallest ordered interval ranges that exactly cover all numbers in the array. That is, each element of num is exactly covered by an interval range, and there is no number x belonging to a range but not num.
Each interval range [a,b] in the list shall be output in the following format:
- "A - > b", if a= b
- "A", if a == b
Example 1:
Input: nums = [0,1,2,4,5,7] Output:["0->2","4->5","7"] Explanation: the range is: [0,2] --> "0->2" [4,5] --> "4->5" [7,7] --> "7" Source: force buckle( LeetCode) Link: https://leetcode-cn.com/problems/summary-ranges The copyright belongs to Lingkou network. For commercial reprint, please contact the official authorization, and for non-commercial reprint, please indicate the source.
Example 2:
Input: nums = [0,2,3,4,6,8,9] Output:["0","2->4","6","8->9"] Explanation: the range is: [0,0] --> "0" [2,4] --> "2->4" [6,6] --> "6" [8,9] --> "8->9"
Example 3:
Input: nums = [] Output:[]
Example 4:
Input: nums = [-1] Output:["-1"]
Example 5:
Input: nums = [0] Output:["0"]
Tips:
- 0 <= nums.length <= 20
- -231 <= nums[i] <= 231 - 1
- All values in nums are different from each other
- nums in ascending order
π» C# method: depth first search
- In essence, it is a circular array to judge whether the difference between the current position and the previous position (or between the current position and the next position) is 1. If yes, continue, and if not, add it to the list
- There are also two cases of adding to the list.
a. If the continuous value exceeds 1, use X - > y format
b. Continuous values equal to 1 and no more than 1 are in x format
code:
public class Solution { public IList<string> SummaryRanges(int[] nums) { List<string> res = new List<string>(); int i=0,j=0; while(j<nums.Length){ //J = = num.length-1 is an important judgment, and the last situation of the array can be considered if(j==nums.Length-1 || nums[j+1]-nums[j]!=1){ if(i==j) res.Add($"{nums[i]}"); else res.Add($"{nums[i]}->{nums[j]}"); i = j+1; } ++j; } return res; } }
results of enforcement
adopt Execution time: 232 msοΌAt all C# Defeated 69.77% of users in submission Memory consumption: 30 MBοΌAt all C# Defeated 74.42% of users in submission
π» Java method: one-time traversal
Train of thought analysis
We start from the position 00 of the array and traverse to the right. Every time we encounter that the difference between adjacent elements is greater than 11, we find an interval. After traversing the array, we can get a list of a series of intervals.
In the traversal process, maintain the subscripts low and high to record the starting point and ending point of the interval respectively. For any interval, there is low ≤ high. When an interval is obtained, the string representation of the interval is generated according to the values of low and high.
- When low < high, the string of the interval is expressed as' low → high ';
- When low=high, the string of the interval is represented as' low '.
code:
class Solution { public List<String> summaryRanges(int[] nums) { List<String> ret = new ArrayList<String>(); int i = 0; int n = nums.length; while (i < n) { int low = i; i++; while (i < n && nums[i] == nums[i - 1] + 1) { i++; } int high = i - 1; StringBuffer temp = new StringBuffer(Integer.toString(nums[low])); if (low < high) { temp.append("->"); temp.append(Integer.toString(nums[high])); } ret.add(temp.toString()); } return ret; } }
results of enforcement
adopt Execution time: 0 msοΌAt all Java Defeated 100 in submission.00%User Memory consumption: 36.5 MBοΌAt all Java Beat 65 in submission.23%User
Complexity analysis
Time complexity: O( n ) Space complexity: O( 1 )
π¬ summary
- Today is the 49th day of punching out the force deduction algorithm!
- This paper uses C# and Java programming languages to solve problems
- Some methods are also written by Likou God, and they are also shared while learning. Thanks again to the algorithm bosses
- That's the end of today's algorithm sharing. See you tomorrow!
π Previous high-quality article sharing
- β€οΈUnity zero foundation to entry | systematic learning route of game engine unity from 0 to 1 [comprehensive summary - suggestions collection]!
- π§‘Spend a day doing a high-quality aircraft war game and have a complete course of 10000 word Unity! The beautiful schoolgirl read the direct call 666!
- πRecall the production process + analysis of the classic game [Bomber game] played with my friends in childhood
- πA first person shooting game Demo similar to CS made all night! It's not very difficult to play the game
- π€Explosive liver wrote a real-time combat game Demo similar to the Royal war all weekend! More than 20000 word game production process + analysis!
- πHow to make a horizontal arcade fighting game similar to "dinosaur quick fight"? | learn together and send the source code by the way [code text is not easy, it is recommended to collect and learn]
- π[super practical skills] | necessary skills to improve the quality and speed of writing: detailed description of Typora drawing bed configuration