This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * The MIT License (MIT) | |
| * | |
| * Copyright (c) 2014 Matthieu Harlé | |
| * | |
| * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| * of this software and associated documentation files (the "Software"), to deal | |
| * in the Software without restriction, including without limitation the rights | |
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| * copies of the Software, and to permit persons to whom the Software is |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // one month | |
| let dateFormatter = DateFormatter() | |
| dateFormatter.dateFormat = "yyyyMMdd" | |
| let today = dateFormatter.date(from: calenderDate)! | |
| let startOfMonth = Calendar.current.date(from: Calendar.current.dateComponents([.year, .month, .day], from: Calendar.current.startOfDay(for: today)))! | |
| let endOfMonth = Calendar.current.date(byAdding: DateComponents(month: 1, day: -1), to: startOfMonth)! | |
| let startOfMonString = dateFormatter.string(from: startOfMonth) | |
| let endOfMonString = dateFormatter.string(from: endOfMonth) | |
| NSLog(dateFormatter.string(from: startOfMonth)) | |
| NSLog(dateFormatter.string(from: endOfMonth)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public int rob(TreeNode root) { | |
| if (root == null) return 0; | |
| int notRobAmt = rob(root.left, false) + rob(root.right, false); | |
| int robAmt = rob(root.left, true) + rob(root.right, true) + root.val; | |
| return Math.max(notRobAmt, robAmt); | |
| } | |
| private int rob(TreeNode root, boolean robbed) { | |
| if (root == null) return 0; | |
| if (robbed) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Solution { | |
| public int rob(int[] nums) { | |
| if (nums == null || nums.length == 0) return 0; | |
| if (nums.length == 1) return nums[0]; | |
| int[] s = new int[nums.length]; | |
| s[0] = nums[0]; | |
| s[1] = Math.max(nums[0], nums[1]); | |
| for (int i = 2; i < nums.length; i++) { | |
| s[i] = Math.max(s[i-2] + nums[i], s[i-1]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func textToSpeech(text: String) { | |
| let synthesizer:AVSpeechSynthesizer = AVSpeechSynthesizer() | |
| let utterance:AVSpeechUtterance = AVSpeechUtterance(string: text) | |
| utterance.rate = 0.1 | |
| synthesizer.speakUtterance(utterance) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private void hideSoftKeyBoard() { | |
| final View myCurrentFocusView = getCurrentFocus(); | |
| myCurrentFocusView.postDelayed(new Runnable() { | |
| @Override | |
| public void run() { | |
| // TODO Auto-generated method stub | |
| InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); | |
| imm.hideSoftInputFromWindow(myCurrentFocusView.getWindowToken(), 0); | |
| } | |
| },100); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public boolean wordPattern(String pattern, String str) { | |
| if (pattern == null || str == null) return false; | |
| String[] words = str.split(" "); | |
| if (pattern.length() != words.length) return false; | |
| HashMap<String, Character> sMap = new HashMap<String, Character>(); | |
| HashMap<Character, String> cMap = new HashMap<Character, String>(); | |
| for (int i = 0; i < words.length; i++) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # mirror sentence | |
| # eg: This is my test. -> .tset ym si sihT | |
| " ".join(map(lambda x: x[::-1], s.split())) | |
| # reverse string | |
| # eg: test -> tset | |
| "test"[::-1] | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| Write a function that takes a string as input and reverse only the vowels of a string. | |
| Example 1: | |
| Given s = "hello", return "holle". | |
| Example 2: | |
| Given s = "leetcode", return "leotcede". | |
| Note: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. | |
| Find all the elements that appear twice in this array. | |
| Could you do it without extra space and in O(n) runtime? | |
| Example: | |
| Input: | |
| [4,3,2,7,8,2,3,1] |