leetcode: Longest Palindromic Substring

  • 问题

Given a string s, return the longest palindromic substring in s.

  • 代码
class Solution {

    /**
     * @param String $s
     * @return String
     */
    function longestPalindrome($s) {
        $len = strlen($s);
        if ($len === 1) {
            return $s;
        }

        if ($len === 2) {
            if ($s[0] === $s[1]) {
                return $s;
            }

            return $s[0];
        }

        $palindromic = $s[0];
        $max = 1;
        for($i = 0; $i < $len; $i++) {
            if (strlen(substr($s, $i)) < $max) {
                break;
            }
            
            for($j = $len - $i - 1; $j >= 0; $j--) {
                if ($j + 1 < $max) {
                    break;
                }

                $str = substr($s, $i, $j + 1);
                $sub = strrev($str);
                if ($sub === $str) {
                    if($max < strlen($sub)) {
                        $palindromic = $sub;
                        $max = strlen($sub);
                    }

                    break;
                }
            }
        }

        return $palindromic;
    }
}
  • 结果

11510/11510 cases passed (90 ms)
Your runtime beats 9.13 % of php submissions
Your memory usage beats 99.87 % of php submissions (15.1 MB)

leetcode: Median of Two Sorted Arrays

  • 问题

Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.

The overall run time complexity should be O(log (m+n)).

  • 代码
class Solution {

    /**
     * @param Integer[] $nums1
     * @param Integer[] $nums2
     * @return Float
     */
    function findMedianSortedArrays($nums1, $nums2) {
        $nums = array_merge($nums1, $nums2);
        $len = count($nums);
        $nums = $this->bubbleSort($nums, $len);

        if ($len % 2) {
            return $nums[floor($len / 2)];
        }

        return ($nums[$len / 2] + $nums[$len / 2 - 1]) / 2;
    }

    function bubbleSort($arr, $n)
    {
        for($i = 0; $i < $n; $i++)
        {
            $swapped = false;
            for ($j = 0; $j < $n - $i - 1; $j++)
            {
                if ($arr[$j] > $arr[$j+1])
                {
                    $t = $arr[$j];
                    $arr[$j] = $arr[$j+1];
                    $arr[$j+1] = $t;
                    $swapped = true;
                }
            }

            if($swapped === false) {
                break;
            }
        }

        return $arr;
    }
}

leetcode: String to Integer (atoi)

class Solution
{

    /**
     * @param String $s
     * @return Integer
     */
    public function myAtoi($s)
    {
        $s = ltrim($s);
        if (empty($s)) {
            return 0;
        }

        $ascii = ord($s[0]);
        $sign  = $ascii === 45;
        if ($ascii === 45 || $ascii === 43) {
            $sum = 0;
        } elseif ($ascii >= 48 && $ascii <= 57) {
            $sum = intval($s[0]);
        } else {
            return 0;
        }

        for ($i = 1, $len = strlen($s); $i < $len; ++$i) {
            $ascii = ord($s[$i]);
            if ($ascii < 48 || $ascii > 57) {
                break;
            }

            $sum = $sum * 10 + intval($s[$i]);
        }

        if ($sign) {
            $sum *= -1;
        }

        $boundry = pow(2, 31);
        if ($sum <= -$boundry) {
            return -$boundry;
        } elseif ($sum >= --$boundry) {
            return $boundry;
        }

        return $sum;
    }
}

leetcode: Palindrome Number

class Solution {

    /**
     * @param Integer $x
     * @return Boolean
     */
    function isPalindrome($x) {
        if($x >= 0 && $x < 10){
            return true;
        }

        if ($x < 0 || $x % 10 === 0) {
            return false;
        }


        return $this->reverse($x) === $x;
    }

    function reverse($x){
        $sum = 0;
        while($x >= 10){
            $last = $x % 10;
            $x = intval($x / 10);
            $sum = $sum * 10 + $last;
        }

        return $sum * 10 + $x;
    }
}

leetcode: Reverse Integer

class Solution
{

    /**
     * @param Integer $x
     * @return Integer
     */
    public function reverse($x)
    {
        if ($x < 10 && $x > -10) {
            return $x;
        }

        $reverse = 0;
        $unsign  = $x >= 0;
        $x       = abs($x);
        do {
            $last    = $x % 10;
            $reverse = $reverse * 10 + $last;
            $x       = intval($x / 10);
        } while ($x >= 10);
        $reverse = $reverse * 10 + $x;
        if (!$unsign) {
            $reverse = -$reverse;

            return $reverse < -(pow(2, 31)) ? 0 : $reverse;
        }

        return $reverse > (pow(2, 31) - 1) ? 0 : $reverse;
    }
}

leetcode: ZigZag Conversion

/*
 * @lc app=leetcode id=6 lang=php
 *
 * [6] ZigZag Conversion
 */

// @lc code=start
class Solution {

