Skip to content

Instantly share code, notes, and snippets.

View rdtr's full-sized avatar

Norio Akagi rdtr

View GitHub Profile
@rdtr
rdtr / algorithm_math_string_to_integer_atoi.java
Created December 21, 2015 21:12
algorithm_math_string_to_integer_atoi.java
public class Solution {
public int myAtoi(String str) {
int start = 0;
while (start < str.length() && str.charAt(start) == ' ') ++start;
if (start == str.length()) return 0;
int isPositive = 1;
if (str.charAt(start) == '-') {
isPositive = -1;
++start;
@rdtr
rdtr / algorithm_math_string_to_integer_atoi.py
Last active December 21, 2015 20:21
algorithm_math_string_to_integer_atoi.py
class Solution(object):
def myAtoi(self, str):
str = str.strip()
negative = 1
res = 0
for i, ch in enumerate(str):
if i == 0 and ch == '-':
negative = -1
continue
@rdtr
rdtr / algorithm_math_reverse_integer.java
Last active December 21, 2015 20:38
algorithm_math_reverse_integer.java
public class Solution {
public int reverse(int x) {
int isPositive = 1;
if (x < 0) {
isPositive = -1;
x = -1 * x;
}
int sum = 0;
while (x > 0) {
@rdtr
rdtr / algorithm_math_reverse_integer.py
Last active December 21, 2015 20:39
algorithm_math_reverse_integer.py
class Solution(object):
INTEGER_MAX = 2**31
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
isPositive = 1
if x < 0:
@rdtr
rdtr / algorithm_string_longest_palindromic_substring.java
Created December 13, 2015 09:44
algorithm_string_longest_palindromic_substring.java
public class Solution {
public String longestPalindrome(String s) {
String res = "";
int len = s.length();
for (int i = 0; i < len; i++) {
if (2 * (len - i - 1) + 1 < res.length()) break;
String res1 = extendStr(s, i, i);
String res2 = extendStr(s, i, i+1);
@rdtr
rdtr / algorithm_string_longest_palindromic_substring.py
Created December 13, 2015 07:54
algorithm_string_longest_palindromic_substring.py
class Solution(object):
def longestPalindrome(self, s):
ml, mr, mlen = 0, 0, 0
ls = len(s)
for i in range(ls):
if 2*(ls-i-1)+1 < mlen: break
s1l, s1r = self.extendStr(s, i, i)
s2l, s2r = self.extendStr(s, i, i + 1)
@rdtr
rdtr / algorithm_search_median_of_two_sorted_arrays.java
Created December 8, 2015 08:36
algorithm_search_median_of_two_sorted_arrays.java
public class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int len1 = nums1.length;
int len2 = nums2.length;
int mid = (len1 + len2) / 2;
double res = 0.0;
res = 1.0 * findKthSmallest(nums1, 0, len1-1, nums2, 0, len2-1, mid);
if ((len1+len2) % 2 == 0) {
double res2 = 1.0 * findKthSmallest(nums1, 0, len1-1, nums2, 0, len2-1, mid-1);
@rdtr
rdtr / algorithm_search_median_of_two_sorted_arrays.py
Created December 8, 2015 08:34
algorithm_search_median_of_two_sorted_arrays.py
class Solution(object):
def findMedianSortedArrays(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: float
"""
len1, len2 = len(nums1), len(nums2)
leng = len1 + len2
mid = leng / 2
@rdtr
rdtr / algorithm_tree_lowest_common_ancestor_of_a_binary_tree_2.java
Created November 18, 2015 11:29
algorithm_tree_lowest_common_ancestor_of_a_binary_tree_2.java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
@rdtr
rdtr / algorithm_tree_lowest_common_ancestor_of_a_binary_tree_1.java
Created November 18, 2015 10:12
algorithm_tree_lowest_common_ancestor_of_a_binary_tree_1.java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {