日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

OA总结

發(fā)布時間:2023/12/20 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 OA总结 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Rotate String

  • 題目:
  • 思路:
class Solution {public boolean rotateString(String A, String B) {if (A == null || B == null || A.length() != B.length()) {return false;}String C = B + B;return C.indexOf(A) == -1 ? false : true;} } 復制代碼

Closest Two Sum max average sum subetree 兩種版本: 1.二叉樹 2.多個子節(jié)點 merge two Sorted List 一排數(shù)據(jù)中心,每個數(shù)據(jù)中心鏈接有cost, 求將所有數(shù)據(jù)中心連接起來并且cost最小的方式 Leetcode 819 Leetcode 973 K Closest Points to Origin

  • 問題: 給出一個二維數(shù)組表示一些點的坐標和一個值K,求出距離原點(0,0)最近的K個點,并放在二維數(shù)組中返回
  • 思路: 維護一個size 為K的最大堆,用來更新距離前k小的點

PriorityQueue<Integer> pq = new PriorityQueue(k, new Comparator<Integer>(public int compare(int a, int b) {return b - a;}))) compare是聲明的方法

PriorityQueue<Point> pq = new PriorityQueue<Point> (k, new Comparator<Point> () {@Overridepublic int compare(Point a, Point b) {return (int) (getDistance(a, origin) - getDistance(b, origin));} });// 實現(xiàn)最大堆的代碼需要記憶 class Solution {public int[][] kClosest(int[][] points, int K) {Queue<int[]> queue=new PriorityQueue<>((a, b) -> (a[0]*a[0]+a[1]*a[1])-(b[0]*b[0]+b[1]*b[1]));int[][] res=new int[K][2];int index=0;for(int[] arr:points) {queue.add(arr);}while(index<K) {res[index]=queue.poll();index++;}return res;} } 復制代碼

LintCode High Five

public class Solution {/*** @param results a list of <student_id, score>* @return find the average of 5 highest scores for each person* Map<Integer, Double> (student_id, average_score)*/public Map<Integer, Double> highFive(Record[] results) {Map<Integer, Double> answer = new HashMap<Integer, Double>();Map<Integer, PriorityQueue<Integer>> hash = new HashMap<Integer, PriorityQueue<Integer>>();for (Record r : results) {if (!hash.containsKey(r.id)){hash.put(r.id, new PriorityQueue<Integer>());}PriorityQueue<Integer> pq=hash.get(r.id);if (pq.size() < 5) {pq.add(r.score);} else {if (pq.peek() < r.score){pq.poll();pq.add(r.score);}}}for (Map.Entry<Integer, PriorityQueue<Integer>> entry : hash.entrySet()) {int id = entry.getKey();PriorityQueue<Integer> scores = entry.getValue();double average = 0;for (int i = 0; i < 5; ++i)average += scores.poll();average /= 5.0;answer.put(id, average);}return answer;} } 復制代碼

K - 1 Substring

Valid Parenthesis

[LintCode] 597 Subtree with Maximum Average

  • 題目: 給出一棵二叉樹,找出其中平均值最大的子樹節(jié)點
  • 思路: 新寫一個ResultType類來將保存node,size子樹節(jié)點個數(shù),sum(子樹值的總和)
  • 變形: 給出多叉樹,或者只能返回非葉節(jié)點
public class Solution {/*** @param root: the root of binary tree* @return: the root of the maximum average of subtree*/private class Result {private int sum;private int size;private TreeNode node;public Result(int sum, int size, TreeNode node) {this.sum = sum;this.size = size;this.node = node;}}private Result currMax = null;public TreeNode findSubtree2(TreeNode root) {// write your code hereif (root == null) {return null;}helper(root);return currMax.node;}private Result helper(TreeNode node) {if (node == null) {return new Result(0,0,null);}Result left = helper(node.left);Result right = helper(node.right);Result res = new Result(left.sum + right.sum + node.val,left.size + right.size + 1,node);if (currMax == null || res.sum * currMax.size > res.size * currMax.sum) {currMax = res;}return res;} } 復制代碼public class Solution {class Result {CategoryNode node;int sum;int size;public Result(CategoryNode node, int sum, int size) {this.node = node;this.sum = sum;this.size = size;}}private Result currMax = null;public CategoryNode getMostPupularNode(CategoryNode rootCategory) {if (rootCategory == null) {return null;}Result rootRes = helper(rootCategory);return res.node;}public Result helper(CategoryNode root) {if (root == null) {return new Result(null, 0, 0);}ArrayList<Result> results = new ArrayList<Result>();for (CategoryNode n : root.subCategoryNode) {results.add(helper(n));}int childSum = 0;int childSize = 0;for (Result r : results) {childSum += r.sum;childSize += r.size;}Result c = new Result(root)} }復制代碼

819. Most Common Word

