<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實(shí)戰(zhàn)170:兩數(shù)之和 III - 數(shù)據(jù)結(jié)構(gòu)設(shè)計(jì)

          共 2020字,需瀏覽 5分鐘

           ·

          2021-02-02 07:16

          算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識(shí)和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個(gè)公眾號(hào)后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

          今天和大家聊的問題叫做?兩數(shù)之和 III - 數(shù)據(jù)結(jié)構(gòu)設(shè)計(jì)??,我們先來看題面:
          https://leetcode-cn.com/problems/two-sum-iii-data-structure-design/

          Design and implement a TwoSum class. It should support the following operations: add
          and find.
          add(input) – Add the number input to an internal data structure.
          find(value) – Find if there exists any pair of numbers which sum is equal to the value.

          題意



          設(shè)計(jì)并實(shí)現(xiàn)一個(gè) TwoSum 的類,使該類需要支持 add 和 find 的操作。
          add 操作 - 對(duì)內(nèi)部數(shù)據(jù)結(jié)構(gòu)增加一個(gè)數(shù)。
          find 操作 - 尋找內(nèi)部數(shù)據(jù)結(jié)構(gòu)中是否存在一對(duì)整數(shù),使得兩數(shù)之和與給定的數(shù)相等。

          樣例

          示例 1:

          add(1); add(3); add(5);
          find(4) -> true
          find(7) -> false

          示例 2:

          add(3)
          ; add(1); add(2);
          find(3) -> true
          find(6) -> false



          解題

          • 在類下面聲明屬性

          • 在構(gòu)造函數(shù)方法里初始化類的屬性

          • 在其他函數(shù)對(duì)屬性和函數(shù)進(jìn)行調(diào)用完成功能


          class?TwoSum?{
          ????????//屬性
          ????????private?ArrayList nums;
          ????????private?boolean is_sorted;
          ????/** Initialize your data structure here. */
          ????public?TwoSum(){
          ????????this.nums = new?ArrayList();
          ????????is_sorted =false;
          ????}
          ????/** Add the number to an internal data structure.. */
          ????public?void?add(int?number) {
          ????????this.nums.add(number);
          ????????this.is_sorted = false;

          ????}
          ????
          ????/** Find if there exists any pair of numbers which sum is equal to the value. */
          ????public?boolean find(int?value) {
          ?????????if?(!this.is_sorted) {
          ??????//調(diào)用Collections類進(jìn)行數(shù)組排序
          ??????Collections.sort(this.nums);
          ????}
          ????int?low = 0, high = this.nums.size() - 1;
          ????while?(low < high) {
          ??????int?twosum = this.nums.get(low) + this.nums.get(high);
          ??????if?(twosum < value)
          ????????low += 1;
          ??????else?if?(twosum > value)
          ????????high -= 1;
          ??????else
          ????????return?true;
          ????}
          ????//默認(rèn)返回false
          ????return?false;
          ????}
          }


          好了,今天的文章就到這里,如果覺得有所收獲,請(qǐng)順手點(diǎn)個(gè)在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動(dòng)力 。

          上期推文:

          LeetCode1-160題匯總,希望對(duì)你有點(diǎn)幫助!
          LeetCode刷題實(shí)戰(zhàn)161:相隔為1的編輯距離
          LeetCode刷題實(shí)戰(zhàn)162:尋找峰值
          LeetCode刷題實(shí)戰(zhàn)163:缺失的區(qū)間
          LeetCode刷題實(shí)戰(zhàn)164:最大間距
          LeetCode刷題實(shí)戰(zhàn)165:比較版本號(hào)
          LeetCode刷題實(shí)戰(zhàn)166:分?jǐn)?shù)到小數(shù)
          LeetCode刷題實(shí)戰(zhàn)167:兩數(shù)之和 II - 輸入有序數(shù)組
          LeetCode刷題實(shí)戰(zhàn)168:Excel表列名稱
          LeetCode刷題實(shí)戰(zhàn)169:多數(shù)元素


          瀏覽 25
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  天天干天天操综合网 | 91成人视频 亚洲一区二区 | 日本色播视频在线播放 | 国产精品视频在线播放 | 视频一区二区三区在线观看 |