LeetCode - Algorithms - 34. Find First and Last Position of Element in Sorted Array

Problem

34. Find First and Last Position of Element in Sorted Array

Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution {
public int[] searchRange(int[] nums, int target) {
int lo = 0;
int hi = nums.length - 1;
while (lo<=hi) {
int mid = lo + (hi - lo) / 2;
if (target < nums[mid])
hi = mid-1;
else if (target > nums[mid])
lo = mid+1;
else {
int startPos = mid;
int endPos = mid;
while(startPos>=0 && nums[startPos]==nums[mid]) startPos--;
while(endPos<=nums.length - 1 && nums[endPos]==nums[mid]) endPos++;
int[] r = new int[2];
r[0] = startPos+1;
r[1] = endPos-1;
return r;
}
}

int[] r = {-1, -1};
return r;
}
}

Submission Detail

  • 88 / 88 test cases passed.
  • Runtime: 0 ms, faster than 100.00% of Java online submissions for Find First and Last Position of Element in Sorted Array.
  • Memory Usage: 44.5 MB, less than 6.38% of Java online submissions for Find First and Last Position of Element in Sorted Array.

JavaScript

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var searchRange = function(nums, target) {
var lo = 0, hi = nums.length-1;
while (lo<=hi) {
var mid = lo + Math.floor((hi-lo)/2);
if (target < nums[mid])
hi = mid-1;
else if (target > nums[mid])
lo = mid+1;
else {
var startPos = mid, endPos = mid;
while(startPos>=0 && nums[startPos]==nums[mid]) startPos--;
while(endPos<=nums.length - 1 && nums[endPos]==nums[mid]) endPos++;
return [startPos+1, endPos-1];
}
}

return [-1, -1];
};

Submission Detail

  • 88 / 88 test cases passed.
  • Runtime: 92 ms, faster than 7.49% of JavaScript online submissions for Find First and Last Position of Element in Sorted Array.
  • Memory Usage: 34.9 MB, less than 100.00% of JavaScript online submissions for Find First and Last Position of Element in Sorted Array.

LeetCode - Algorithms - 205. Isomorphic Strings

Problem

205. Isomorphic Strings

Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution {
public boolean isIsomorphic(String s, String t) {
boolean r = true;
Map<Character,Character> map = new HashMap<Character, Character>();
Map<Character,Character> map2 = new HashMap<Character, Character>();
for(int i=0; i<s.length(); i++) {
char k = s.charAt(i);
char x = t.charAt(i);
if (map.containsKey(k)) {
char v = map.get(k);
if (x!=v) {
return false;
}
}
else {
map.put(k,x);
}
if (map2.containsKey(x)) {
char v = map2.get(x);
if (k!=v) {
return false;
}
}
else {
map2.put(x, k);
}
}
return r;
}
}

Submission Detail

  • 32 / 32 test cases passed.
  • Runtime: 12 ms, faster than 26.96% of Java online submissions for Isomorphic Strings.
  • Memory Usage: 39.6 MB, less than 6.14% of Java online submissions for Isomorphic Strings.

How to Read Code

How to read other people’s code? You will need good code-browsing skills to make sense of the code.

Start by gathering information about which problems the program should solve and which code style to adapt.

Don’t spend all your time figuring out how everything works at the beginning. Narrow things down to something you can actually process.

Step through behavior that concerns you with a debugger. The stack trace have a lot of information about how events are propogated in the application.

In Action

getting it into a “smart” IDE that can make sense of it

try to build and run it (preferably in a debugger)

  • Find Main, Reading “main”

Learn to Dig

git blame

You can use git blame on a file to get an author name, last modified date, and commit hash for every single line.

git log

Use git log to look at the commit history of the overall repo.

git log can also show you the history of a single file with the -p flag: git log -p index.js.

breakpoint

One method the community strongly suggests is to set out breakpoint on methods that triggers behaviours that concerns you. If changing a line of code causes unexpected changes on other places, put a breakpoint on that line, and see what happens. You have a lot of information on the runtime state once the breakpoint hits. Breakpoints stops the execution of the code on a certain line, and from there, you can see from the stacktrace how the code got there. Use the step into and step out functions to see what the different methods does and how events are propogated in the application. This will teach you something about the structure of the codebase.

Grepping

the search function in your IDE

Search for Key Words

