Leecode刷题

来源:互联网 发布:sjcam sj5000x淘宝 编辑:程序博客网 时间:2024/05/22 07:41
  1. Reverse Integer QuestionEditorial Solution My Submissions
    Total Accepted: 163775
    Total Submissions: 687339
    Difficulty: Easy
    Reverse digits of an integer.
    Example1: x = 123, return 321
    Example2: x = -123, return -321

1)我的解法:主要通过toCharArray(),substring(),Integer.valueOf()方法;

public class Solution {public static void main(String[] args) {    Solution s=new Solution();    s.reverse(123);}    public int reverse(int x) {       char []c=null;       String strArr="";       if(x==0){           return 0;       }       else if(x>0){           c=(x+"").toCharArray();       }else{           strArr="-";           c=(x+"").substring(1).toCharArray();       }       for(int i=c.length-1;i>=0;i--){           strArr+=c[i];       }       return Integer.valueOf(strArr);    } }
  1. Palindrome Number(回文数字)
    方法一:
public class Solution {    public boolean isPalindrome(int x) {       int num=1;       if(x<0){           return false;       }if(x<10){           return true;       }       while(x/num<10){           num*=10;       }       while(x!=0){           int left=x/num;           int right=x%10;           if(left!=right){               return false;           }          x=(x%num)/10;          num/=100;       }        return true;     }}

方法二:转换一种思想,不用逐一去比较只要用算法求得回文后的数字再直接进行比较。

public class Solution {    public boolean isPalindrome(int x) {     int reverse=0;     int num=1;     if(x<0){         return false;     }     while(x/num>10){         num*=num;     }     while(x!=0){     x/=10;     reverse=reverse+(x%10)*num;     num/=10;     }     return reverse==x;    }}

Longest Palindromic Substring
1)我的解法:运用指针的思想forword和back进行比较

public class Solution {    public String longestPalindrome(String s) {    char[] c=s.toCharArray();    int forword=0;    int back=c.length-1;    String str;    while(forword<=c.length-1&&back>=0){    if(c[forword]==c[back]){      forword++;      back--;    }    else if(c[forword]!=c[back]){        forword=0;        back--;    }}    str=s.substring(0,forword);    return str;     }    }

Remove Duplicates from Sorted Array

public class Solution {    public int removeDuplicates(int[] nums) {     if(nums.length == 0) return 0;     int end=1;     int dup=nums[0];     for(int i=1;i<nums.length;i++){         if(nums[i]!=dup){                 nums[end]=nums[i];                 dup=nums[i];                 end++;         }     }     return end;    }}

Move Zeroes

 public class Solution {    public void moveZeroes(int[] nums) {      int add=0;      int end=0;      for(int i=0;i<nums.length;i++){         if(nums[i]!=0){             nums[end]=nums[i];             end++;         }else{             add++;         }      }         while(add!=0){         nums[end]=0;         add--;         end++;         }}}

Search Insert Position
Here are few examples.
[1,3,5,6], 5 → 2
[1,3,5,6], 2 → 1
[1,3,5,6], 7 → 4
[1,3,5,6], 0 → 0

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

Best Time to Buy and Sell Stock
Input: [7, 1, 5, 3, 6, 4]
Output: 5
max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price)
Input: [7, 6, 4, 3, 1]
Output: 0
In this case, no transaction is done, i.e. max profit = 0.
1)我的解法:两层for循环,导致时间复杂度为O(n^2);

public class Solution {    public int maxProfit(int[] prices) {        int max=0;        int cha=0;     for(int i=0;i<prices.length;i++){         for(int j=i+1;j<prices.length;j++)         if(prices[j]>=prices[i]){             cha=prices[j]-prices[i];             if(cha>max){                 max=cha;             }         }     }     return max;    }}

2)另外一种算法思想:
分析:动态规划法。从前向后遍历数组,记录当前出现过的最低价格,作为买入价格,并计算以当天价格出售的收益,作为可能的最大收益,整个遍历过程中,出现过的最大收益就是所求。

public class Solution {    public int maxProfit(int[] prices) {        if(prices.length==0){            return 0;        }        int max=0;        int min=prices[0];        for(int i=1;i<prices.length;i++){          if(prices[i]<min){              min=prices[i];          }          else if(prices[i]-min>max){          //则证明当前的值一定大于min进行相减,再跟max比较                  max=prices[i]-min;          }        }        return max;    }}

Best Time to Buy and Sell Stock II
用一个数组表示股票每天的价格,数组的第i个数表示股票在第i天的价格。交易次数不限,但一次只能交易一支股票,也就是说手上最多只能持有一支股票,求最大收益。

分析:贪心法。从前向后遍历数组,只要当天的价格高于前一天的价格,就算入收益。
代码:时间O(n),空间O(1)。

public class Solution {    public int maxProfit(int[] prices) {        if(prices.length<2){            return 0;        }      int maxprofit=0;     int diff;      for(int i=1;i<prices.length;i++){       diff = prices[i]-prices[i-1];        if(diff>0){            maxprofit+=diff;        }      }      return maxprofit;    }}

Merge Two Sorted Lists
将两个单链表合并,合并一个新的单链表

/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { val = x; } * } */public class Solution {    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {        ListNode head=null;        ListNode end=null;        ListNode first=l1;        ListNode second=l2;         if (l1 == null)          {              return l2;          }          if (l2 == null)           {              return l1;          }        while(first!=null&&second!=null){           int temp1=first.val;           int temp2=second.val;            if(temp1<=temp2){                if(head==null){                    head=first;                    end=head;                }                else{                    end.next=first;                    end=end.next;                }                 first = first.next;            }            else{                if(head==null){                    head=second;                    end=head;                }                else{                    end.next=second;                    end=end.next;                }                second = second.next;            }        }        if(first!=null){            end.next=first;        }        if(second!=null){           end.next = second;        }        return head;    }}
0 0
原创粉丝点击