  • 題目: 給出一個長的String段落和一個詞匯黑名單,找出不在禁用詞當中的頻率最高的詞
  • 思路: 先切割段落,然后統(tǒng)計詞頻,找出不在黑名單中的單詞 統(tǒng)計詞頻是用HashMap. 切割段落的時候要同時用到空格和逗號兩個分隔符?
// 直接一句話搞定 兩種情況 在map中的+1 和 不在map中的直接新建為1for (String word : words) {map.put(word, map.getOrDefault(word, 0) + 1);}class Solution {public String mostCommonWord(String paragraph, String[] banned) {if (paragraph == null || paragraph.length() == 0) {return "";}HashMap<String, Integer> map = new HashMap<>();// 使用窮舉法進行段落分割 `+`加號在這里表示出現(xiàn)次數(shù)為多次String[] words = paragraph.toLowerCase().split("[ ?!;,.']+");for (String word : words) {map.put(word, map.getOrDefault(word, 0) + 1);}for (String word : banned) {if (map.containsKey(word)) {map.remove(word);}}String res = null;for (String key : map.keySet()) {if (res == null || map.get(key) > map.get(res)) {res = key;System.out.println(key + "" + map.get(key));}}return res;} } 復制代碼

雙指針類

所有的雙指針類型的題目都是要維持指針區(qū)間,一定會有多個循環(huán)條件,已經(jīng)指針的更新條件的變化,注意指針移動變化之后的變量與不變量 兩個指針更新方式 可以通過while loop或者 for loop

