| 廣告聯系 | 簡體版 | 手機版 | 微信 | 微博 | 搜索:
歡迎您 游客 | 登錄 | 免費注冊 | 忘記了密碼 | 社交賬號注冊或登錄

首頁

新聞資訊

論壇

溫哥華地產

大溫餐館點評

溫哥華汽車

溫哥華教育

黃頁/二手

旅游
搜索:  

 論壇通告:  請不要上傳第三方有版權的照片,請尊重版權,謝謝   轉載新聞請務必注明出處,這些媒體請不要轉,謝謝   批評商家需要注意  
 個人空間: XY | lxls | 豬頭看世界 | 格局 | NotmeL8 | 羅蓬特機器人 | 白龍王許道長 | 一襲絳襦落鵬城,疑似玄女下九天 | 花隨風 | 呂洪來的個人空間 | 靜觀雲卷雲舒 | 顧曉軍 | 客觀中立而實事求是,唯服理據而杜絕辱罵 | 逸言堂 | 大溫房產和地產研究 | 我的退休生活 | 禪人俗事 | 湖裡湖塗 | 天涯逐夢 | My AI Tech Channel
 最新求助: 請問誰知道哪裡有賣理發的電動推子?   忽然有個疑問:戰爭時期,加拿大拿PR卡未入籍的永久居民會被強制服兵役嗎?   這個銀條   如何修改會員名?
 論壇轉跳:
     發帖回帖獲取加西鎊, 兌換精彩禮物

論壇首頁 -> IT人生

Leetcode Hacking Practice -- Solution in Java Part 2 (發表於10年前)

分頁: 1, 2, 3 ... 11, 12, 13  下一頁  



回復主題  圖片幻燈展示  增添帖子到書簽中  給帖子中的發貼者批量贈送獻花或者花籃    |##| -> |=|        發表新主題
閱讀上一個主題 :: 閱讀下一個主題  
作者 正文
webdriver
(只看此人)




文章 時間: 2014-9-27 00:49 引用回復
LeetCode (used to call i has 1337 code) is a social platform for preparing IT technical interviews. We strive to provide you with the best learning experience in preparing interviews for companies in the IT industry.

To be successful in a technical interview, we believe it is mainly repeating these three important steps:

Code. Read. Discuss.

We strive to provide you the LeetCode platform as the ultimate solution for preparing technical interviews.

1. Code -> Code solution using the Online Judge system.
2. Read -> Read high quality article featuring in-depth thought process. Also read other LeetCoders’ code.
3. Discuss -> Discuss your thoughts about the problem with other LeetCoders.

We hope that through our platform, you will grow into a LeetCoder. Not only will you be successful in all of your interviews, and most importantly, you will be a better coder in general !

This is Part 2 (Java Solution)

Part 1: www.westca.com/Forums/...inese.html


 
花籃
分享
_________________
There is no wisdom tree; nor a stand of a mirror bright, Since all is void, where can the dust alight?
樓主 | 電梯直達
閱讀會員資料 發送站內短信 主題 User photo gallery 禮物  
webdriver
(只看此人)




文章 時間: 2014-9-28 00:55 引用回復
S算法問題: Longest Palindromic Substring
問題描述:
Given a string S, find the longest palindromic substring in S.
You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.
 
花籃
分享
_________________
There is no wisdom tree; nor a stand of a mirror bright, Since all is void, where can the dust alight?
沙發 | 返回頂端
閱讀會員資料 發送站內短信 主題 User photo gallery 禮物  
webdriver
(只看此人)



文章 時間: 2014-9-28 00:58 引用回復
算法問題推薦解法來了...

