diff --git a/Week_02/id_12/242_12 b/Week_02/id_12/242_12 new file mode 100644 index 00000000..28529939 --- /dev/null +++ b/Week_02/id_12/242_12 @@ -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; + } + } +} diff --git a/Week_02/id_12/671_12 b/Week_02/id_12/671_12 new file mode 100644 index 00000000..df50f41d --- /dev/null +++ b/Week_02/id_12/671_12 @@ -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); + } + +} diff --git a/Week_03/id_12/104_12 b/Week_03/id_12/104_12 new file mode 100644 index 00000000..98a17303 --- /dev/null +++ b/Week_03/id_12/104_12 @@ -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); + } +} diff --git a/Week_03/id_12/997_12 b/Week_03/id_12/997_12 new file mode 100644 index 00000000..35d36be4 --- /dev/null +++ b/Week_03/id_12/997_12 @@ -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; + } +} diff --git a/Week_04/id_12/LeetCode_169_12.php b/Week_04/id_12/LeetCode_169_12.php new file mode 100644 index 00000000..ebce86ce --- /dev/null +++ b/Week_04/id_12/LeetCode_169_12.php @@ -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); + } +} diff --git a/Week_04/id_12/LeetCode_455_12.php b/Week_04/id_12/LeetCode_455_12.php new file mode 100644 index 00000000..342cf98a --- /dev/null +++ b/Week_04/id_12/LeetCode_455_12.php @@ -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; + } +} diff --git a/Week_04/id_12/LeetCode_720_12.php b/Week_04/id_12/LeetCode_720_12.php new file mode 100644 index 00000000..ea23eb8a --- /dev/null +++ b/Week_04/id_12/LeetCode_720_12.php @@ -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; + + } + +}