算法練習之合併兩個有序鏈表, 刪除排序數組中的重複項,移除元素,實現strStr(),搜索插入位置,無重複字符的最長子串

最近在學習java,可是對於數據操做那部分仍是不熟悉php

所以決定找幾個簡單的算法寫,用php和java分別實現java

1.合併兩個有序鏈表算法

將兩個有序鏈表合併爲一個新的有序鏈表並返回。新鏈表是經過拼接給定的兩個鏈表的全部節點組成的。 數組

示例:函數

輸入:1->2->4, 1->3->4
輸出:1->1->2->3->4->4

java學習

/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */
class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode rs = new ListNode(0); ListNode point = rs; while (l1!=null && l2!=null) { if(l1.val<l2.val){ point.next = l1; point = point.next; l1 = l1.next; }else{ point.next = l2; point = point.next; l2 = l2.next; } } if(l1==null){ point.next = l2; }else{ point.next = l1; } return rs.next; } }

php測試

/** * Definition for a singly-linked list. * class ListNode { * public $val = 0; * public $next = null; * function __construct($val) { $this->val = $val; } * } */
class Solution { /** * @param ListNode $l1 * @param ListNode $l2 * @return ListNode */
    function mergeTwoLists($l1, $l2) { $rs    = new ListNode(0); $point = $rs; while (!empty($l1) && !empty($l2)) { if($l1->val<$l2->val){ $point->next = $l1; $point = $point->next; $l1 = $l1->next; }else{ $point->next = $l2; $point = $point->next; $l2 = $l2->next; } } if($l1==null){ $point->next = $l2; }else{ $point->next = $l1; } return $rs->next; } }

測試this

$l1        = new ListNode(5); $l11       = new ListNode(2); $l1->next  = $l11; $l12       = new ListNode(4); $l11->next = $l12; $l2        = new ListNode(1); $l21       = new ListNode(2); $l2->next  = $l21; $l22       = new ListNode(4); $l21->next = $l22; $aa = new Solution(); $rs = $aa->mergeTwoLists($l1, $l2);

2.刪除排序數組中的重複項spa

給定一個排序數組,須要在原地刪除重複出現的元素,使得每一個元素只出現一次,返回移除後數組的新長度。code

不要使用額外的數組空間,必須在原地修改輸入數組並在使用 O(1) 額外空間的條件下完成。

示例 1: 給定數組 nums = [1,1,2], 函數應該返回新的長度 2, 而且原數組 nums 的前兩個元素被修改成 1, 2。 不須要考慮數組中超出新長度後面的元素。
示例
2: 給定 nums = [0,0,1,1,1,2,2,3,3,4], 函數應該返回新的長度 5, 而且原數組 nums 的前五個元素被修改成 0, 1, 2, 3, 4。 不須要考慮數組中超出新長度後面的元素。

java

class Solution { public int removeDuplicates(int[] nums) { int i=0; for(int n :nums){ if(i<1||n>nums[i-1]){ nums[i++] = n; } } return i; } }

php

class Solution { /** * @param Integer[] $nums * @return Integer */
    function removeDuplicates(&$nums) { $i = 0; foreach ($nums as $key => $value) { if($i<1||$value>$nums[$i-1]){ $nums[$i++] = $value; } } return $i; } }

說明:

  數組是排序以後的,所以移動的爲

    a.第一個元素,指向它

    b.比指向的數大的元素

3.移除元素

給定一個數組 nums 和一個值 val,須要原地移除全部數值等於 val 的元素,返回移除後數組的新長度。

不要使用額外的數組空間,必須在原地修改輸入數組並在使用 O(1) 額外空間的條件下完成。

元素的順序能夠改變。不須要考慮數組中超出新長度後面的元素。

示例 1: 給定 nums = [3,2,2,3], val = 3, 函數應該返回新的長度 2, 而且 nums 中的前兩個元素均爲 2。 不須要考慮數組中超出新長度後面的元素。
示例
2: 給定 nums = [0,1,2,2,3,0,4,2], val = 2, 函數應該返回新的長度 5, 而且 nums 中的前五個元素爲 0, 1, 3, 0, 4。 注意這五個元素可爲任意順序。 不須要考慮數組中超出新長度後面的元素。