1. Time O(n^2), Space O(n^2)
2. Time O(n^2), Space O(n)
3. Time O(n^2), Space O(1) (actually much more efficient than 1 & 2)
4. Time O(n), Space O(n) (Manacher's Algorithm)

解法(Java)
代碼:

public class LongestPalindromicSubstring {
   public String longestPalindrome(String s) {
      int length = s.length();
      String result = "";
      for (int i = 0; i < length; i++) {
         String ps = getPalindrome(s, i, i);
         if (ps.length() > result.length()) {
            result = ps;
         }
         ps = getPalindrome(s, i, i + 1);
         if (ps.length() > result.length()) {
            result = ps;
         }
      }
      return result;
   }

   private String getPalindrome(String s, int l, int r) {
      while (l >= 0 && r < s.length() && s.charAt(l) == s.charAt(r)) {
         l--;
         r++;
      }
      return s.substring(l + 1, r);
   }
}
 
花籃
分享
_________________
There is no wisdom tree; nor a stand of a mirror bright, Since all is void, where can the dust alight?
板凳 | 返回頂端
閱讀會員資料 發送站內短信 主題 User photo gallery 禮物  
webdriver
(只看此人)



文章 時間: 2014-9-28 00:59 引用回復
S算法問題: Longest Substring Without Repeating Characters
問題描述:
Given a string, find the length of the longest substring without repeating characters.
For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3.
For "bbbbb" the longest substring is "b", with the length of 1.
 
花籃
分享
_________________
There is no wisdom tree; nor a stand of a mirror bright, Since all is void, where can the dust alight?
地板 | 返回頂端
閱讀會員資料 發送站內短信 主題 User photo gallery 禮物  
webdriver
(只看此人)



文章 時間: 2014-9-28 01:00 引用回復
算法問題推薦解法來了...

Pay attention when moving the 'start' pointer forward.

解法(Java)
代碼:


import java.util.HashMap;


public class LongestSubstringWithoutRepeatingCharacters {
   public int lengthOfLongestSubstring(String s) {
      if (s.length() == 0)
         return 0;
      int i = 0, j = 0;
      int result = 0;
      HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
      while (j < s.length()) {
         Integer c = new Integer(s.charAt(j));
         if (!map.containsKey(c)) {
            map.put(c, j);
         } else {
            int length = j - i;
            if (result < length) {
               result = length;
            }
            Integer index = map.get(c);
            i = Math.max(i, index + 1);
            map.put(c, j);
         }
         j++;
      }

      if (result < j - i)
         return j - i;
      else
         return result;
   }
}
 
花籃
分享
_________________
There is no wisdom tree; nor a stand of a mirror bright, Since all is void, where can the dust alight?
5 樓 | 返回頂端
閱讀會員資料 發送站內短信 主題 User photo gallery 禮物  
webdriver
(只看此人)



文章 時間: 2014-9-28 01:01 引用回復
==== 今天的學習很緊張 ====
 
花籃
分享
_________________
There is no wisdom tree; nor a stand of a mirror bright, Since all is void, where can the dust alight?


上一次由webdriver於2014-9-28 01:22修改,總共修改了1次
6 樓 | 返回頂端
閱讀會員資料 發送站內短信 主題 User photo gallery 禮物  
webdriver
(只看此人)



文章 時間: 2014-9-28 01:02 引用回復
S算法問題: LowestCommonAncestorOfaBinarySearchTree
問題描述:

Lowest Common Ancestor of a Binary Search Tree (BST)

Given a binary search tree (BST), find the lowest common ancestor of two given nodes in the BST.


_______6______
\
___2__ ___8__
\ \
0 _4 7 9
\
3 5
Using the above tree as an example, the lowest common ancestor (LCA) of nodes 2 and 8 is 6.
But how about LCA of nodes 2 and 4? Should it be 6 or 2?

According to the definition of LCA on Wikipedia: ??The lowest common ancestor is defined between
two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a
node to be a descendant of itself).?? Since a node can be a descendant of itself, the LCA of 2 and
4 should be 2, according to this definition.

Hint:
A top-down walk from the root of the tree is enough.
 
花籃
分享
_________________
There is no wisdom tree; nor a stand of a mirror bright, Since all is void, where can the dust alight?
7 樓 | 返回頂端
閱讀會員資料 發送站內短信 主題 User photo gallery 禮物  
webdriver
(只看此人)



文章 時間: 2014-9-28 01:02 引用回復
算法問題推薦解法來了...



解法(Java)
代碼:
public class LowestCommonAncestorOfaBinarySearchTree {
   public TreeNode LCA(TreeNode root, TreeNode p, TreeNode q) {
      if (root == null || p == null || q == null)
         return null;
      if (Math.max(p.val, q.val) < root.val)
         return LCA(root.left, p, q);
      if (Math.min(p.val, q.val) > root.val)
         return LCA(root.right, p, q);
      return root;
   }
}
 
