LoginSignup
0
0

More than 1 year has passed since last update.

Leetcode 1768. Merge Strings Alternately

Posted at

アプローチ

  • Brute-force

コード

class Solution {
    public  String mergeAlternately(String word1, String word2) {
        String result = "";

        int word1Size = word1.length();
        int word2Size = word2.length();

        int word1index = 0;
        int word2index = 0;

        int turn = 1;

        while (true) {
            if (word1index >= word1Size && word2index >= word2Size) {
                break;
            }

            if (turn % 2 == 1) {
                if (word1index <= word1Size - 1) {
                    result += "" + word1.charAt(word1index);

                }
                word1index++;


            } else {
                if (word2index <= word2Size - 1) {
                    result += "" + word2.charAt(word2index);

                }
                word2index++;

            }
            turn++;
        }

        return result;
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0