Use your editor’s Find feature (or grep) to search the entire source tree for occurrences of words associated with what you want to know. For example, if you want to figure out how/where the program opens files, search for “open” or “file”. You may get zillions of hits, but that’s not a bad thing–a focused reading of the code will result.
Often-useful words for C/C++ programs are “main”, “abort”, “exit”, “catch”, “throw”, “fopen”, “creat”, “signal”, “alarm”, “socket”, “fork”, “select”. Other words are useful in other languages.

Code Review

Software development is a very collaborative work. No one can build a large or a significant software alone. Every software is built in a team. In a team, everyone contributes to shaping a project. End of the day, everyone’s contribution get merged and become a good piece of work which has a real value to the customers.

Besides doing the actual coding, there is another practice that every team these days does, which is, review each other’s code making observations, suggestions and learning from one other. This is a powerful tool for building knowledge of a code base, creating strong bonds in a team and improving code quality that leads to fewer bugs in the system, and happy customers.

Doing code review, you are forced to read someone else’s code in your team which ends up improving your code reading skill.

Things to Remember

Go Back in Time

Read the Specs

Think of Comments as Hints

Commenting your code is incredibly important, for yourself and for your peers reviewing your code.

Notice Style

Expect to Find Garbage

Don’t Get Lost

  • Where is this button?
  • What do the tests do?
  • The graphical layout
  • Runtime Investigation

Find one thing you know the code does, and trace those actions backward, starting at the end.

Let’s call those connected pieces of code a “chain of actions.”

Following input events

Rinse and repeat.

a body of code is designed to tackle one (or more) complex problems.

the more you can gain an understanding of how different parts of the code are connected, the more you’ll develop an understanding of the entire codebase, as a whole.

the more (good) code you see, the easier it becomes to read and understand all code, and the faster you can do so.

“high quality examples of expertise” = good code

exposure to high quantity, high quality examples of expertise is one of the two main factors that dictate how quickly and effectively people learn new skills. – Badass: Making Users Awesome, Kathy Sierra

The more you watch (or listen) to expert examples, the better you can become. The less exposure you have to experts or results of expert work, the less likely you are to develop expert skills.

the longer you’re programming — and thus the more code samples you see, of all different kinds — the easier it gets to understand other people’s code. And the faster you’re able to do it.

Reading a class

Before reading the implementation of a class, however, I recommend you study its interface.

The public functions will most likely be the command interface for your class.

Retelling or Rubber Ducking

Use/know tools

There are plenty of tools out there for reading and exploring source code that can help to visualize code. For example – IntelliJIdea has really a great capability of navigating source code where you can search by words, part of a word or even abbreviation. You should learn the keyboard shortcuts as well. Navigating source code with the mouse can be pretty boring and slow where working with the keyboard shortcuts can be faster. You can jump from one part of the source code to another quickly.

There is another great software to read code, called Sourcegraph, which was created by two Stanford grads, Quinn Slack and Beyang Liu, who, after spending hours hunting through the poorly documented code, decided to build a tool to help them better reading and understanding the code.

Know the language/conventions

Read the best practices/design patterns

Temporary Refactoring

heuristics

Code is not like a novel

if you insist on understanding each and every thing before you understand the next, you will be lost for sure.

Let the machine help

Take the time to learn and use the stepper, trace, inspecting and browsing tools.

Many important relations in a program are non-sequential, and there are tools that will help you find what to look at next.

Start with an example

Don’t try to understand what all the code does at once. Instead, start with a specific example, representative of what a new user might try in his or her first session with the program, or an example from the manual primer.
Set yourself the task of learning just how that particular example works. Ignore everything else that doesn’t pertain to that example.

Go just as deep as you have to do to understand it, no deeper. Treat everything else as a black box. Learn about how it works later.

Become familiar with the user interface from a user’s perspective. For each user-visible action or object, ask: “Where is that implemented”?

Have a specific goal in mind when you work. Experiment with small changes and extensions to the code to test your understanding.

If it ain’t broke, fix it

The best attitude to take is to pretend you are debugging the code, even if there is nothing wrong with it. The same tools that are used for debugging the code will be helpful in getting you to understand it in the first place. Even if you aren’t looking for bugs now, if you plan on using, extending or modifying the code, you soon will be.

Tools

  • Jump to function definition
  • Step the code
  • Guess function names
  • Trace functions
  • Inspect the application’s data structures

RTFM

When all else fails, read the documentation.

another invaluable tool is stored inside your browser: the Developer tools. Knowing how to use the developer tools correctly and effectively can help you quickly locate the source of problems with styling these sections.

localized JavaScript console, where you can test out code separately from your programs.

Start with what you do know

find one thing you know the code does, and trace those actions backward, starting at the end.

Believe that you know what you’re looking at

For ultimately effective searching, you’ll want to use something like “language_name function_name” in your Google search, rather than just the function name, as they can be repetitive across multiple coding languages.

In the long run, expose yourself to high-quality code

By exposing yourself to well-written code made by experienced programmers, you are subconsciously taking in the methodology they follow, as well as their best practices.

If you read poorly-written, non-semantic code, you will likely emulate the same when writing your own. And yes, although we all start writing garbage code, our ultimate goal is to move on from that point as quickly as possible!

Try watching live-streamed programming (or pair programming with a peer)

it’s one of the best ways to watch the coding process from start to finish in real time, and ask questions as you encounter them. Watching other people program in real time also gives you insight into other people’s coding processes, and helps you develop good habits, regardless of whether or not they are a master themselves.

For an excellent resource on the front end (HTML, CSS, JavaScript), try perusing Codepen. Free, and very user-friendly, many front-end developers use this site as a sandbox.

On the back end, I recommend you sign up for GitHub and start digging into some of the most popular repositories. You can view a file from right within your browser, and if you wish to improve on the code, you can edit it in your own forked version and create a “pull request”. If the author likes your improvements, they may choose to implement them into their programs!

Find the high-level logic

Starting with the program’s entry point (e.g. main() in C/C++/Java), find out how the program initializes itself, does its thing, and then terminates.

Basically, skimmed through the whole project and gain a primary idea and then ask a question to yourself where do you want to focus, which part you want to read first.

Draw Some Flowcharts

Yes, we all know flowcharts are a horrible design tool, but they can be useful when analyzing program flow. You can’t keep all that information in your head, so draw some flowcharts or state diagrams while you are reading. Make sure you account for both sides of a two-way branch, and all branches of multi-way branches.

Examine Library Calls

Leverage the Power of Code Comprehension Tools

Some techniques available with a good text search tool, or better yet a tool that can analyze the source and find references and generate diagrams, allow the following questions to be answered, for object-oriented code:

  • Who calls this method?
  • Who implements this interface or subclasses this class?
  • What are this class’ superclasses?
  • Where are instances of this class created, held, and passed as arguments or returned?
  • What superclass methods does this class override?
  • Where might methods of this class be called polymorphically, i.e. through a base class or interface?

developing a “big picture” view of the code

reading and writing tests

Although testing is rare, it acts as a wonderful documentation tool, and is a good introduction to a new codebase.

Comment the Code

One of the best ways of reverse-engineering code you’re so unfamiliar with that you cannot even guess to comment is to derive HoareTriples for the code under scrutiny, and to compute a procedure’s WeakestPrecondition. Knowing the invariants of a procedure often gives valuable clues as to its intended purpose, thus letting you derive understanding from the code in a way that merely guessing from reading a number of poorly named procedure names simply cannot. Using this technique, you might even find bugs in the code, despite your lack of understanding thereof (I know this from experience!).

Clean Up the Code

Use Aspect Browser

Aspect Browser is a tool for viewing cross-cutting code aspects in large amounts of code. I find it indispensable for navigating unfamiliar code, as a kind of super-grep.

AspectBrowser for Eclipse

Resources

HackerRank - Regex - Character Class

© HackerRank

Matching Specific Characters

The character class [ ] matches only one out of several characters placed inside the square brackets.

Task

1203x.

1
^[123][120][xs0][30Aa][xsu][\.\,]$

Excluding Specific Characters

The negated character class [^] matches any character that is not in the square brackets.

Task

think?

1
^[^\d][^aeiou][^bcDF][^\s][^AEIOU][^\.\,]$

Matching Character Ranges

In the context of a regular expression (RegEx), a character class is a set of characters enclosed within square brackets that allows you to match one character in the set.

A hyphen (-) inside a character class specifies a range of characters where the left and right operands are the respective lower and upper bounds of the range. For example:

  • [a-z] is the same as [abcdefghijklmnopqrstuvwxyz]
  • [A-Z] is the same as [ABCDEFGHIJKLMNOPQRSTUVWXYZ]
  • [0-9] is the same as [0123456789]

In addition, if you use a caret (^) as the first character inside a character class, it will match anything that is not in that range. For example, [^0-9] matches any character that is not a digit in the inclusive range from 0 to 9.

It’s important to note that, when used outside of (immediately preceding) a character or character class, the caret matches the first character in the string against that character or set of characters.

Task