  • 同向雙指針
  • Longest Substring with At Most Two Distinct Characters
    • 題目: 找出只有兩個distinct字符的最長子串
    • 思路: 用count[]數(shù)組來記錄出現(xiàn)次數(shù), 用distinctCount變量記錄出現(xiàn)的distinct字符數(shù)
    class Solution { public int lengthOfLongestSubstringTwoDistinct(String s) {if (s == null || s.length() == 0) {return 0;} int maxSize = 0;int size = s.length();int[] count = new int[128];int distinctCount = 0;int j = 0;for (int i = 0; i < size; i++) {while (j < size) {if (distinctCount == 2 && count[s.charAt(j)] == 0) {break;}if (count[s.charAt(j)] == 0) {distinctCount++;}count[s.charAt(j++)]++;}maxSize = Math.max(maxSize, j - i);count[s.charAt(i)]--;if (count[s.charAt(i)] == 0) {distinctCount--;}}return maxSize; } 復制代碼

} ```

  • Longest Substring with At Most K Distinct Characters
  • 相向雙指針

LintCode 533 [Two Sum Closest]

  • 對撞型雙指針
  • 問題: 找到不大于k的最接近的一組數(shù)字
  • 思路: 先排序
public int twoSumClosest(int[] nums, int target) {if (nums == null || nums.length < 2) {return -1;}Arrays.sort(nums);int left = 0;int right = nums.length - 1;int diff = Integer.MAX_VALUE;while (left < right) {if (nums[left] + nums[right] < target) {diff = Math.min(diff, target - nums[left] - nums[right]);left++;}else {diff = Math.min(diff, nums[left] + nums[right] - target);right--;}}return diff; } 復制代碼

Shortest path in a Binary Maze

  • https://en.wikipedia.org/wiki/Lee_algorithm Lee Algorithm:
  • initialize
  • wave expansion: repeat mark all unlabeled neighbors of points marked with i with i + 1
  • go to target point -> repeat go to next node that has a lower mark than the current node
  • clear marks
  • Reorder 字符的字典序比較 dictionary order alphabetical order "ah1".compareTo("ahb") 比較的就是兩個字符的ASCII差值,如果小于零說明第一個字符串小于第二個字符串

    64. Minimum Path Sum

    • 題目: 給出一個m * n的方格,找出一條從左上到右下和最小的路徑, 只可以向下或向右移動
    • 思路: 局部最小和全局最小之間的關(guān)系?全局最小也是局部最小
    public class Solution {public int minPathSum(int[][] grid) {if (grid == null || grid.length == 0 || grid[0].length == 0) {return 0;}int M = grid.length;int N = grid[0].length;int[][] sum = new int[M][N];sum[0][0] = grid[0][0];for (int i = 1; i < M; i++) {sum[i][0] = sum[i - 1][0] + grid[i][0];}for (int i = 1; i < N; i++) {sum[0][i] = sum[0][i - 1] + grid[0][i];}for (int i = 1; i < M; i++) {for (int j = 1; j < N; j++) {sum[i][j] = Math.min(sum[i - 1][j], sum[i][j - 1]) + grid[i][j];}}return sum[M - 1][N - 1];} }復制代碼

    K - 1 distinct characters

    • 問題: 給一個字符串和一個整數(shù)數(shù)字
    • 輸入: inputString = "awaglk" num = 4
    • 輸出: ["awag"]
    • 思路: 雙指針, 1個指針固定頭, 另一個直接
    import java.util.*; public class Solution {public static List<String> Kdistinct(String s, int k) {List<String> res = new ArrayList<>();if (s == null || s.length() == 0) {return res;} int size = s.length();int[] count = new int[128];int distinctCount = 0;for (int i = 0; i < size; i++) {Arrays.fill(count, 0);distinctCount = 0;for (int j = i; j < size; j++) {if (count[s.charAt(j)] == 0) {distinctCount++;}System.out.println("i: " + i);System.out.println("j: " + j);System.out.println("distinct: " + distinctCount);if (distinctCount == k - 1 && j - i + 1== k) {res.add(s.substring(i, j + 1));}count[s.charAt(j)]++;}}return res;}public static void main(String[] args) {int n = 4;String input = "awagkmg";List<String> res = Kdistinct(input, n);for (String s : res) { System.out.println(s);}} } 復制代碼

    K-Substring with K different characters

    public class Solution {/*** @param stringIn: The original string.* @param K: The length of substrings.* @return: return the count of substring of length K and exactly K distinct characters.*/public int KSubstring(String stringIn, int K) {if (stringIn == null || stringIn.length() == 0 || K <= 0) {return 0;}int count = 0;HashMap<Character, Integer> charMap = new HashMap<>();HashSet<String> resultSet = new HashSet<String>();int len = stringIn.length();int j = 0;for (int i = 0; i < len; i++) {while (j < len && charMap.size() < K) {char c = stringIn.charAt(j);if (charMap.containsKey(c)) {break;}charMap.put(c, 1);j++;// size == k是保證了子串長度為kif (charMap.size() == K) {resultSet.add(stringIn.substring(i, j));}}charMap.remove(stringIn.charAt(i));}return resultSet.size();} } 復制代碼

    937. Reorder Log Files

    • 問題: 本質(zhì)上是字符串的字典序比較 分為三種情況討論 identifier > letter > digit
    • 思路: 自定義一個Comparator進行比較
    class Solution {public String[] reorderLogFiles(String[] logs) {Comparator<String> com = new Comparator<String>() {@Overridepublic int compare(String s1, String s2) {int s1Start = s1.indexOf(' ');int s2Start = s2.indexOf(' ');char s1Char = s1.charAt(s1Start + 1);char s2Char = s2.charAt(s2Start + 1);if (s1Char <= '9') {if (s2Char <= '9') { // natural orderreturn 0;} else { // s1 is digit-log, s2 is letter-logreturn 1;}}if (s2Char <= '9') { // s2 is digit-log, s1 is letter-logreturn -1;}// s1 and s2 are both letter-logint preCompute = s1.substring(s1Start + 1).compareTo(s2.substring(s2Start + 1));// ignore identifier letter-logs are ordered tieif (preCompute == 0) {return s1.substring(0, s1Start).compareTo(s2.substring(0, s2Start));}return preCompute;}};Arrays.sort(logs, com);return logs;} } 復制代碼

    轉(zhuǎn)載于:https://juejin.im/post/5c7945156fb9a049c43e6116

    總結(jié)

    以上是生活随笔為你收集整理的OA总结的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。