2019.9.10
771.寶石與石頭
public int numJewelsInStones(String J, String S) {
if (J == null || S == null) return 0;
if (J.length() == 0 || S.length() == 0) return 0;
Byte[] b = new Byte[58];
int count = 0;
for(char ch: J.toCharArray()){
b[ch - 'A'] = 1;//'A'的ASCII碼是65,'A'和'z'的ASCII碼相差58
}
for(char ch: S.toCharArray()){
if(b[ch - 'A'] != null && b[ch - 'A']== 1)
count++;
}
return count;
}
2019.9.17
1108.IP 地址無效化
和劍指offer的【字符串-替換空格】差不多的思路
public String defangIPaddr(String address) {
int count = 0;//記錄字符串中.的個數
for(int i = 0; i < address.length(); i++){
if(address.charAt(i) == '.')
count++;
}
int oldLength = address.length();
StringBuffer s = new StringBuffer(address);
s.setLength(oldLength+count*2);
for(int k = oldLength - 1; k > -1; k--){
if(count > 0){
if(s.charAt(k) != '.'){
s.setCharAt(k+count*2,s.charAt(k));
}else{
s.replace(k+count*2-2,k+count*2+1,"[.]");
count--;
}
}
else
break;
}
return s.toString();
}
237.刪除鏈表中的節點
這題題意有點難理解,deleteNode傳參是待刪除的那個節點
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public void deleteNode(ListNode node) {
node.val = node.next.val;
node.next = node.next.next;
node = null;
//在鏈表上已刪除節點後,清空它爲null,方便GC標記回收
}
}
182.查找重複的電子郵箱(原來力扣還有MySQL的題)
count 是聚合函數,使用 count(*) 要比count(字段) 要快,省略了判斷字段是否爲 null 的過程,mysql 對 count(*) 做了優化。
SELECT Email FROM Person GROUP BY Email HAVING COUNT(*) > 1;
# 或者
# select distinct a.Email from Person a, Person b where a.Email = b.Email and a.Id != b.Id
938.二叉搜索樹的範圍和(又是題目都看不懂的我,還有救嗎)
參數給了二叉搜索樹所屬的兩個節點,求二叉搜索樹節點值按序排列時兩節點值間(包括兩節點)的所有節點值的和。可以遍歷節點去判斷節點值是否在這兩個節點值間,在配合剪枝減少沒必要的節點遍歷
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
import java.util.Stack;
class Solution {
private int res = 0;//記錄和
public int rangeSumBST(TreeNode root, int L, int R) {
if (root == null) return 0;
else{
dfs(root, L, R);
}
return res;
}
//遞歸
private void dfs(TreeNode root, int L, int R){
if(root != null){
if(root.val > L){
dfs(root.left, L, R);
}
if(root.val < R){
dfs(root.right, L, R);
}
if(root.val >= L && root.val <= R){
res += root.val;
}
}
}
//迭代
private void dfs1(TreeNode root, int L, int R){
Stack<TreeNode> stack = new Stack<TreeNode>();
stack.push(root);
while(!stack.isEmpty()){
TreeNode t = stack.pop();
if(t != null){
if(t.val > L){
stack.push(t.left);
}
if(t.val < L){
stack.push(t.right);
}
if(t.val >= L && t.val <= R){
res += t.val;
}
}
}
}
}
2019.9.18
1021.刪除最外層的括號
//用棧記錄左右括號的匹配
public static String removeOuterParentheses(String S) {
Stack<Character> stack = new Stack<>();
StringBuffer sB = new StringBuffer();
int start = 0;
for(int i = 0; i < S.length(); i++){
if(S.charAt(i) == '('){
stack.push(S.charAt(i));
}else {
stack.pop();
if(stack.empty()){
sB.append(S.substring(start+1, i));
start = i + 1;
}
}
}
return sB.toString();
}
//直接計數,效率更高。
//最外面的左括號跳過,L初始爲1;當右括號數量等於左括號數量的位置就是最外面的右括號
public static String removeOuterParentheses1(String S) {
StringBuffer sB = new StringBuffer();
int L = 1, R = 0;
for(int i = 1; i < S.length(); i++){
if(S.charAt(i) == '('){
sB.append(S.charAt(i));
L++;
}else {
R++;
if(L == R){
i++;
L = 1;
R = 0;
}else {
sB.append(S.charAt(i));
}
}
}
return sB.toString();
}
595.大的國家(拯救信心繫列)
SELECT name, population, area FROM WORLD WHERE area > 3000000 OR population > 25000000
709.轉換成小寫字母(拯救信心繫列)
public String toLowerCase(String str) {
StringBuffer sB = new StringBuffer(str);
for(int i = 0; i < str.length(); i++){
int a = str.charAt(i) - 'A';
if(a >= 0 && a <= 25){
sB.setCharAt(i, (char)('a'+a));
}
}
return sB.toString();
}
//效率更高。ASCII碼 'A'=65 'a'=97
public String toLowerCase1(String str) {
StringBuffer sB = new StringBuffer(str);
for(int i = 0; i < str.length(); i++){
if(str.charAt(i) >= 65 && str.charAt(i) <= 90){
sB.setCharAt(i, (char)(str.charAt(i)+32));
}
}
return sB.toString();
}
617.合併二叉樹(明明想法一樣,我就遞歸不出來,哭。。。)
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
if(t1 == null) return t2;
if(t2 == null) return t1;
t1.val = t1.val + t2.val;
t1.left = mergeTrees(t1.left, t2.left);
t1.right = mergeTrees(t1.right, t2.right);
return t1;
}
804.唯一摩爾斯密碼詞(拯救信心繫列)
改進:String+HashMap ——> StringBuffer+HashSet
public int uniqueMorseRepresentations(String[] words) {
String[] array = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
//HashMap<String, Byte> map = new HashMap<>();
HashSet<String> set = new HashSet<>();//考慮用
for(String s: words){
//要操作字符串的話,StringBuffer比String高效一點
StringBuffer tmp = new StringBuffer();
for(int i = 0; i < s.length(); i++){
tmp.append(array[s.charAt(i)-'a']);
}
//不用判斷map是否包含key,因爲如果key已存在,put會把value直接覆蓋
//map.put(tmp.toString(),(byte)0);
set.add(tmp.toString());
}
//return map.size();
return set.size();
}
832.翻轉圖像(拯救信心繫列)
public static int[][] flipAndInvertImage(int[][] A) {
int col = A[0].length;
for(int i = 0; i < A.length; i++){
for(int k = 0; k < (col+1)/2; k++){
if(A[i][k] == A[i][col-1-k]){
if(k != col-1-k){
A[i][col-1-k]= (A[i][k]==1?0:1);
}
A[i][k] = (A[i][k]==1?0:1);
}
}
}
return A;
}
620.有趣的電影(。。第一次竟然沒敲出來)
MySQL用mod(row_name, number)函數來取模;用id&1來判斷奇數更快;根據最左前綴和and的短路,讓where先判斷id再去判斷description,更快;ANSI標準中是用<>表示不等於,雖然也可以用!=
SELECT * FROM cinema WHERE id&1 AND description <> 'boring' ORDER BY rating DESC
461.漢明距離
先異或,然後用劍指offer裏的【二進制數中1的個數】
public int hammingDistance(int x, int y) {
int xor = x ^ y;
int count = 0;
while(xor != 0){
count += xor&1;
xor>>=1;
}
return count;
}
226.翻轉二叉樹(劍指offer【二叉樹的鏡像】)
private TreeNode tmp = null;
public TreeNode invertTree(TreeNode root) {
if(root == null) return null;
tmp = root.left;
root.left = root.right;
root.right = tmp;
invertTree(root.left);
invertTree(root.right);
return root;
}
657.機器人能否返回原點
public boolean judgeCircle(String moves) {
int x = 0, y = 0;
for(int i = 0; i < moves.length(); i++){
switch (moves.charAt(i)){
case 'L' : x++; break;
case 'R' : x--; break;
case 'U' : y++; break;
case 'D' : y--; break;
}
}
return x == 0 && y == 0;
}
1051.高度檢查器
public static int heightChecker(int[] heights) {
int[] res = new int[100];//用於計數排序
int count = 0;
for(int i = 0; i < heights.length; i++){
res[heights[i]-1]++;
}
int k = 0;
for(int i = 0; i < heights.length; i++){
while (res[k] == 0){
k++;
}
if(k!=heights[i]-1) count++;
res[k]--;
}
return count;
}
627.交換工資
抄的。SQL有char(int)、ascii(char)函數?牛逼!!
update salary set sex = char(ASCII(sex) ^ ASCII('m') ^ ASCII('f')); #暫時這個最棒
#UPDATE salary SET sex = char(ascii('m') + ascii('f') - ascii(sex));
官方的case…when…也可以學學
UPDATE salary
SET sex =
CASE sex
WHEN 'm' THEN 'f'
ELSE 'm'
END;
咱變着花樣來,還有if
update salary set sex=if(sex = 'f', 'm','f');