码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode 边做边记录

时间:2015-08-04 11:04:36      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:

其实就是把有道云笔记的东西搬过来,

不定期会更新

 

随便写写的东西,很多都需要优化,

如果有人看到,希望能给点建议,谢谢!

 

1.Palindrome Number

  public class Solution {
    public boolean isPalindrome(int x) {
        int y = 0;
        int tmp = x;
        if(x < 0){tmp = x*(-1);}
        while(tmp != 0){
            y = y*10 + tmp%10;
            tmp = tmp/10;
        }
        if(x == y){
            return true;
        }
        return false;
    }
}

  

思路是把原来的数不断取10的模,然后加在另外一个数上
 
2.最后一个string的长度
 
description:

Given a string s consists of upper/lower-case alphabets and empty space characters ‘ ‘, return the length of last word in the string.

If the last word does not exist, return 0.

Note: A word is defined as a character sequence consists of non-space characters only.

For example, 
Given s = "Hello World",
return 5.

 

思路是从后往前,如果有字符,就++,如果是空,判断count是否有值

 

public class Solution {

    public int lengthOfLastWord(String s) {

        int count = 0;

        char[] wholeSentence = s.toCharArray();

        for(int i = wholeSentence.length - 1; i >= 0 ; i--){

            if(wholeSentence[i] != ‘ ‘){

                count ++;

            }

            else{

                if(count != 0){

                    return count;

                }

            }

        }

        return count;

    }

}

  

 

3.power of two

 

public class Solution {

    public int lengthOfLastWord(String s) {

        int count = 0;

        char[] wholeSentence = s.toCharArray();

        for(int i = wholeSentence.length - 1; i >= 0 ; i--){

            if(wholeSentence[i] != ‘ ‘){

                count ++;

            }

            else{

                if(count != 0){

                    return count;

                }

            }

        }

        return count;

    }

}

  

to judge whether a integer is power of two

an integer can divide two totally util it becomes 1 if it is power of two

 

4.Summary Ranges      【需要重新考虑的问题】

Given a sorted integer array without duplicates, return the summary of its ranges.

For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"].

 

 1 public class Solution {
 2 
 3     public List<String> summaryRanges(int[] nums) {
 4 
 5         List<String> result = new ArrayList<String>();
 6 
 7         List<Integer> temp = new ArrayList<Integer>();
 8 
 9         if(nums.length > 0){
10 
11         for(int i = 0; i< nums.length; i++){
12 
13             temp.add(nums[i]);
14 
15             if(i != (nums.length -1) && nums[i + 1] == nums[i] + 1){
16 
17                 continue;
18 
19             }
20 
21             else{
22 
23                 result.add(generateStr(temp));
24 
25                 temp.clear();
26 
27             }
28 
29         }
30 
31     }
32 
33     return result;
34 
35     }
36 
37    
38 
39     private String generateStr(List<Integer> temp){
40 
41         if(temp.size() != 1){
42 
43         String generatedStr = temp.get(0).toString() + "->" + temp.get(temp.size()-1).toString();
44 
45         return generatedStr;}
46 
47         else{
48 
49             return temp.get(0).toString();
50 
51         }
52 
53     }
54 
55 }

 

 

取一个区间,放在一个list,然后把这个list转换成一个string,这个应该算是比较笨的办法,仔细想想怎么做会更加好?

 

5.valid anagram

 1 Given two strings s and t, write a function to determine if t is an anagram of s.
 2 
 3 For example,
 4 s = "anagram", t = "nagaram", return true.
 5 s = "rat", t = "car", return false.
 6 
 7 Note:
 8 You may assume the string contains only lowercase alphabets. 
 9 
10 public class Solution {
11 
12     public boolean isAnagram(String s, String t) {
13 
14  
15 
16     Map<Character, Integer> sHash = new HashMap<Character, Integer>();
17 
18     Map<Character, Integer> tHash = new HashMap<Character, Integer>();
19 
20    
21 
22     char[] sArray = s.toCharArray();
23 
24     char[] tArray = t.toCharArray();
25 
26    
27 
28     if(sArray.length != tArray.length)
29 
30         return false;
31 
32     for(int i = 0; i < sArray.length; i++){
33 
34         if(sHash.containsKey(sArray[i])){
35 
36             int count = sHash.get(sArray[i]);
37 
38             sHash.put(sArray[i], ++count);
39 
40         }
41 
42         else{
43 
44             sHash.put(sArray[i], 1);
45 
46         }
47 
48         if(tHash.containsKey(tArray[i])){
49 
50            int count = tHash.get(tArray[i]);
51 
52            tHash.put(tArray[i], ++count);
53 
54         }
55 
56         else{
57 
58             tHash.put(tArray[i], 1);
59 
60         }
61 
62     }
63 
64    
65 
66     return sHash.equals(tHash);
67 
68     }
69 
70    
71 
72 }

 

 

这里使用了hashmap的比较方法,应该来讲,效率比较高

 

6.

contains duplicate 2

Given an array of integers and an integer k, find out whether there there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between iand j is at most k.

 1 public class Solution {
 2 
 3     public boolean containsNearbyDuplicate(int[] nums, int k) {
 4 
 5         if(nums.length == 0 ) return false;
 6 
 7         if(k == 0) return false;
 8 
 9         Map<Integer,List<Integer>> intHash = new HashMap<Integer, List<Integer>>();
10 
11         for(int i = 0; i< nums.length; i++){
12 
13             if(intHash.containsKey(nums[i])){
14 
15                 List<Integer> indexList = intHash.get(nums[i]);
16 
17                 indexList.add(i);
18 
19                 intHash.put(nums[i], indexList);
20 
21             }
22 
23             else{
24 
25                 List<Integer> indexList = new ArrayList<Integer>();
26 
27                 indexList.add(i);
28 
29                 intHash.put(nums[i], indexList);
30 
31             }
32 
33         }
34 
35        
36 
37         if(!intHash.isEmpty()){
38 
39             for(Integer i: intHash.keySet()){
40 
41             List<Integer> indexList = intHash.get(i);
42 
43             for(int j = 0; j< indexList.size() - 1; j++){
44 
45                 for(int m = j+1; m < indexList.size(); m++){
46 
47                     if((indexList.get(m) - indexList.get(j)) <= k && (indexList.get(m) - indexList.get(j)) >=(0-k) ){
48 
49                         return true;
50 
51                     }
52 
53                 }
54 
55             }
56 
57         }
58 
59         }
60 
61        
62 
63         return false;
64 
65     }
66 
67 }

第一个版本的算法是:从头开始取数,然后在这个数的k区间内取别的数,进行对比,判断是否有一样的,一样就返回true,没有的话最后返回false

但是在处理[1-29999],k值为29999的时候出现了问题,是因为没有重复的数字,全部遍历了一遍,时间复杂度为o(n*n),造成OJ上面的runtime超时。

 

 

LeetCode 边做边记录

标签:

原文地址:http://www.cnblogs.com/michael-faust/p/4701189.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!