iT邦幫忙

2021 iThome 鐵人賽

DAY 1
1
AI & Data

想到甚麼打甚麼系列 第 1

找LeetCode上簡單的題目來撐過30天啦(DAY1)

  • 分享至 

  • xImage
  •  

哈囉,各位好,我本來想寫一些,自己想學、有主題的東西,但考量最近工作繁忙,自己又在準備一些考試,所以今年就在開賽前最後一刻決定寫些比較不怕生不出來的東西,找LeetCode上簡單的題目來撐過30天啦

作為一個上班不用寫甚麼程式,快要變成文組生的我,寫有邏輯的程式已經快要是一年前的事情了,所以不勉強自己,找簡單的開始吧!今天暖暖身,寫個一題!

今天的題目上菜囉
題號1. 標題Two Sum 難度Easy
題目內容
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Output: Because nums[0] + nums[1] == 9, we return [0, 1].
Example 2:
Input: nums = [3,2,4], target = 6
Output: [1,2]
Example 3:
Input: nums = [3,3], target = 6
Output: [0,1]

Constraints:
• 2 <= nums.length <= 104
• -109 <= nums[i] <= 109
• -109 <= target <= 109
• Only one valid answer exists.

我的程式

class Solution {
    public int[] twoSum(int[] nums, int target) {
        int len = nums.length;
        int i ,j ;
        int[] result;
        result = new int[2]; 
        for( i=0 ; i<len ; i++){
           for(j=i+1;j<len;j++){ 
                if(nums[i]+nums[j]==target){
                    result[0]=i;
                    result[1]=j;}    
        }}
        return result;
    }
}

邏輯
沒什麼邏輯,就是就是全部掃一遍,能夠組成TARGET數字就給他RETURN出去

DAY1心得
我覺得中秋節年假要飛囉


下一篇
找LeetCode上簡單的題目來撐過30天啦(DAY2)
系列文
想到甚麼打甚麼30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
阿瑜
iT邦研究生 4 級 ‧ 2021-09-16 23:27:32

李臻最近有寫這題

看更多先前的回應...收起先前的回應...
soft_soft iT邦新手 5 級 ‧ 2021-09-17 08:38:42 檢舉

叫他不要跟菜鳥搶題目~
寫Hard啊(不說了快告訴我她有沒有比較聰明的解題方法

阿瑜 iT邦研究生 4 級 ‧ 2021-09-17 10:57:54 檢舉

沒問題! 我連結貼給他

為什麼你們都知道誰寫什麼題@@LeetCode可以互相看?

阿瑜 iT邦研究生 4 級 ‧ 2021-09-17 23:27:50 檢舉

因為
我們同一個Team
&&
我最近跟李臻像麻糬一樣

soft_soft iT邦新手 5 級 ‧ 2021-09-18 00:04:14 檢舉

不要在我的文章下面放閃喔/images/emoticon/emoticon23.gif

我要留言

立即登入留言