    /**
     * @param String $s
     * @param Integer $numRows
     * @return String
     */
    function convert($s, $numRows) {
        $len = strlen($s);
        if ($len === 1 || $numRows === 1) {
            return $s;
        }

        $max = 2 * $numRows - 2;
        
        $str = '';
        // 0 4 8 12
        // 1 3 5 7 9 11 13
        // 2 6 10

        // 0   6    12
        // 1 5 7 11 13
        // 2 4 8 10
        // 3   9    [15]

        // P   H
        // A S I
        // Y I R
        // P L I G
        // A   N
        // 0   8
        // 1 7 9
        // 2 6 10
        // 3 5 11 13
        // 4   12
        
        //ABCDEFGHIJKLMNOPQRSTUVWXYZ
        //A   E   I   M   Q   U   Y
        //B D F H J L N P R T V X Z
        //C   G   K   O   S   W
        //0   4   8    12    16 --- 4
        //1 3 5 7 9 11 13 15 17 --- 2
        //2   6   10   14    18 --- 4
        
        //A   G   M
        //B F H L N R
        //C E I K O Q
        //D   J   P
        //0   6    12    18    6
        //1 5 7 11 13 17 19    4
        //2 4 8 10 14 16 20    2
        //3   9    15    21

        //A   I   Q
        //B H J P R
        //C G K O S
        //D F L N T
        //E   M   U
        //0   8     16    24    8
        //1 7 9  15 17 23 25    6 2 6 2
        //2 6 10 14 18 22       4 4 4 4
        //3 5 11 13 19 21       2 6 2 6
        //4   12    20          8

        //A   K   U
        //B J L T V
        //C I M S W
        //D H N R
        //E G O Q
        //F   P
        //0   10    20   10
        //1 9 11 19 21   8 2 8 2
        //2 8 12 18 22   6 4 6 4
        //3 7 13 17 23   4 6 4 6
        //4 6 14 16 24   2 8 2 8
        //5   15    25   10
        $arr[] = [0, $max];
        $j = 2;
        while($max - $j > 0) {
            $arr[] = [$max - $j, $j];
            $j += 2;
        }
        $arr[] = [0, $max];

        for ($i = 0; $i < $numRows; $i++) {
            $str .= $s[$i];
            $idx = $i;
            while(true) {
                foreach($arr[$i] as $val) {
                    $idx += $val;
                    if($idx >= $len) {
                        break 2;
                    }
                    
                    if (!$val) {
                        continue;
                    }

                    $str .= $s[$idx];
                }
            }
        }

        return $str;
    }
}
// @lc code=end

leetcode: longest substring without repeating characters

  • 问题

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

You can return the answer in any order.

  • leetcode类别

中等

  • 代码
class Solution {

    /**
     * @param String $s
     * @return Integer
     */
    function lengthOfLongestSubstring($s) {
        $len1 = strlen($s);
        if (!$len1) {
            return $len1;
        }

        $max = 1;
        $p = [];
        for($i = 0, $j = 0; $i < $len1; $i++) {
            $code = ord($s[$i]);
            if (!array_key_exists($code, $p)) {
                $p[$code] = $i;
                continue;
            }
            
            $strlen = $i - $j;
            $max = $strlen > $max ? $strlen : $max;
            $i = $p[$code];
            $j = $i + 1;
            $p = [];
        }
        
        $cnt = count($p);
        $max = $cnt > $max ? $cnt : $max;
        
        return $max;
    }
}

leetcode:add-two-numbers

  • 问题

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

You can return the answer in any order.

  • leetcode类别

中等

  • 代码
/**
 * Definition for a singly-linked list.
 * class ListNode {
 *     public $val = 0;
 *     public $next = null;
 *     function __construct($val = 0, $next = null) {
 *         $this->val = $val;
 *         $this->next = $next;
 *     }
 * }
 */
class Solution {

    /**
     * @param ListNode $l1
     * @param ListNode $l2
     * @return ListNode
     */
    function addTwoNumbers($l1, $l2) {
        return $this->reverseList($this->getNode($l1, $l2));
    }

    function reverseList($list) {
        $prev = null;
        while($list !== null) {
            $node = new ListNode($list->val, null);
            $node->next = $prev;

            $prev = $node;
            $list = $list->next;
        }

        return $node;
    }

    function getNode($l1, $l2) {
        $prev = null;
        $plus = 0;
        
        while(true) {
            if ($l1 === null && $l2 === null && !$plus) {
                break;
            }

            $val = 0;
            if ($l1 !== null) {
                $val += $l1->val;
            }

            if ($l2 !== null) {
                $val += $l2->val;
            }

            $val += $plus;

            $plus = 0;
            if ($val > 9) {
                $val = $val % 10;
                $plus = 1;
            }
            
            $node = new ListNode($val, null);
            $node->next = $prev;

            $prev = $node;
            
            if ($l1 !== null) {
                $l1 = $l1->next;
            }

            if ($l2 !== null) {
                $l2 = $l2->next;
            }
        }

        return $node;
    }
}

leetcode:two-sum

  • 问题

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

You can return the answer in any order.

  • leetcode类别

简单

  • 代码
class Solution {

    /**
     * @param Integer[] $nums
     * @param Integer $target
     * @return Integer[]
     */
    function twoSum($nums, $target) {
        $len = count($nums);

        for ($i = 0; $i < $len - 1; ++$i) {
            $sub = $target - $nums[$i];
            for ($j = $i + 1; $j < $len; ++$j) {
                if ($nums[$j] === $sub) {
                    return [$i, $j];
                }
            }
        }

        return [];
    }
}

max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

Elasticsearch在启动时出现错误:

max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

这个问题是虚拟内存设置问题。在虚拟内存有说明:Elasticsearch默认使用mmapfs类型存储文档。对mmapfs的介绍,Elasticsearch文档也有说明。系统默认是65530,对Elasticsearch远远不够。

  • 解决办法

修改/etc/sysctl.conf,如果文件内有vm.max_map_count项,那么修改值大于或等于报错值262144。如果没有,在末尾追加一行。

修改完之后再终端执行sysctl --system或者sysctl -p使修改生效。检查是否生效可以使用sysctl vm.max_map_count查看。