h4CkR

1
^[a-z][1-9][^a-z][^A-Z][A-Z]*

Resources

Regex - Subdomains - Character Class

If—

by Rudyard Kipling

If you can keep your head when all about you
Are losing theirs and blaming it on you;
If you can trust yourself when all men doubt you,
But make allowance for their doubting too;
If you can wait and not be tired by waiting,
Or, being lied about, don’t deal in lies,
Or being hated, don’t give way to hating,
And yet don’t look too good, nor talk too wise;

If you can dream—and not make dreams your master;
If you can think—and not make thoughts your aim;
If you can meet with Triumph and Disaster
And treat those two impostors just the same;
If you can bear to hear the truth you’ve spoken
Twisted by knaves to make a trap for fools,
Or watch the things you gave your life to, broken,
And stoop and build ’em up with worn-out tools;

If you can make one heap of all your winnings
And risk it on one turn of pitch-and-toss,
And lose, and start again at your beginnings
And never breathe a word about your loss;
If you can force your heart and nerve and sinew
To serve your turn long after they are gone,
And so hold on when there is nothing in you
Except the Will which says to them: “Hold on!”

If you can talk with crowds and keep your virtue,
Or walk with kings—nor lose the common touch,
If neither foes nor loving friends can hurt you,
If all men count with you, but none too much;
If you can fill the unforgiving minute
With sixty seconds’ worth of distance run,
Yours is the Earth and everything that’s in it,
And—which is more—you’ll be a Man, my son!


  • In a poll conducted in 1995 by the BBC Radio 4 Bookworm programme to determine the nation’s favourite poems, If— came first.
  • The poem If of Kipling is written as if a father is talking to his son, giving him advices on the way to behave to obtain the reputation of being a mature and an outstanding citizen of the community and the world. It has been written in 1895 and published in his collection Rewards and Fairies in 1909. Throughout the poem, Kipling illustrates ideal behaviour and virtue through the use of paradox: righteousness without smugness; detachment while practicing determination; and noble life blended with commonality. The employment of these contradictory extremes throughout the poem serves to illustrate a central theme of striving for an idealized “golden mean” in all facets of life.

Resources

HackerRank - Regex - Introduction

© HackerRank

Matching Anything But a Newline

The dot (.) matches anything (except for a newline).

Task

123.456.abc.def

1
^(.{3}\.){3}.{3}$

Matching Digits & Non-Digit Characters

\d

The expression \d matches any digit [0-9].

\D

The expression \D matches any character that is not a digit.

Task

06-11-2015

1
(\d){2}(\D){1}(\d){2}(\D){1}(\d){4}

Matching Whitespace & Non-Whitespace Character

\s

\s matches any whitespace character [ \r\n\t\f ].

\S

\S matches any non-white space character.

For Example

match blank line

1
\r\n

Task

12 11 15

1
(\S){2}(\s){1}(\S){2}(\s){1}(\S){2}

Matching Word & Non-Word Character

\w

The expression \w will match any word character.
Word characters include alphanumeric characters (a-z, A-Z and 0-9) and underscores (_).

\W

\W matches any non-word character.
Non-word characters include characters other than alphanumeric characters (a-z, A-Z and 0-9) and underscore (_).

Task

www.hackerrank.com

1
(\w){3}(\W){1}(\w){10}(\W){1}(\w){3}

Matching Start & End

^

The ^ symbol matches the position at the start of a string.

$

The $ symbol matches the position at the end of a string.

Task

0qwer.

1
^(\d){1}(\w){4}(\.){1}$

Resources

Regex - Subdomains - Introduction

LeetCode - Algorithms - 128. Longest Consecutive Sequence

Hard problem. Exercise for union-find. Almost done by myself, with help of union-find by Robert Sedgewick and Kevin Wayne.

Problem

128. Longest Consecutive Sequence

Java

union–find

