leetCode
Laiyong Wang Lv6
  1. 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    class Solution {

    /**
    * @param Integer[] $nums
    * @return Integer
    */
    function singleNumber($nums) {
    $arr = [];
    foreach($nums as $key => $val){
    $arr[$val] += 1;
    }
    return array_search(1,$arr);

    }
    }
  2. 给定一个大小为 n 的数组 nums ,返回其中的多数元素。多数元素是指在数组中出现次数 大于 ⌊ n/2 ⌋ 的元素
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    class Solution {

    /**
    * @param Integer[] $nums
    * @return Integer
    */
    function majorityElement($nums) {
    $length = count($nums);
    $result = [];
    foreach($nums as $val){
    if(array_key_exists($val,$result)){
    $result[$val]++;
    } else {
    $result[$val] = 1;
    }
    }
    foreach($result as $k=>$v){
    if($v > $length/2){
    return $k;
    }
    }
    }
    }
  3. 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target 。该矩阵具有以下特性:每行的元素从左到右升序排列,每列的元素从上到下升序排。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    class Solution {

    /**
    * @param Integer[][] $matrix
    * @param Integer $target
    * @return Boolean
    */
    function searchMatrix($matrix, $target) {
    $total = count($matrix) - 1;//行
    $line = 0;
    while(true){
    if (!isset($matrix[$total][$line])) return false;
    if ($matrix[$total][$line] == $target){
    return true;
    }elseif($matrix[$total][$line] < $target){
    $line++;
    } else {
    $total--;
    }
    }
    return false;
    }
    }
$li-margin-bottom = 0.8rem $post-tool-button-width = 2.5rem .post-tools-container { padding-top var(--component-gap) .post-tools-list { li { margin-bottom $li-margin-bottom &:last-child { margin-bottom 0 } } li.tools-item { position relative box-sizing border-box width $post-tool-button-width height $post-tool-button-width color var(--text-color-3) font-size 1.2rem background var(--background-color-1) border-radius 50% box-shadow 2px 2px 5px var(--shadow-color) cursor pointer &:hover { box-shadow 2px 2px 8px var(--shadow-hover-color) } i { color var(--text-color-3) } &:hover { color var(--background-color-1) background var(--primary-color) i { color var(--background-color-1) !important } } &.toggle-show-toc { display none } &.go-to-comments { .post-comments-count { position absolute top 0 right -1rem display none align-items center justify-content center box-sizing border-box min-width 1.1rem height 1.1rem padding 0 0.2rem color var(--badge-color) font-size 12px background var(--badge-background-color) border-radius 0.4rem +keep-tablet() { display none !important } } } } li.status-item { width $post-tool-button-width height $post-tool-button-width color var(--text-color-3) font-size 1.6rem cursor pointer &.post-lock { cursor default .fa-lock-open { display none color var(--keep-success-color) } &.decrypt { cursor pointer .fa-lock-open { display block } .fa-lock { display none } } } } } }