發表文章

目前顯示的是有「leetcode」標籤的文章

[Leetcode]485. Max Consecutive Ones

求出連續相連幾個數字1 Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1,1,0,1,1,1] Output: 3 Explanation: The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3. Note: The input array will only contain  0  and  1 . The length of input array is a positive integer and will not exceed 10,000 Think: 宣告一個記數、暫存 計算如果遇到1就加一,反之 是0 就歸零 當num[i]==0 時,判斷 記數是否大於暫存 ,是 則將 記數值存入暫存 JAVA class Solution {     public int findMaxConsecutiveOnes(int[] nums) {         int count = 0;         int tem = 0;         for(int i = 0; i<nums.length;i++){             if(nums[i]==1){                 count++;             }             else{                 if(count>=t...

[Leetcode] 234. Palindrome Linked List

判斷是否是回文 Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false Example 2: Input: 1->2->2->1 Output: true 先將串列放入list內 判斷第一的位置和最後一個位置的數值相減是否為0,若不為0,則記數+1 判斷記數是否為0,若為0 則True ,反之 False Python    # Definition for singly-linked list.  # class ListNode(object):  # def __init__(self, val=0, next=None):  # self.val = val  # self.next = next   class Solution(object):     def isPalindrome(self, head):    """    :type head: ListNode    :rtype: bool    """    tem=[]    count = 0    while(head!= None ):    tem.append(head.val)    head=head.next    length=len(tem)/2     if(length==0):    return True     else:    for i in range(length):           if(tem[i]-tem[-i-1] != 0):    count+=1 ...

[LeetCode] 258. Add Digits

Given a non-negative integer  num , repeatedly add all its digits until the result has only one digit. Example: Input: 38 Output: 2 Explanation: The process is like: 3 + 8 = 11 , 1 + 1 = 2 .   Since 2 has only one digit, return it. Follow up: Could you do it without any loop/recursion in O(1) runtime?

[Java] Array 跟 ArrayList 的區別

Array是Java中的基本功能。 而ArrayList就是Collection Framework的一部分。 因此就有著完全不同的使用方式! 創建方式不同 Array 是一個有固定大小的Array。 每次創建一個新的Array時,都需要為它設定一個大小。 而且在創建後,是不能再更改大小。 int arr[] = new int[10] ArrayList 是一個有浮動大小的Array。 每次創建一個新的Array時,不需要為它設定大小。 因為在創建後,你可以隨意更改它的大小。 另外,它是List interface的實作。 ArrayList arrL = new ArrayList(); 新增元素及存取元素的方式不同 Array透過[]的方式新增元素而ArrayList就透過add()。 Array透過[]的方式存取元素而ArrayList就透過get()。

[Java]Array跟list的用法

 java.util.ArrayList  類提供了可調整大小的數組, 並實現了List接口。以下是關於ArrayList中的要點: 它實現了所有可選的列表操作,並且還允許所有元素,包括空值null。 它提供了一些方法來操作內部用來存儲​​列表的數組的大小。 相較於LinkedList實現的常數因子較低。 ArrayList<資料型態> arrlist = new ArrayList<資料型態>(資料大小) ; //資料大小沒寫代表無限制           ArrayList<String> arrlist = new ArrayList< String >() ;      ArrayList<Integer> arrlist = new ArrayList< Integer >() ;      ArrayList<Boolean> arrlist = new ArrayList<Boolean>() ; 參考來源: http://tw.gitbook.net/java/util/java_util_arraylist.html //判斷矩陣內的數值+額外的數值是否超過矩陣的最大值 如果大於等於 則為 true 反之 false //輸出一個list<boolean> Given the array  candies  and the integer  extraCandies , where  candies[i]  represents the number of candies that the  ith  kid has. For each kid check if there is a way to distribute  extraCandies  among the kids such that he or she can have the  greatest  number of candies...

[Java] 數字跟字串轉換

   int n =1234; 方法一:  String str1 = Integer.toString(n);  方法二:  String str3 = String.valueOf(n); 字串轉成矩陣   String[] stringarray = str1.split(""); // stringarray =[1,2,3,4] 字串轉成數字 int index1 = Integer.parseInt(stringarray[0]); ------------------------------------------------------- [LeetCode] 1281 .  Subtract the Product and Sum of Digits of an Integer Given an integer number  n , return the difference between the product of its digits and the sum of its digits.   Example 1: Input: n = 234 Output: 15 Explanation: Product of digits = 2 * 3 * 4 = 24 Sum of digits = 2 + 3 + 4 = 9 Result = 24 - 9 = 15 Example 2: Input: n = 4421 Output: 21 Explanation: Product of digits = 4 * 4 * 2 * 1 = 32 Sum of digits = 4 + 4 + 2 + 1 = 11 Result = 32 - 11 = 21   Constraints: 1 <= n <= 10^5