java

class Solution { public int removeElement(int[] nums, int val) { int i=0; for(int n:nums){ if(val!=n){ nums[i++] = n; } } return i; } }

php

class Solution { /** * @param Integer[] $nums * @param Integer $val * @return Integer */
    function removeElement(&$nums, $val) { $i = 0; foreach ($nums as $key => $value) { if($value!=$val){ $nums[$i++] = $value; } } return $i; } }

4.實現strStr()

實現 strStr() 函數。

給定一個 haystack 字符串和一個 needle 字符串,在 haystack 字符串中找出 needle 字符串出現的第一個位置 (從0開始)。若是不存在,則返回  -1。

示例 1: 輸入: haystack = "hello", needle = "ll" 輸出: 2
示例 2: 輸入: haystack = "aaaaa", needle = "bba" 輸出: -1

java

class Solution { public int strStr(String haystack, String needle) { if(needle.isEmpty()||needle.equals(haystack)) return 0; int l=needle.length(); int r = haystack.length()-l; for(int i=0;i<r+1;i++){ String tempStr=haystack.substring(i,l+i); if(tempStr.equals(needle)) return i; } return -1; } }

php

class Solution { /** * @param String $haystack * @param String $needle * @return Integer */
    function strStr($haystack, $needle) { if(empty($needle)||$needle ==$haystack) return 0; $l=strlen($needle); $r = strlen($haystack)-$l; for($i=0;$i<$r+1;$i++){ $tempStr=substr($haystack,$i, $l); if($tempStr==$needle) return $i; } return -1; } }

5.搜索插入位置

給定一個排序數組和一個目標值,在數組中找到目標值,並返回其索引。若是目標值不存在於數組中,返回它將會被按順序插入的位置。

你能夠假設數組中無重複元素。

示例 1: 輸入: [1,3,5,6], 5 輸出: 2
示例 2: 輸入: [1,3,5,6], 2 輸出: 1
示例 3: 輸入: [1,3,5,6], 7 輸出: 4
示例 4: 輸入: [1,3,5,6], 0 輸出: 0

java

class Solution { public int searchInsert(int[] nums, int target) { int i = 0; for(;i<nums.length;i++){ if(target<=nums[i])break; } return i; } }

php

class Solution { /** * @param Integer[] $nums * @param Integer $target * @return Integer */
    function searchInsert($nums, $target) { $i=0; for($i=0;$i<count($nums);$i++){ if($target<=$nums[$i]) break; } return $i; } }

 6.無重複字符的最長子串

給定一個字符串,找出其中不含有重複字符的 最長子串 的長度。

示例 1: 輸入: "abcabcbb" 輸出: 3 解釋: 由於無重複字符的最長子串是 "abc",因此其長度爲 3。 示例 2: 輸入: "bbbbb" 輸出: 1 解釋: 由於無重複字符的最長子串是 "b",因此其長度爲 1。 示例 3: 輸入: "pwwkew" 輸出: 3 解釋: 由於無重複字符的最長子串是 "wke",因此其長度爲 3。 請注意,答案必須是 子串 的長度,"pwke" 是一個子序列,不是子串。

java

class Solution { public int lengthOfLongestSubstring(String s) { int longSub = 0; if(s.isEmpty()) return 0; if(s.length()==1) return 1; char[] arr = s.toCharArray(); String str = String.valueOf(arr[0]); longSub = 1; for(int i=1;i<s.length();i++){ int pos = str.indexOf(arr[i]); if (pos !=-1) { str = str.substring(pos+1)+arr[i]; }else{ str+=arr[i]; } if(str.length()>longSub) longSub = str.length(); } return longSub; } }

php

class Solution { /** * @param String $s * @return Integer */
    function lengthOfLongestSubstring($s) { $len     = strlen($s); if($len==0) return 0; $str     = $s[0]; $longSub = 1; for ($i = 1; $i < $len; $i++) { $pos = strpos($str, $s[$i]); if ($pos === false) { $str .= $s[$i]; } else { $str = substr($str, $pos + 1) . $s[$i]; } $tmp = strlen($str); if ($longSub < $tmp) { $longSub = $tmp; } } return $longSub; } }
相關文章
相關標籤/搜索