githubEdit

Week 1

Monday

Problem

Solution

My Solution

The whole idea is to start from the LSB, iterate all the way till the MSB. And use a variable remain to keep track of whether the plus one has been consumed or not.

  1. If it is consumed, break the loop and return the result.

  2. If not, technically means a new remain, which is 1, is generated, we keep iterating, but remember to change that digit to 0.

At the end of the iteration, if remain still exist, means we have got a [1, 0, ... ,0].

Good Solution

One solution publised on leetcode is more elegant than mine

And the tips worth noting down is

  1. When create an array using int[] a = new int[3], all elements of that array are initialized to the default value automatically.

  2. For Java Array, to get the array length, we call .length field. For Java Strings and other objects, we call .length() method or .size() based on their corresponding API.

Tuesday

Problem

Solution

My Solution

This is a trivial problem, as the String class in Java provides a method called .toLowerCase(), which will return a String with all its characters in lower case.

Btw, this problem also invokes us to think the general String Manipulation in Java. For the sake of this, see Java Basics for DSA.

Wednesday

Problem

Solution

My Solution

Technically, as this prob is demostrated by Prof Halim during lecture. I would say this is technically Prof Halim's solution. 😂

The learning point for this problem is how to do the String Iteration using .toCharArray().

Thursday

Problem

Solution

The question is really straight-forward. Just use a for loop to sum the primary and secondary diagnols. And if the width of the square is odd, minus the center point.

The learning point of this problem is to know how to get the length and width of a 2-D array.

Friday

Problem

Solution

My Solution

My initial idea is to use a for loop to solve, but the last two test cases I got Time Limit Exceed. And after looking at the hints, I find out this problem is just a finding pattern problem in Java. The pattern should be: (denote high-low+1 as range)

  1. If range is even, then the number of odd should be range/2.

  2. Else, meaning that low and high must be both odd or even

    1. If low and high are odd, the number of odd should be range/2 + 1

    2. Else, the number of odd should be range/2

Last updated