LeetCode-Algorithms #006 ZigZag Conversion, Database #181 Employees Earning More Than Their Managers
LeetCode-Algorithms #006 ZigZag Conversion
給定一個字串, 以及需要輸出的行數, 將字串按照給定行數進行Z字形排列(效果見上圖), 然後將得到的結果逐行拼接成一個新的字串, 返回這個新串
我的思路:
通過給定的行數計算每一個字元在Z形排列中位於哪一行並不困難, 如果要求是排列為n行, 那麼原字串中的字元所在的行數每前進2n-2個就會經歷一個迴圈
不難發現, 原字串中的第i個字元只要滿足如下條件, 就必然在Z形排列的第n行出現:
i % (2 * numRows - 2) == n || i % (2 * numRows - 2) == (2 * numRows - 2 - n)
這樣, 遍歷後就可以得到各行的子串, 拼接後即可得到結果:
1 class Solution { 2 public String convert(String s, int numRows) { 3 //排除特殊情況 4 if(numRows == 1) return s; 5 //建立一個字串儲存結果 6 String result = ""; 7 //把字串轉化為字元陣列 8 char[] arr = s.toCharArray(); 9 //獲取字串長度 10 intlen = arr.length; 11 12 //分別獲取每一行中的字元 13 for (int n = 0; n < numRows; n++) { 14 //建立一個空串儲存每一行中的字元 15 String temp = ""; 16 //遍歷字串陣列, 把符合條件的字元儲存至temp中 17 for (int i = 0; i < len; i++) { 18 if (i % (2 * numRows - 2) == n || i % (2 * numRows - 2) == (2 * numRows - 2 - n)) {19 temp = temp + arr[i]; 20 } 21 } 22 //把temp新增到result中 23 result = result + temp; 24 } 25 //返回結果 26 return result; 27 } 28 }
這樣處理雖然非常直觀, 但是效率很低, n行的結果就需要對字串進行n次遍歷, 顯然是很低效的
參考提示後對程式碼進行修改:
1 class Solution { 2 public String convert(String s, int numRows) { 3 //排除特殊情況 4 if(numRows == 1) return s; 5 //把字串轉化為字元陣列 6 char[] arr = s.toCharArray(); 7 //獲取字串長度 8 int len = arr.length; 9 //排除特殊情況 10 if(len<=numRows)return s; 11 //建立一個字串儲存結果 12 String result = ""; 13 //建立一個StringBuilder的List儲存各行結果 14 List<StringBuilder> list = new ArrayList<>(); 15 for(int i = 0; i < numRows; i++) { 16 list.add(new StringBuilder()); 17 } 18 19 //將各個字元新增至應在的行 20 for(int index = 0; index < len; index++) { 21 int rowIndex = (index % (2*numRows -2)) < numRows ? (index % (2*numRows -2)) : (2*numRows -2)-(index % (2*numRows -2)); 22 list.get(rowIndex).append(arr[index]); 23 } 24 //組合結果 25 for (StringBuilder stringBuilder : list) { 26 result += stringBuilder.toString(); 27 } 28 //返回結果 29 return result; 30 } 31 }
這樣顯然節省了不少計算, 但是實際上並沒有快很多, 主要問題我想還是出在細節上, 比如能用StringBuilder.append的地方用了字串拼接等等
看一看官方答案, 就漂亮多了:
1 class Solution { 2 public String convert(String s, int numRows) { 3 4 if (numRows == 1) return s; 5 6 List<StringBuilder> rows = new ArrayList<>(); 7 for (int i = 0; i < Math.min(numRows, s.length()); i++) 8 rows.add(new StringBuilder()); 9 10 int curRow = 0; 11 boolean goingDown = false; 12 13 for (char c : s.toCharArray()) { 14 rows.get(curRow).append(c); 15 if (curRow == 0 || curRow == numRows - 1) goingDown = !goingDown; 16 curRow += goingDown ? 1 : -1; 17 } 18 19 StringBuilder ret = new StringBuilder(); 20 for (StringBuilder row : rows) ret.append(row); 21 return ret.toString(); 22 } 23 }
用了一個指標來記錄遍歷到的字元應該在哪一行, 節省了不少計算
再來看另一種思路, 遍歷的過程和我最開始的方法近似, 但是每次不是每個字元都遍歷, 而是每新增一個字元就將指標後推一個迴圈:
1 class Solution { 2 public String convert(String s, int numRows) { 3 4 if (numRows == 1) return s; 5 6 StringBuilder ret = new StringBuilder(); 7 int n = s.length(); 8 int cycleLen = 2 * numRows - 2; 9 10 for (int i = 0; i < numRows; i++) { 11 for (int j = 0; j + i < n; j += cycleLen) { 12 ret.append(s.charAt(j + i)); 13 if (i != 0 && i != numRows - 1 && j + cycleLen - i < n) 14 ret.append(s.charAt(j + cycleLen - i)); 15 } 16 } 17 return ret.toString(); 18 } 19 }
LeetCode-#181 Employees Earning More Than Their Managers
在職員表中找到薪資比其經理還高的職員的名字
這幾天做過幾個數據庫的題目後終於有了點感覺, 也沒啥說的:
1 SELECT e1.Name AS Employee 2 FROM 3 Employee e1, 4 Employee e2 5 WHERE 6 e2.Id = e1.ManagerId 7 AND e1.Salary > e2.Salary 8 ;
或者也可以用join, 下面這是官方提供的答案之一:
1 SELECT 2 a.NAME AS Employee 3 FROM Employee AS a JOIN Employee AS b 4 ON a.ManagerId = b.Id 5 AND a.Salary > b.Salary 6 ;
據說用JOIN會比用WHERE快一些, 我也不太確定, 總之就聽他的好了, 以後考慮多用JOIN
相關推薦
LeetCode-Algorithms #006 ZigZag Conversion, Database #181 Employees Earning More Than Their Managers
LeetCode-Algorithms #006 ZigZag Conversion 給定一個字串, 以及需要輸出的行數, 將字串按照給定行數進行Z字形排列(效果見上圖), 然後將得到的結果逐行拼接成一個新的字串, 返回這個新串 我的思路: 通過給定的行數計算每一個字元在Z形排列中位於哪一行並不困難, 如
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# 181. Employees Earning More Than Their Managers
http tps write more span div leetcode tco ive https://leetcode.com/problems/employees-earning-more-than-their-managers/ The Employee
181. Employees Earning More Than Their Managers
The Employee table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id
[LeetCode] Employees Earning More Than Their Managers
tco mysql leetcode code hold ger sam class statement The Employee table holds all employees including their managers. Every employee has
LeetCode-超過經理收入的員工(employees-earning-more-than-their-managers)
超過經理收入的員工 難度 簡單 更多LeetCode答案歡迎大家關注Github: https://github.com/lxyer/LeetCodeAnswer Employee 表包含所有員工,他們的經理也屬於員工。每個員工都有一個 Id,此外還有一列
LeetCode Employees Earning More Than Their Managers
Problem The Employee table holds all employees including their managers. Every employee has an Id, and there is also a col
[LeetCode] Employees Earning More Than Their Managers 員工掙得比經理多
The Employee table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id. +----+-------+
[SQL]LeetCode181. 超過經理收入的員工 | Employees Earning More Than Their Managers
SQL架構 1 Create table If Not Exists Employee (Id int, Name varchar(255), Salary int, ManagerId int) 2 Truncate table Employee 3 insert into Emp
Employees Earning More Than Their Managers
The Employee table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id.
LeetCode 006 ZigZag Conversion - Java
mil cnblogs isp 字符串 lsi tex version wan app The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like th
【LeetCode-面試演算法經典-Java實現】【006-ZigZag Conversion(Z字型轉換)】
原題 The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows
LeetCode 006 ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a
leetcode題解 6.ZigZag Conversion
paypal 所有 lin 特殊 display log () take lsi 6.ZigZag Conversion 題目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given nu
【LeetCode】6.ZigZag Conversion Z字形變換
示例1: Input: s = "PAYPALISHIRING", numRows = 3 Output: "PAHNAPLSIIGYIR" Explanation: P A H N A P L S I I G Y I R 示例2: Input: s =
【LeetCode】6. ZigZag Conversion(C++)
題目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this
Leetcode Medium 0 ZigZag Conversion
第一次 196ms 18%: class Solution: def convert(self, s, numRows): l = [] r = '' for i in range(numRows):
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 #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 #001 Two Sum, Database #175 Combine Two Tables
最近兩週一直感覺學習比較鬆懈, 打算加大一點強度, 從今天開始, 希望能在每天正常進度完成後在LeetCode上選一兩題寫一寫, 同時學習一下高手的做法. LeetCode - Algorithms #001 Two Sum 給定一個整數陣列, 找出其中兩個元素, 使其和等於目標值, 返回這兩個元素在原陣