花籃
分享
_________________
There is no wisdom tree; nor a stand of a mirror bright, Since all is void, where can the dust alight?
8 樓 | 返回頂端
閱讀會員資料 發送站內短信 主題 User photo gallery 禮物  
webdriver
(只看此人)



文章 時間: 2014-9-28 01:03 引用回復
S算法問題: LowestCommonAncestorOfBinaryTree
問題描述:

Given a binary tree, find the lowest common ancestor of two given nodes in the tree.


_______3______
\
___5__ ___1__
\ \
6 _2 0 8
\
7 4
If you are not so sure about the definition of lowest common ancestor (LCA), please refer to my previous
post: Lowest Common Ancestor of a Binary Search Tree (BST) or the definition of LCA here. Using the tree
above as an example, the LCA of nodes 5 and 1 is 3. Please note that LCA for nodes 5 and 4 is 5.

Hint:
Top-down or bottom-up? Consider both approaches and see which one is more efficient.
 
花籃
分享
_________________
There is no wisdom tree; nor a stand of a mirror bright, Since all is void, where can the dust alight?
9 樓 | 返回頂端
閱讀會員資料 發送站內短信 主題 User photo gallery 禮物  
webdriver
(只看此人)



文章 時間: 2014-9-28 01:04 引用回復
算法問題推薦解法來了...



解法(Java)
代碼:
public class LowestCommonAncestorOfBinaryTree {
   public TreeNode LCA(TreeNode root, TreeNode p, TreeNode q) {
      if (root == null)
         return null;
      if (root == p || root == q)
         return root;
      TreeNode left = LCA(root.left, p, q);
      TreeNode right = LCA(root.right, p, q);
      if (left != null && right != null)
         return root;
      return left != null ? left : right;
   }
}
 
花籃
分享
_________________
There is no wisdom tree; nor a stand of a mirror bright, Since all is void, where can the dust alight?
10 樓 | 返回頂端
閱讀會員資料 發送站內短信 主題 User photo gallery 禮物  
 
回復主題     |##| -> |=|     論壇首頁 -> IT人生 所有的時間均為 美國太平洋時間
1頁,共13 分頁: 1, 2, 3 ... 11, 12, 13  下一頁  


