Wiki
FlickrLeetcode
  • 💞Artificial Intelligence
    • ⚙️Midjourney vs Stable Diffusion
    • ⚙️Creative QR Codes with ControlNet
      • ⚙️How to generate a QR Code
      • ⚙️Collect prompt
      • ⚙️clip skip
      • ⚙️AUTOMATIC1111
      • ⚙️Edge detection and human pose detection
    • ⚙️Stable Diffusion
    • ⚙️What is 'token' and 'turbo'?
    • ⚙️Today's learning--LangChain
      • ⚙️Prompt
    • ⚙️LLM Parameters Demystified
    • ⚙️What is Cohere playground?
    • ⚙️DALL-E
    • ⚙️How to use AI to learn something that you don't know?
    • ⚙️Diffusers
    • Boosting Algorithms in machine learning, part 1:AdaBoost
  • 💞GitHub
    • ✅How to add a issue of the code with GitHub?
    • ✅How to edit code?
    • ✅How to use GitHub Desktop
    • ✅How to review in GutHub?
  • 💞Lastest concepts for me
    • 🪅Pandas DataFrame
    • 🪅Choosing between loc and iloc
  • 💞Need to remember
    • 🔢An article for leetcode
    • 🉑two types of group work
    • 🍒What is hashtag#?
    • 🐝Week6_APD
    • 🦋API
    • 🎼BFF
  • 💞Python
    • 🐍argument & parameter
    • 🐍"{:.2f}"
    • 🐍Timeit in Python
    • 🐍Today's learning--Pylint
    • 🐍Split and Strip in Python
    • 🐍Getter and Setter in Python
    • 🐍"import json" in Python
    • 🐍Open CSV file in Python
    • 🐍print(f"An error occurred: {e}")
  • Page
  • 🪅command-line
  • 💞DataVisualization
    • 🪅How to choose plot type
  • 💞DataCleaning
    • 🪅Some basic code of data_cleaning
  • 💞Java
    • 🍡difference use between ArrayList and HashMap in Java
    • 🍡ArrayList & LinkedList
    • 🍡assertFalse(App.checkInputNumber(1,0))
      • 🍡HashSet
    • 🍡iterator
    • 🍡Java concept of assignment 1
    • 🍡Week6_Java
    • 🍡serializable
  • 💞Mark something that easily to forget
    • 🙉Mark something
    • 🙉How to edit cover picture from "Flickr" using "URL" in GitBook?
  • 💞VS Code
    • ✖️Install a project in VS Code
    • ✖️What should do after editing the code of one branch?
    • ✖️How to create a branch in VS code?
    • ✖️How to debug?
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
  1. Java

difference use between ArrayList and HashMap in Java

An example:

When you want to iterate every element in ArrayList:

ArrayList<Character> Mylist = new ArrayList<Character>();
        for (char element : magazine.toCharArray()) {
            Mylist.add(element);
        }

When you want to iterate every element in HashMap:

HashMap<Character, Integer> charFreq = new HashMap<>();
    for (char c : magazine.toCharArray()) {
        charFreq.put(c, charFreq.getOrDefault(c, 0) + 1);
    }

Example 1:

Input: ransomNote = "a", magazine = "b"
Output: false

Example 2:

Input: ransomNote = "aa", magazine = "ab"
Output: false

Example 3:

Input: ransomNote = "aa", magazine = "aab"
Output: true

Option 1: Use ArrayList:

class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
        if (ransomNote.length() > magazine.length()) {
            return false;
        }
        ArrayList<Character> Mylist = new ArrayList<Character>();
        for (char element : magazine.toCharArray()) {
            Mylist.add(element);
        }
        for (char i : ransomNote.toCharArray()) {
            if (Mylist.contains(i)) {
                Mylist.remove((Character) i);    
            } else {
                return false;
            }
        }
        return true;
    }
} 

Option2: Use HashMap:

public boolean canConstruct(String ransomNote, String magazine) {
    if (ransomNote.length() > magazine.length()) {
        return false;
    }
    HashMap<Character, Integer> charFreq = new HashMap<>();
    for (char c : magazine.toCharArray()) {
        charFreq.put(c, charFreq.getOrDefault(c, 0) + 1);
    }
    for (char c : ransomNote.toCharArray()) {
        if (charFreq.containsKey(c) && charFreq.get(c) > 0) {
            charFreq.put(c, charFreq.get(c) - 1);
        } else {
            return false;
        }
    }
    return true;
}

In the context of the given code, char c is a variable that represents a single character in a string. The variable c is used to iterate through each character in the ransomNote and magazine strings.

The toCharArray() method is a built-in function in Java that is used to convert a string to a character array. When this method is called on a string, it returns a newly formed character array with a length equal to the given string and with the characters in the given string initialized as its contents. The returned array length is equal to the length of the string. This method is an instance method of the String class and does not accept any parameter. It is useful when we want to convert a string to a character array without writing any custom code. The toCharArray() method is used in the given code to convert the magazine and ransomNote strings to character arrays so that we can iterate through each character in the strings using a for-each loop.

PreviousSome basic code of data_cleaningNextArrayList & LinkedList

Last updated 1 year ago

Was this helpful?

💞
🍡