© 1.5 Case Study: Union-Find Copyright © 2000–2019 Robert Sedgewick and Kevin Wayne. All rights reserved.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
class Solution {
public int longestConsecutive(int[] nums) {
int[] arr = removeDuplicates(nums);
int N = arr.length;
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for(int i=0; i<N; i++) {
if (!map.containsKey(arr[i]-1)) map.put(arr[i]-1, i);
if (!map.containsKey(arr[i]+1)) map.put(arr[i]+1, i);
}
int len = 0;
UF uf = new UF(N);
for(int i=0; i<N; i++) {
if (map.containsKey(arr[i])) {
uf.union(i, map.get(arr[i]));
}
}
int[] parent = uf.parent();
Map<Integer,Integer> tmpMap = new HashMap<Integer, Integer>();
int k;
for(int i=0; i<parent.length; i++) {
k = parent[i];
if (tmpMap.containsKey(parent[i]))
tmpMap.put(k, tmpMap.get(k)+1);
else
tmpMap.put(k, 1);
}
for(Integer n : tmpMap.values()) {
if (n>len)
len = n;
}

return len;
}

private int[] removeDuplicates(int[] input) {
if (input == null || input.length <= 0) {
return input;
}
Set<Integer> aSet = new HashSet<>(input.length);
for (int i : input) {
aSet.add(i);
}
int[] a = new int[aSet.size()];
int i=0;
for(Integer x : aSet)
a[i++] = x;
return a;
}
}

/**
* Weighted quick-union by rank with path compression by halving.
* @author Robert Sedgewick
* @author Kevin Wayne
* Algorithms, 4th edition textbook code and libraries
* https://github.com/kevin-wayne/algs4
*/
class UF {
private int[] parent;
private byte[] rank;
private int count;

public UF(int n) {
if (n < 0) throw new IllegalArgumentException();
count = n;
parent = new int[n];
rank = new byte[n];
for (int i = 0; i < n; i++) {
parent[i] = i;
rank[i] = 0;
}
}

public int find(int p) {
validate(p);
while (p != parent[p]) {
parent[p] = parent[parent[p]];
p = parent[p];
}
return p;
}

public int count() {
return count;
}

public void union(int p, int q) {
int rootP = find(p);
int rootQ = find(q);
if (rootP == rootQ) return;

if (rank[rootP] < rank[rootQ]) parent[rootP] = rootQ;
else if (rank[rootP] > rank[rootQ]) parent[rootQ] = rootP;
else {
parent[rootQ] = rootP;
rank[rootP]++;
}
count--;
}

private void validate(int p) {
int n = parent.length;
if (p < 0 || p >= n) {
throw new IllegalArgumentException("index " + p + " is not between 0 and " + (n-1));
}
}

public int[] parent() {return parent;}
}

Submission Detail

  • 68 / 68 test cases passed.
  • Runtime: 8 ms, faster than 23.72% of Java online submissions for Longest Consecutive Sequence.
  • Memory Usage: 40.8 MB, less than 8.62% of Java online submissions for Longest Consecutive Sequence.

Resources

Some Differences between British and American English

England and America are two countries divided by a common language. — George Bernard Shaw

vocabulary

American English British English
airplane aeroplane
anyplace, anywhere anywhere
apartment flat, apartment
area code dialing code (phone)
attorney, lawyer barrister, solicitor, lawyer
bar pub
billfold wallet
busy engaged (phone)
cab taxi
call collect reverse the charges (phone)
can tin, can
candy sweets
cellphone mobile
check/bill bill (in a restaurant)
coin-purse purse
cookie, cracker biscuit
corn sweet corn, maize
crib cot
crazy mad
crosswalk pedestrian, zebra crossing
cuffs turn-ups (on trousers)
diaper nappy
doctor’s office doctor’s surgery
drugstore chemist
dumb, stupid stupid
elevator lift
eraser rubber, eraser
fall, autumn autumn
faucet, tap tap (indoors)
first floor, second floor etc ground floor, first floor etc
flashlight torch
flat (tire) flat tyre, puncture
french fries chips
garbage, trash rubbish
garbage can, trashcan dustbin, rubbish bin
gas(oline) Petrol
gear shift gear lever (on a car)
highway, freeway main road, motorway
hood bonnet (of a car, at the front)
intersection crossroads
mad angry
mail post
mean nasty
movie, film film
movies cinema
one-way (ticket) single (ticket)
pants, trousers trousers
parking lot car park
pavement road surface
pitcher jug
pocketbook, purse, handbag handbag
(potato) chips crisps
railroad railway
raise rise (in salary)
rest room, bathroom (public) toilet
resume cv
round trip return (journey / ticket)
schedule, timetable timetable
shorts pants
sidewalk pavement
sneakers trainers (=sports shoes)
spigot, faucet tap (outdoors)
stand in line queue
stingy mean (opposite of generous)
store, shop shop
subway underground, tube (train)
truck van, lorry
trunk boot (of a car, at the back)
two weeks fortnight, two weeks
undershirt vest
vacation holiday(s)
windshield windscreen (on a car)
zee zed (the name of the letter z)
zip code postcode
zipper zip