注:
  • 以上論壇所有發言僅代表發帖者個人觀點, 並不代表本站觀點或立場, 加西網對此不負任何責任。
  • 投資理財及買房賣房版面的帖子不構成投資建議。投資有風險,責任請自負
  • 對二手買賣中的虛假信息,買賣中的糾紛等均與本站無關。
  • 黃頁熱門商家 免費個人廣告
    發布商業廣告

    不能在本論壇發表新主題
    不能在本論壇回復主題
    不能在本論壇編輯自己的文章
    不能在本論壇刪除自己的文章
    不能在本論壇發表投票
    不能在這個論壇添加附件
    可以在這個論壇下載文件

    論壇轉跳: 

    webdriver, webdriver, webdriver, webdriver, webdriver, webdriver, webdriver, webdriver, webdriver, webdriver
    潛力帖子 精華帖子 熱門帖子
    紅場閱兵大陸儀仗登熱搜,普京朋友...
    今天看眼醫
    普京在紅場閱兵式上講話:贊賞中國...
    應該軍事雜志介紹一下
    戰機是陣風還是大疆?
    飯都快吃不起了
    習近平亂扯二戰歷史被台灣打臉;
    印巴空戰最新消息解讀!中國軍備在...
    莫斯科衛國戰爭勝利80周年閱兵實況...
    中俄聯合聲明
    殲十多了個名號
    的確不太好,但是挺逗的!
    中國高鐵牛啊
    個人認為很危險的想法
    巴印空戰0比6之啟示
    5月2日換幣盛況
    維達大師,另類收藏,請您欣賞!
    清代福州台伏鈔票
    四川官錢局鈔票
    大漢四川軍政府軍用銀票
    今年新幣發行計劃
    要出一個新的一元
    古董金幣
    mint三月新幣(四月新幣從22樓起,五...
    1999 mule 25分
    2025 蛇年敲幣活動
    加拿大新總理馬克卡尼
    我在小紅書被罵窮得沒錢給孩子買衣服
    美國2025年AWQ(美國婦女25c)發行計劃
    韓國空難FDR黑匣子缺失最後四分鍾關...
    皮爾今天在溫哥華 - 藍色wave - 保...
    幾分鍾前,中國強硬反擊,征34+50,...
    曼谷高樓直接倒了
    我說我希望特朗普贏,老公氣得眼睛...
    知乎?加西網上為什麼有老男人喜歡...
    明明有能力統台,大陸為何遲遲不動手?
    貌似ndp稍占上風。。。。。
    今天是感恩節,跟大家道個別,以後...
    咱最後還是投了ndp
    生平第一次被偷車了
    中國會不會武統台灣
    突發:台灣隊戰勝中國隊奧運奪冠,...
    溫哥華房姐出事了
    有在看總統辯論的嗎?
    退休幾年後的感悟

    最新新聞 熱門新聞 熱評新聞
    TikTok惡作劇跟風 高中生半夜敲門遭屋主槍殺
    局勢升級!前所未見的轟炸要來了
    最強手機殼!他高空跳傘意外掉出iPhone....
    66歲逆齡男神演華人總探長,成"警察專業戶"型到炸裂
    "中國罕見提出大規模采購意向"(圖
    繼張維為之後,復旦又出了一個驚世之才
    涉嫌溫哥華素裡兩宗凶殺 男子被捕
    絕大多數加國人寧願在本國公路旅行,也不願前往美國
    列市這路交通堵塞因為警方在處理.
    印度凌晨不宣而戰 5大國第一時間收到通報 中國不在內
    川普與胡塞私下達成停火協議 以誓言"獨自"保衛自己
    不信美國人能當教宗!親兄弟曝新教宗"私下真實為人"
    川普高喊"美經濟將如火箭飆升":現在就去買美股!
    美警攔下無照女駕駛 驚見副駕"浣熊叼毒品"正在嗨
    美中談判 瑞士爆美中可能達成一重大成果
    范斯坦言烏克蘭戰況不利 俄羅斯在戰場占上風
    俄副總理:中俄"西伯利亞力量-2"項目談判取得進展
    美國農民發愁:雞爪魚頭,除了中國 難找到買家
    馬雲現身打卡阿裡總部"湖畔小屋" 鼓勵堅持創業精神
    中國四月出口同比增8.1% 對美出口下降17.6%
    大溫這市因"不當交易"損失$250萬
    首見匈牙利間諜網 吸收烏克蘭軍人 2嫌被捕
    日內瓦中美貿易談判,中國更具優勢
    因經濟不確定性 大溫房價可能下跌
    突發!綠黨反對這法案 BC或將大選
    泰國蘇提達王後回瑞士豪宅 60仆人伺候
    紐約時報刊文:新教皇可能很像老教皇
    印度和巴基斯坦的軍事實力對比(圖
    主動從美國退市!這家中國大廠打響第一槍
    中國選美冠軍栽了!名校學歷是假的 淒慘下場曝
    加國消減留學名額 院校五千人失業
    中國"仍然占上風" 語氣相當強硬
    這選區將重新計票 自由黨或增一席
    紀念蘇聯偉大衛國戰爭勝利80周年,紀念的是什麼?
    加拿大的國會要重開 這個黨尷尬了
    卡尼新內閣即將出爐 看年薪多少?
    英媒:比黃金還珍貴的月塵從中國運抵英國
    "太濕了先暫停一下" 人妻偷吃小王簡訊被發現
    獨家報道傳 川普最快下周將中國關稅降至…
    王丹:中國為何這麼快就與美談判?
    租了11年的房子要賣 大溫女子忠告
    中國體制內人員,欠薪風暴越刮越猛
    中國憂心"2下場"!選擇對美讓步(圖
    壓軸登場、長長紅地毯....習在莫斯科享盡榮耀
    美國農民發愁:雞爪魚頭,除了中國 難找到買家

    更多方式閱讀論壇:

    Android: 加西網
    [下載]

    Android: 溫哥華論壇
    [下載]

    PDA版本: 論壇

    加西網微信

    加西網微博


    Powered by phpBB 2.0.8
    Terms & Conditions    Privacy Policy    Political ADs    Activities Agreement    Contact Us    Sitemap    

    加西網為北美中文網傳媒集團旗下網站

    頁面生成: 0.0568 秒 and 7 DB Queries in 0.0019 秒