Skip to content

作业 #347

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
36 changes: 36 additions & 0 deletions Week_02/id_12/242_12
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class Solution {

/**
* @param String $s
* @param String $t
* @return Boolean
*/
function isAnagram($s, $t) {
$ttemp = $t;
$sLen = strlen($s);
$tLen = strlen($t);
if($sLen != $tLen){
return false;
}
$sbool = $tbool = true;
for($i=0;$i<$sLen;$i++){
if(stripos($t, $s[$i]) === false){
$sbool = false;
break;
}
$t = preg_replace("/$s[$i]/", '', $t, 1);
}
for($j=0;$j<$tLen;$j++){
if(stripos($s, $ttemp[$j]) === false){
$tbool = false;
break;
}
$s = preg_replace("/$ttemp[$j]/", '', $s, 1);
}
if($sbool && $tbool){
return true;
}else{
return false;
}
}
}
31 changes: 31 additions & 0 deletions Week_02/id_12/671_12
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int findSecondMinimumValue(TreeNode root) {
return traversal(root,root.val);
}

private int traversal(TreeNode root,int value){
if(root == null){
return -1;
}
if(root.val > value){
return root.val;
}
int l = traversal(root.left,value);
int r = traversal(root.right,value);

if(l>=0 && r>=0){
return Math.min(l,r);
}
return Math.max(l,r);
}

}
15 changes: 15 additions & 0 deletions Week_03/id_12/104_12
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int maxDepth(TreeNode root) {
if(root == null){return 0;}
return Math.max(maxDepth(root.left)+1, maxDepth(root.right)+1);
}
}
31 changes: 31 additions & 0 deletions Week_03/id_12/997_12
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Solution {

/**
* @param Integer $N
* @param Integer[][] $trust
* @return Integer
*/
function findJudge($N, $trust) {
if(count($trust)==1){
return $trust[0][1];
}
if(count($trust)==0){
return 1;
}

$countArr = [];
foreach($trust as $key=>$val){
$countArr[$val[0]] = -1;
if($countArr[$val[1]] !=-1){
$countArr[$val[1]]++;
}
}

for($i=0;$i<=$N;$i++){
if($countArr[$i] == $N-1){
return $i;
}
}
return -1;
}
}
19 changes: 19 additions & 0 deletions Week_04/id_12/LeetCode_169_12.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {

/**
* @param Integer[] $nums
* @return Integer
*/
function majorityElement($nums) {
$numsTem = $maxArr = array();
foreach($nums as $key=>$val){
if(in_array($val,$numsTem)){
$maxArr[$val]++;
}else{
$numsTem[$val] = $val;
$maxArr[$val] = 1;
}
}
return array_search(max($maxArr),$maxArr);
}
}
24 changes: 24 additions & 0 deletions Week_04/id_12/LeetCode_455_12.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution {

/**
* @param Integer[] $g
* @param Integer[] $s
* @return Integer
*/
function findContentChildren($g, $s) {

asort($g);
asort($s);
$g = array_values($g);
$s = array_values($s);
$child = $cake = 0;
while($child < count($g) && $cake < count($s)){
if($g[$child] <= $s[$cake]){
$child++;
var_dump($child);
}
$cake++;
}
return $child;
}
}
24 changes: 24 additions & 0 deletions Week_04/id_12/LeetCode_720_12.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution {

/**
* @param String[] $words
* @return String
*/
function longestWord($words) {

asort($words);
$arrTem = array();
$res = '';
foreach($words as $key=>$val){
$strLen = strlen($val);
$strTem = substr($val,0,$strLen-1);
if($strLen == 1 || in_array($strTem,$arrTem)){
$arrTem[$key] = $val;
$res = $strLen > strlen($res) ? $val : $res;
}
}
return $res;

}

}