spelling

American English British English
center centre
analyze analyse
tire tyre(on a wheel)
neighbor neighbour

Resources

  • Practical English Usage, by Michael Swan
  • Learning English as a Foreign Language FOR DUMmIES, By Gavin Dudeney and Nicky Hockly

LeetCode - Algorithms - 48. Rotate Image

Done by myself. Aha following step with 867. Transpose Matrix. After I solved it, I found this In-place rotate matrix by 90 degrees in clock-wise direction on web. Interesting. It is likely to be an elegant solution to this problem. “There is no algorithm for creativity.”, as Andy Hargreaves had ever said.

Problem

48. Rotate Image

Java

1

transpose and then swap columns symmetrically

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public void rotate(int[][] matrix) {
int N = matrix.length;
int tmp;
for(int i=0; i<N; i++) {
for(int j=0; j<i; j++) {
tmp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = tmp;
}
}
for(int i=0; i<N; i++) {
for(int j=0; j<N/2; j++) {
tmp = matrix[i][j];
matrix[i][j] = matrix[i][N-1-j];
matrix[i][N-1-j] = tmp;
}
}
}
}

Submission Detail

  • 21 / 21 test cases passed.
  • Runtime: 0 ms, faster than 100.00% of Java online submissions for Rotate Image.
  • Memory Usage: 39.4 MB, less than 5.77% of Java online submissions for Rotate Image.

2

solution of www.geeksforgeeks.org

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public void rotate(int[][] matrix) {
int N = matrix.length;
int tmp;
for(int i=0; i<N/2; i++) {
for(int j=i; j<N-i-1; j++) {
tmp = matrix[i][j];
matrix[i][j] = matrix[N-1-j][i];
matrix[N-1-j][i] = matrix[N-1-i][N-1-j];
matrix[N-1-i][N-1-j] = matrix[j][N-1-i];
matrix[j][N-1-i] = tmp;
}
}
}
}

Submission Detail

  • 21 / 21 test cases passed.
  • Runtime: 0 ms, faster than 100.00% of Java online submissions for Rotate Image.
  • Memory Usage: 39.4 MB, less than 5.77% of Java online submissions for Rotate Image.

non in-place method

matrix rotate

1
2
3
4
5
6
7
8
9
10
public int[][] rotate_math_stackexchange(int[][] matrix) {
int N = matrix.length;
int[][] R = new int[N][N];
for(int i=0; i<N; i++) {
for(int j=0; j<N; j++) {
R[i][j] = matrix[N-1-j][i];
}
}
return R;
}

JavaScript

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
* @param {number[][]} matrix
* @return {void} Do not return anything, modify matrix in-place instead.
*/
var rotate = function(matrix) {
var n = matrix.length;
var tmp;
for(var i=0; i<n; i++) {
for(var j=0; j<i; j++) {
tmp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = tmp;
}
}
for(var i=0; i<n; i++) {
for(var j=0; j<n/2; j++) {
tmp = matrix[i][j];
matrix[i][j] = matrix[i][n-1-j];
matrix[i][n-1-j] = tmp;
}
}
};

Submission Detail

  • 21 / 21 test cases passed.
  • Runtime: 56 ms, faster than 57.20% of JavaScript online submissions for Rotate Image.
  • Memory Usage: 33.4 MB, less than 100.00% of JavaScript online submissions for Rotate Image.

Algorithms Quotes

An algorithm must be seen to be believed. — Donald Knuth

For me, great algorithms are the poetry of computation. Just like verse, they can be terse, allusive, dense, and even mysterious. But once unlocked, they cast a brilliant new light on some aspect of computing. — Francis Sullivan

I will, in fact, claim that the difference between a bad programmer and a good one is whether he considers his code or his data structures more important. Bad programmers worry about the code. Good programmers worry about data structures and their relationships.
Linus Torvalds

Algorithms + Data Structures = Programs. — Niklaus Wirth

Fancy algorithms are slow when N is small, and N is usually small. — Rob Pike

An algorithm is like a recipe. — Muhammad Waseem

The Euclidean algorithm is the granddaddy of all algorithms, because it is the oldest nontrivial algorithm that has survived to the present day. — Donald Knuth

The best programs are written so that computing machines can perform them quickly and so that human beings can understand them clearly. A programmer is ideally an essayist who works with traditional aesthetic and literary forms as well as mathematical concepts, to communicate the way that an algorithm works and to convince a reader that the results will be correct. — Donald Knuth