LeetCode-Algorithms #009 Palindrome Number, Database #184 Department Highest Salary
LeetCode-Algorithms #009 Palindrome Number
判斷一個整數是否是迴文數.
進階版: 不將原數轉化為字串的前提下判斷一個數是否是迴文數
先用字串做一次:
1 class Solution { 2 public boolean isPalindrome(int x) { 3 //負數都不是迴文數 4 if(x < 0)return false; 5 //將原數轉化為字串 6 String s = Integer.toString(x); 7 //將字串轉換為字元陣列8 char[] arr = s.toCharArray(); 9 //遍歷字元陣列判斷是否是迴文 10 for(int i = 0; i < arr.length / 2; i++){ 11 //不是就返回false 12 if(arr[i] != arr[arr.length - 1 - i])return false; 13 } 14 //是就返回true 15 return true; 16 } 17 }
很簡單, 沒什麼好說的, 那麼如果不使用字串呢?
我的想法是用一個linkedlist儲存各位數字, 然後驗證:
1 class Solution { 2 public boolean isPalindrome(int x) { 3 // 負數都不是迴文數 4 if (x < 0) 5 return false; 6 //建立一個LinkedList儲存各位數字 7 LinkedList<Integer> list = new LinkedList<>(); 8 while (x != 0) {9 int pop = x % 10; 10 x /= 10; 11 list.add(pop); 12 } 13 //獲取位數 14 int len = list.size(); 15 //驗證是否是迴文數 16 for(int i = 0; i < len/2; i++) { 17 if(list.removeFirst() != list.removeLast()) return false; 18 } 19 return true; 20 } 21 }
兩個寫法速度都普普通通, 看一看別人的作法:
class Solution { public boolean isPalindrome(int x) { String s = Integer.toString(x); String reverse = new StringBuffer(s).reverse().toString(); if(s.equals(reverse)){ return true; } else{ return false; } } }
StringBuffer本身就自帶反轉功能, 這個是我已經忘了.
1 class Solution { 2 public boolean isPalindrome(int x) { 3 if (x<0 || (x!=0 && x%10==0)) 4 return false; 5 int rev = 0; 6 while (x>rev){ 7 rev = rev*10 + x%10; 8 x = x/10; 9 } 10 return (x==rev || x==rev/10); 11 } 12 }
這個做法也很清楚, 直接把反轉後的數字算出來進行比較, 但是是否可以只做一半次數的迴圈?
我試著寫了一下, 不過沒弄得很仔細:
1 class Solution { 2 public boolean isPalindrome(int x) { 3 if (x<0 || (x!=0 && x%10==0)) return false; 4 if (x == 0) return true; 5 int y = 0; 6 while (x != 0) { 7 int pop = x % 10; 8 x /= 10; 9 if(x==y)return true; 10 y = y * 10 + pop; 11 if(x==y)return true; 12 } 13 return false; 14 } 15 }
但是這樣每一位迴圈要做兩次判斷, 並沒有實質的節省計算, 上面別人那個寫法是把判斷放在外面, 顯然比較快
LeetCode-Database #184 Department Highest Salary
如圖找出各部門各自工資最高的員工
不太會寫, 用GROUP BY加上MAX我知道, 但是GROUP BY之後不能確定找到的員工名字是工資最高的那個
答案是這樣的:
1 SELECT 2 Department.name AS 'Department', 3 Employee.name AS 'Employee', 4 Salary 5 FROM 6 Employee 7 JOIN 8 Department ON Employee.DepartmentId = Department.Id 9 WHERE 10 (Employee.DepartmentId , Salary) IN 11 ( SELECT 12 DepartmentId, MAX(Salary) 13 FROM 14 Employee 15 GROUP BY DepartmentId 16 ) 17 ;
原來如此, 是把這個東西作為查詢範圍來使用的:
1 SELECT MAX(Salary), DepartmentId 2 From Employee 3 GROUP bY DepartmentId;
相關推薦
LeetCode-Algorithms #009 Palindrome Number, Database #184 Department Highest Salary
LeetCode-Algorithms #009 Palindrome Number 判斷一個整數是否是迴文數. 進階版: 不將原數轉化為字串的前提下判斷一個數是否是迴文數 先用字串做一次: 1 class Solution { 2 public boolean isPalindrome(in
[LeetCode] Algorithms-9. Palindrome Number
mcs npe mar algo etc lan targe href leet U骨沿17貿V日禱吞3http://weibo.com/p/1005055847889847 17北ZPF皇3懶Rhttp://huiyi.docin.com/sina_6269974098
【LeetCode】184. Department Highest Salary
The Employee table holds all employees. Every employee has an Id, a salary, and there is also a column for the department Id. +----+----
leetcode.184. Department Highest Salary
The Employee table holds all employees. Every employee has an Id, a salary, and there is also a column for the department Id. +----+---
184. Department Highest Salary (medium)
https pro table select company leet have fin ems Source: https://leetcode.com/problems/department-highest-salary/#/descriptionDescription
184. Department Highest Salary
The Employee table holds all employees. Every employee has an Id, a salary, and there is also a column for the department Id. +----+---
資料庫:184. Department Highest Salary
The Employee table holds all employees. Every employee has an Id, a salary, and there is also a
leetcode題解 9. Palindrome Number
ber HR 哈哈 tin using scrip 情況 == while 9. Palindrome Number 題目: Determine whether an integer is a palindrome. Do this without extra space.
【leetcode】 9. palindrome number
ble number cli ack ont art ews styles doc @requires_authorization @author johnsondu
LeetCode-Algorithms #006 ZigZag Conversion, Database #180 Employees Earning More Than Their Managers
from arr 結果 toc strong 遍歷 har ++ 以及 LeetCode-Algorithms #006 ZigZag Conversion 給定一個字符串, 以及需要輸出的行數, 將字符串按照給定行數進行Z字形排列(效果見上圖), 然後將得到的結果逐行
【LeetCode】126.Palindrome Number
題目描述(Easy) Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
【LeetCode】9. Palindrome Number - Java實現
文章目錄 1. 題目描述: 2. 思路分析: 3. Java程式碼: 1. 題目描述: Determine whether an integer is a palindrome. An integer is a palindrome
【leetcode】9. Palindrome Number
Description: Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
【演算法】LeetCode演算法題-Palindrome Number
這是悅樂書的第144次更新,第146篇原創 今天這道題和迴文有關,即從前往後和從後往前是一樣的,如“上海自來水來自海上”就是一個迴文字串,如整數121就是迴文數,這些都是和迴文相關的。 01 看題和準備 今天介紹的是LeetCode演算法題中Easy級別的第3題
Crack LeetCode 之 9. Palindrome Number
這一題巧妙運用取餘和除法運算來取十進位制數字的高位和低位。之前我還想到過把數字轉為字串,然後再檢查,但是這樣效率比較低。以下為C++的程式碼和python的程式碼,時間複雜度是O(n),空間複雜度是O(1)。 struct Solution { bool isPalindrome(int
LeetCode-Algorithms #007 Reverse Integer, Database #182 Duplicate Emails
LeetCode-Algorithms #007 Reverse Integer 給定一個32位整數, 將其各位反轉並返回, 如果結果超出取值範圍就返回0 1 class Solution { 2 public int reverse(int x) { 3 //對原數取絕對值
LeetCode-Algorithms #002 Add Two Numbers, Database #176 Second Highest Salary
LeetCode-Algorithms #002 Add Two Numbers 給定兩個非空的以連結串列結構表示的非負整數, 這個結構長這樣: 1 public class ListNode { 2 int val; 3 ListNode next; 4 ListNode(int
LeetCode-Algorithms #006 ZigZag Conversion, Database #181 Employees Earning More Than Their Managers
LeetCode-Algorithms #006 ZigZag Conversion 給定一個字串, 以及需要輸出的行數, 將字串按照給定行數進行Z字形排列(效果見上圖), 然後將得到的結果逐行拼接成一個新的字串, 返回這個新串 我的思路: 通過給定的行數計算每一個字元在Z形排列中位於哪一行並不困難, 如
LeetCode-Algorithms #003 Longest Substring Without Repeating Characters, Database #177 Nth Highest Salary
LeetCode-Algorithms #003 Longest Substring Without Repeating Characters 對於給定的字串, 找出其每個字元都不重複的子串中最長的那個, 並返回該子串的長度: 想法還是遍歷: 1 class Solution { 2 pu
LeetCode-Algorithms #001 Two Sum, Database #175 Combine Two Tables
最近兩週一直感覺學習比較鬆懈, 打算加大一點強度, 從今天開始, 希望能在每天正常進度完成後在LeetCode上選一兩題寫一寫, 同時學習一下高手的做法. LeetCode - Algorithms #001 Two Sum 給定一個整數陣列, 找出其中兩個元素, 使其和等於目標值, 返回這兩個元素在原陣