leetcode java开发基础算法(Easy)

Easy

1.TwoSum(easy)

/**
	 * 利用map来存储数组中的值以及对应的index,时间复杂度为O(n),空间复杂度为O(n)
	 * 再遍历一次map,如果map中包含有(target-key)的键,则返回这两个键的value,时间复杂度为O(n)
	 * 总的时间复杂度为O(n),空间复杂度为O(n)
	 * @param nums
	 * @param target
	 * @return
	 */
	public static int[] twoSum(int[] nums, int target) {
        Map> resultMap = new HashMap>();
        if(nums == null) {
        	return null;
        }
        for(int i = 0; i < nums.length; i++) {
        	if(! resultMap.containsKey(nums[i])) {
        		List list = new ArrayList();
        		list.add(i);
        		resultMap.put(nums[i], list);
        	}else {
        		List list = resultMap.get(nums[i]);
        		list.add(i);
        		resultMap.put(nums[i], list);
        	}
        }
        for(Entry> entry : resultMap.entrySet()) {
        	Integer i = entry.getKey();
        	Integer j = target-i;
        	if(resultMap.containsKey(j)) {
        		if(i.equals(j)) {
        			List list = resultMap.get(i);
        			return new int[] {list.get(0), list.get(1)};
        		}
        		return new int[] {resultMap.get(i).get(0), resultMap.get(j).get(0)};
        	}
        }
        
        return null;
        
        
    }

2.Reverse Integer

Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123
Output: 321

Example 2:

Input: -123
Output: -321

Example 3:

Input: 120
Output: 21
/**
	 * 时间复杂度为O(n),空间复杂度为O(1)
	 * 通过遍历x的每位数来实现逆序
	 * @param x
	 * @return
	 */
	public static int reverse(int x) {
		if(x == 0) {
			return 0;
		}
		int temp = x;
		if(temp < 0) {
			if(temp <= Integer.MIN_VALUE) {
				return 0;
			}
			temp = temp * (-1);
		}
		long result = 0;
		int time = 0;
		while(temp != 0) {
			int i = temp % 10;
			temp = (temp - i )/10;
			time++;
			if(i == 0 && time == 1) {
				continue;
			}
			if(result == 0 ) {
				result += i;
			}else {
				if((result* 10 + i) > Integer.MAX_VALUE) {
					result = 0;
					break;
				}else {
					result = result *10 + i;
				}
			}		
		}
		if(x < 0) {
			return (int) (result*-1);
		}
		return (int) result;        
    }

3. Palindrome Number

/**
	 * 实现复杂度为O(n),空间复杂度为O(n)
	 * @param x
	 * @return
	 */
	public boolean isPalindrome(int x) {
		if(x < 0) {
			return false;
		}
		if(x == 0 ) {
			return true;
		}
		List list = new ArrayList();
		while( x > 0) {
			int item = x % 10;
			x = (x - item)/10;
			list.add(item);
		}
		for(int i = 0, j = list.size()-1 ; i

4.Longest Common Prefix

/**
	 * 时间复杂度:O(n2)
	 *空间复杂度:O(n) 
	 * @param strs
	 * @return
	 */
	  public String longestCommonPrefix(String[] strs) {
			  if(strs == null || strs.length == 0) {
				  return "";
			  }
			  String s = strs[0];
			  List lists = new ArrayList();
			  int i = 0;
			  boolean exit = false;
			  if(s != null && ! s.equals("")) {
				  Character ch = s.charAt(i);
				  while(i < s.length()) {
					  for(String str : strs) {
						  if(i >= str.length() || str == null || str == "") {
							  exit = true;
							  break;
						  }
						  if(s.charAt(i) != str.charAt(i)) {
							  exit = true;
							  break;
						  }
					  }
					  if(exit) {
						  break;
					  }
					  lists.add(s.charAt(i++));
				  } 
			  }
			  
			  if(lists.size() == 0){
				  return "";
			  }
			  StringBuilder sb = new StringBuilder();
			  for(Character ch: lists) {
				  sb.append(ch);
			  }
		      return sb.toString();
	  }
	  

5.Valid Parentheses

/**
	 * 时间复杂度为O(n),空间复杂度为O(n)
	 * @param s
	 * @return
	 */
	public static boolean isValid(String s) {
		Stack stack = new Stack<>();
		for(int i = 0 ; i < s.length(); i++) {
			char ch = s.charAt(i);
			if(ch == '(' || ch == '[' || ch == '{') {
				stack.push(ch);
				continue;
			}
			
			if(stack.size() == 0) {
				return false;
			}
			
			
			if(ch == ')') {
				char item = stack.pop();
				if(item != '(') {
					return false;
				}
			}
			
			if(ch == ']') {
				char item = stack.pop();
				if(item != '[') {
					return false;
				}
			}
			
			if(ch == '}') {
				char item = stack.pop();
				if(item != '{') {
					return false;
				}
			}
			
		}
		
		if(stack.empty()){
            return true;
        }else {
        	return false;
        }
        
    }

Merge Two Sorted Lists

 /**
    *时间复杂度为O(n)
    *
    */
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        	if(l1 == null) {
	    		return l2;
	    	}
	    	
	    	if(l2 == null) {
	    		return l1;
	    	}
	    	
	    	int val;
	    	if(l1.val <= l2.val) {
	    		val = l1.val;
	    		l1 = l1.next;
	    	}else {
	    		val = l2.val;
	    		l2 = l2.next;
	    	}
            ListNode result = new ListNode(val);
	    	ListNode resultList = result;
	    	while(l1 != null && l2 != null) {
	    		if(l1.val <= l2.val) {
	    			ListNode node = new ListNode(l1.val);
	    			resultList.next = node;
	    			l1 = l1.next;
	    		}else {
	    			ListNode node = new ListNode(l2.val);
	    			resultList.next = node;
	    			l2 = l2.next;
	    		}
	    		resultList = resultList.next;
	    	}
	    	
	    	if(l1 != null) {
	    		resultList.next = l1;
	    	}
	    	if(l2 != null) {
	    		resultList.next = l2;
	    	}
	    	
	        return result;
    }

leetcode java开发基础算法(Easy)_第1张图片

你可能感兴趣的:(leetcode)