“Offer comes, ask friends to take it! I am participating in the 2022 Spring Recruit Punch card campaign. Click here for more details.”


📢 preface

🚀 Algorithm 🚀
  • 🌲 punch in an algorithm every day, which is not only a learning process, but also a sharing process 😜
  • 🌲 tip: the programming languages used in this column are C# and Java
  • 🌲 to maintain a state of learning every day, let us work together to become a god of algorithms 🧐!
  • 🌲 today is the 90th day 🎈!
🚀 Algorithm 🚀

🌲 Convert to lowercase letters

Give you a string s, convert the uppercase letters in the string to the same lowercase letters, and return the new string.

Example 1:

Enter: s ="Hello"Output:"hello"
Copy the code

Example 2:

Enter: s ="here"Output:"here"
Copy the code

Example 3:

Enter: s ="LOVELY"Output:"lovely"
Copy the code

Tip:

  • 1 <= s.length <= 100
  • S consists of printable characters in the ASCII character set

🌻C# method: new space traversal

Can be understood as c# in the alphabet and ASCII code decimal number conversion problem, direct judgment can be

Code:

public class Solution {
    public string ToLowerCase(string s) {
            StringBuilder sb = new StringBuilder();
            int len = s.Length;
            for(int i=0; i<len; i++) {char ch = s[i];
                int num = (int)ch;
                if(num>=65&&num<=90)
                {
                    sb.Append((char)(s[i] + 32));
                }
                else{ sb.Append(s[i]); }}returnsb.ToString(); }}Copy the code

The execution result

By execution time:92Ms, in all C# beat 25.50% of users in submissionMemory consumption:35.4MB, in all C# beat 29.90% of users in submission
Copy the code

🌻Java method: hash table

Thinking analytical

Code:

public class Solution {
    public String[] findRestaurant(String[] list1, String[] list2) {
        HashMap < Integer, List < String >> map = new HashMap < > ();
        for (int i = 0; i < list1.length; i++) {
            for (int j = 0; j < list2.length; j++) {
                if (list1[i].equals(list2[j])) {
                    if(! map.containsKey(i + j)) map.put(i + j,newArrayList < String > ()); map.get(i + j).add(list1[i]); }}}int min_index_sum = Integer.MAX_VALUE;
        for (int key: map.keySet())
            min_index_sum = Math.min(min_index_sum, key);
        String[] res = new String[map.get(min_index_sum).size()];
        returnmap.get(min_index_sum).toArray(res); }}Copy the code

The execution result

By execution time:89Ms, beat out all Java commits23.41% user memory consumption:38.8MB, beat out all Java commits94.40% of the userCopy the code

Complexity analysis

Time: O(n) Space: O(1) 
Copy the code

💬 summary

  • Today is the 90th day of the clocking algorithm!
  • The article USES theC# andJavaTwo programming languages to solve the problem
  • Some methods are also written by the god of reference force buckle, and they are also shared while learning, thanks again to the algorithm masters
  • That’s the end of today’s algorithm sharing, see you tomorrow!