Insertion Sort List

Description Difficulty: 1.5/5.0 Description: https://oj.leetcode.com/problems/insertion-sort-list/ Solution class Solution { public: ListNode *insertionSortList(ListNode *head) { ListNode new_head(INT_MIN); new_head.next = head; ListNode * candidate = new_head.next; ListNode * end = &new_head; while(candidate){ if (candidate->val >= end->val){ // should be placed at the...

Median Of Two Sorted Arrays

double findMedianSortedArrays(int A[], int m, int B[], int n) { if ((m + n) % 2 != 0) // odd return (double) findKth(A, B, (m + n) / 2, 0, m - 1, 0, n - 1); else { //...

sqrt

Description https://oj.leetcode.com/submissions/detail/13480861/ Difficulty: 2.5/5.0 Solution Here we use Newton Method to solve this problem. Please refer to http://en.citizendium.org/wiki/Newton%27s_method#Computational_complexity. class Solution { public: int sqrt(int x) { double est = 1; while(fabs(est * est - x) >= 1) est = (est...

Word Break

layout: post title: Word Break comments: true category: Algorithms tag: algorithm, leetcode, search, memorization — Description https://oj.leetcode.com/problems/word-break/ Difficulty: 3.0/5.0 Solution Once we have started from a position and come back to this position with the anwser “NO” for matching, we...

Reverse Words In A String

layout: post title: Reverse Words in a String comments: true category: Algorithms tag: algorithm, leetcode — Description https://oj.leetcode.com/problems/reverse-words-in-a-string/ Difficulty: 2.0/5.0 Solution We can do an in-place reversal by First, reverse the whole string Second, reverse the individual string In this...